![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Level 2 Project Range Device
Dependencies: N5110 SDFileSystem SRF02 TMP102 mbed
Fork of Ranger by
Diff: main.cpp
- Revision:
- 4:673930f04866
- Parent:
- 3:8782b8b8658b
- Child:
- 5:083d484e95a1
--- a/main.cpp Sun Apr 10 15:29:57 2016 +0000 +++ b/main.cpp Thu Apr 14 14:23:37 2016 +0000 @@ -16,7 +16,7 @@ DigitalOut rr_led (PTA1); DigitalOut a_led (PTC2); DigitalOut gg_led(PTB23); - +PwmOut buzzer(PTA2); // Ranger object SRF02 srf02(I2C_SDA,I2C_SCL); @@ -26,6 +26,10 @@ Ticker ticker; Ticker ticker_srf02; +Ticker ticker_tone; +Timeout buzzoff; + + // K64F on-board LEDs DigitalOut r_led(LED_RED); @@ -39,7 +43,8 @@ volatile int g_timer_flag = 0; volatile int g_timer_flag_srf02 = 0; volatile int g_sw2_flag = 0; -//volatile int g_sw1_flag = 0; +volatile int g_timer_flag_tone = 0; +volatile int g_sw1_flag = 0; int alert; int distance; @@ -53,28 +58,34 @@ char frr_led; char fa_led; char fgg_led; + float toneon; + float toneoff; }; typedef const struct Alertlevel STyp; STyp Alertlevel[8] = { - {LOW,LOW,LOW,LOW,LOW,LOW}, // no output - {LOW,LOW,LOW,LOW,LOW,HIGH}, //flash green - {LOW,LOW,HIGH,LOW,LOW,LOW}, //steady green - {LOW,LOW,HIGH,LOW,HIGH,LOW}, //flash amber - {LOW,HIGH,HIGH,LOW,LOW,LOW}, //steady amber - {LOW,HIGH,HIGH,HIGH,LOW,LOW}, //flash red - {HIGH,HIGH,HIGH,LOW,LOW,LOW},// steady red - {LOW,LOW,LOW,HIGH,HIGH,HIGH} // all flash + {LOW,LOW,LOW,LOW,LOW,LOW,0,1}, // no output + {LOW,LOW,LOW,LOW,LOW,HIGH,0.05,1}, //flash green + {LOW,LOW,HIGH,LOW,LOW,LOW,0.1,1}, //steady green + {LOW,LOW,HIGH,LOW,HIGH,LOW,0.2,0.5}, //flash amber + {LOW,HIGH,HIGH,LOW,LOW,LOW,0.55,0.25}, //steady amber + {LOW,HIGH,HIGH,HIGH,LOW,LOW,0.8,0.1}, //flash red + {HIGH,HIGH,HIGH,LOW,LOW,LOW,1,1.05},// steady red + {LOW,LOW,LOW,HIGH,HIGH,HIGH,1,0.05} // all flash }; void lcdoutput(); void timer_isr(); // timed interuprt void timer_isr_srf02(); +void timer_isr_tone(); void backlight(); void init_K64F(); // set up board void sw2_isr(); //void sw1_isr(); void setalert(); void setleds(); +void setbuzzer(); +void flip(); + int main() { @@ -82,20 +93,27 @@ init_K64F(); ticker.attach(&timer_isr,0.35); ticker_srf02.attach(&timer_isr_srf02,0.02); + ticker_tone.attach(&timer_isr_tone,Alertlevel[alert].toneoff); // sw1.fall(&sw1_isr); sw2.fall(&sw2_isr); while(1) { + if (g_sw2_flag) { g_sw2_flag = 0; // if it has, clear the flag backlight(); } if (g_timer_flag_srf02) { g_timer_flag_srf02 = 0; // if it has, clear the flag - int distance = srf02.getDistanceCm(); + distance = srf02.getDistanceCm(); } + + lcdoutput(); setalert(); + + setbuzzer(); setleds(); + sleep(); } } @@ -123,9 +141,10 @@ length = sprintf(buffer,"D = %i Cm",distance); length1 = sprintf(buffer1,"Bright = %f Cm",bright); - if (length <= 14) + if (length <= 14) { lcd.printString(buffer,0,0); - lcd.printString(buffer1,0,1); + lcd.printString(buffer1,0,1); + } // draw a line across the display at y = 40 pixels (origin top-left) for (int i = 0; i < WIDTH; i++) { @@ -209,7 +228,6 @@ g_sw1_flag = 1; // set flag in ISR }*/ - void timer_isr() { g_timer_flag = 1; // set flag in ISR @@ -218,4 +236,24 @@ void timer_isr_srf02() { g_timer_flag_srf02 = 1; // set flag in ISR -} \ No newline at end of file +} + +void timer_isr_tone() +{ + g_timer_flag_tone = 1; +} + +void setbuzzer() +{ + if (g_timer_flag_tone) { + g_timer_flag_tone = 0; + buzzer.period (1/1000); + buzzer=0.5; + buzzoff.attach(&flip,Alertlevel[alert].toneon); + } + } + + void flip() { + buzzer = 0.0; + } +