Level 2 Project Range Device
Dependencies: N5110 SDFileSystem SRF02 TMP102 mbed
Fork of Ranger by
Diff: main.cpp
- Revision:
- 3:8782b8b8658b
- Parent:
- 2:329597081c06
- Child:
- 4:673930f04866
--- a/main.cpp Fri Apr 01 23:09:07 2016 +0000 +++ b/main.cpp Sun Apr 10 15:29:57 2016 +0000 @@ -3,7 +3,6 @@ EL15PJT Embedded System Project EL2645 - */ #include "mbed.h" @@ -13,10 +12,12 @@ #define LOW 0 #define HIGH 1 + DigitalOut rr_led (PTA1); DigitalOut a_led (PTC2); DigitalOut gg_led(PTB23); + // Ranger object SRF02 srf02(I2C_SDA,I2C_SCL); @@ -24,6 +25,7 @@ N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3); Ticker ticker; +Ticker ticker_srf02; // K64F on-board LEDs DigitalOut r_led(LED_RED); @@ -31,16 +33,17 @@ DigitalOut b_led(LED_BLUE); // K64F on-board switches -InterruptIn sw2(SW2); -InterruptIn sw3(SW3); +//InterruptIn sw1(SW2); +InterruptIn sw2(SW3); 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; +int alert; int distance; -int length; -int alert; - -char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) +float bright = 1; // struct for state struct Alertlevel { @@ -63,68 +66,37 @@ {HIGH,HIGH,HIGH,LOW,LOW,LOW},// steady red {LOW,LOW,LOW,HIGH,HIGH,HIGH} // all flash }; -// timed interuprt -void timer_isr(); - -// set up board -void init_K64F(); +void lcdoutput(); +void timer_isr(); // timed interuprt +void timer_isr_srf02(); +void backlight(); +void init_K64F(); // set up board +void sw2_isr(); +//void sw1_isr(); +void setalert(); +void setleds(); int main() { lcd.init(); init_K64F(); - ticker.attach(&timer_isr,0.05); - + ticker.attach(&timer_isr,0.35); + ticker_srf02.attach(&timer_isr_srf02,0.02); + // sw1.fall(&sw1_isr); + sw2.fall(&sw2_isr); while(1) { - -// read sensor distance in cm and print over serial port - int distance = srf02.getDistanceCm(); - length = sprintf(buffer,"D = %i Cm",distance); - if (length <= 14) - lcd.printString(buffer,0,2); -// need to refresh display after setting pixels - lcd.refresh(); - -// short delay before next measurement - wait(0.25); - -//Range Alert selection use to adjust alert boundarys - if (distance >= 200 && distance < 250) { - alert = 1; //flash green - } else if (distance >= 150 && distance < 200) { - alert = 2; //steady green - } else if (distance >= 100 && distance < 150) { - alert = 3; //flashing amber - } else if (distance >= 50 && distance < 100) { - alert = 4; //steady amber - } else if ( distance > 10 && distance < 50) { - alert = 5; //flashing red - } else if (distance > 1 && distance <= 10) { - alert = 6; //steady red - } else if (distance <=1) { - alert = 7; //all flashing - } else { - alert = 0; //no output + if (g_sw2_flag) { + g_sw2_flag = 0; // if it has, clear the flag + backlight(); } -// Steady light outputs - rr_led = Alertlevel[alert].srr_led; - a_led = Alertlevel[alert].sa_led; - gg_led = Alertlevel[alert].sgg_led; - -//Flashing light outputs - if (g_timer_flag) { - g_timer_flag = 0; // if it has, clear the flag - - if ( Alertlevel[alert].frr_led == HIGH) { - rr_led = !rr_led; - } else if ( Alertlevel[alert].fa_led == HIGH) { - a_led = !a_led; - } else if ( Alertlevel[alert].fgg_led == HIGH) { - gg_led = !gg_led; - } + if (g_timer_flag_srf02) { + g_timer_flag_srf02 = 0; // if it has, clear the flag + int distance = srf02.getDistanceCm(); } + lcdoutput(); + setalert(); + setleds(); } - } //Set up board switches and LEDS @@ -138,11 +110,112 @@ // since the on-board switches have external pull-ups, we should disable the internal pull-down // resistors that are enabled by default using InterruptIn sw2.mode(PullNone); - sw3.mode(PullNone); + // sw1.mode(PullNone); +} +void lcdoutput() +{ + int length; + int length1; + char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) + char buffer1[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) + + + length = sprintf(buffer,"D = %i Cm",distance); + length1 = sprintf(buffer1,"Bright = %f Cm",bright); + + if (length <= 14) + lcd.printString(buffer,0,0); + 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++) { + lcd.setPixel(i,45); + } + + lcd.refresh(); // need to refresh display after setting pixels +} + +void setalert() +{ + if (distance >= 200 && distance < 250) { + alert = 1; //flash green + } else if (distance >= 150 && distance < 200) { + alert = 2; //steady green + } else if (distance >= 100 && distance < 150) { + alert = 3; //flashing amber + } else if (distance >= 50 && distance < 100) { + alert = 4; //steady amber + } else if ( distance > 10 && distance < 50) { + alert = 5; //flashing red + } else if (distance > 1 && distance <= 10) { + alert = 6; //steady red + } else if (distance <=1) { + alert = 7; //all flashing + } else { + alert = 0; //no output + } } +void setleds() +{ + // If statments to determin the output of each LED + + if (Alertlevel[alert].frr_led == HIGH) { + if (g_timer_flag) { + g_timer_flag = 0; // if it has, clear the flag + rr_led = !rr_led; + } + } else { + rr_led = Alertlevel[alert].srr_led; + } + + if (Alertlevel[alert].fa_led == HIGH) { + if (g_timer_flag) { + g_timer_flag = 0; // if it has, clear the flag + a_led = !a_led; + } + } else { + a_led = Alertlevel[alert].sa_led; + } + + if (Alertlevel[alert].fgg_led == HIGH) { + if (g_timer_flag) { + g_timer_flag = 0; // if it has, clear the flag + gg_led = !gg_led; + } + } else { + gg_led = Alertlevel[alert].sgg_led; + } +} + +void backlight () +{ + + if (bright == 1.0) { + bright = 0; + } else { + bright += 0.2; + } + lcd.setBrightness(bright); +} + +void sw2_isr() +{ + g_sw2_flag = 1; // set flag in ISR +} + +/*void sw1_isr() +{ + g_sw1_flag = 1; // set flag in ISR +}*/ + void timer_isr() { g_timer_flag = 1; // set flag in ISR +} + +void timer_isr_srf02() +{ + g_timer_flag_srf02 = 1; // set flag in ISR } \ No newline at end of file