![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A program designed to get the distance from an SRF02 distance sensor and create an audible and visual indication of that distance with data logging capabilities.
Dependencies: N5110 PowerControl SRF02 mbed
Diff: main.cpp
- Revision:
- 12:babcce84f9c8
- Parent:
- 11:41052065c916
- Child:
- 13:86520f8c3b72
--- a/main.cpp Fri May 01 16:47:33 2015 +0000 +++ b/main.cpp Fri May 08 08:58:45 2015 +0000 @@ -12,81 +12,81 @@ void welcomeScreen() { - display.init(); /// Initialise the display. - display.clear(); /// Clears the starting pattern from the screen. - display.printString("--++--++--++--",0,0); ///Print string of "" at x,y locations. + display.init(); // Initialise the display. + display.clear(); // Clears the starting pattern from the screen. + display.printString("--++--++--++--",0,0); //Print string of "" at x,y locations. display.printString("Distance",18,1); display.printString("Sensor",22,2); display.printString("--++--++--++--",0,3); display.printString("Sam Russell",10,4); display.printString("--++--++--++--",0,5); - wait(4); ///Delay between the introduction and the begining of the game. - display.clear(); ///Clears the display. + wait(4); //Delay between the introduction and the begining of the game. + display.clear(); //Clears the display. } void barChart() { int buzz=0; - display.drawRect(0,0,82,20,0); ///Draw initial bar graph border. + display.drawRect(0,0,82,20,0); //Draw initial bar graph border. if (distance>240) { - display.drawRect(72,2,8,16,1); ///Draw 8 Bars - if(mode==0 && buzz==0) { ///when mode is normal buzz is zero, play a note. - buzzer.period(1/349.23); ///Plays note 'Fa' - buzz=1; ///sets buzz to one, to prevent multiple notes playing. + display.drawRect(72,2,8,16,1); //Draw 8 Bars + if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. + buzzer.period(1/349.23); //Plays note 'Fa' + buzz=1; //sets buzz to one, to prevent multiple notes playing. } }if(distance>210) { - display.drawRect(62,2,8,16,1); ///Draw 7 Bars + display.drawRect(62,2,8,16,1); //Draw 7 Bars if(mode==0&&buzz==0) { - buzzer.period(1/329.63); ///Plays note 'Mi' - buzz=1; ///sets buzz to one, to prevent multiple notes playing. + buzzer.period(1/329.63); //Plays note 'Mi' + buzz=1; //sets buzz to one, to prevent multiple notes playing. } }if(distance>180) { - display.drawRect(52,2,8,16,1); ///Draw 6 Bars - if(mode==0 && buzz==0) { ///when mode is normal buzz is zero, play a note. - buzzer.period(1/329.63); ///Plays note 'Mi' - buzz=1; ///sets buzz to one, to prevent multiple notes playing. + display.drawRect(52,2,8,16,1); //Draw 6 Bars + if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. + buzzer.period(1/329.63); //Plays note 'Mi' + buzz=1; //sets buzz to one, to prevent multiple notes playing. } } if(distance>150) { - display.drawRect(42,2,8,16,1); ///Draw 5 Bars - if(mode==0 && buzz==0) { ///when mode is normal buzz is zero, play a note. - buzzer.period(1/293.66); ///Plays note 'Re' - buzz=1; ///sets buzz to one, to prevent multiple notes playing. + display.drawRect(42,2,8,16,1); //Draw 5 Bars + if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. + buzzer.period(1/293.66); //Plays note 'Re' + buzz=1; //sets buzz to one, to prevent multiple notes playing. } } if(distance>120) { - display.drawRect(32,2,8,16,1); ///Draw 4 Bars - if(mode==0 && buzz==0) { ///when mode is normal buzz is zero, play a note. - buzzer.period(1/293.66); ///Plays note 'Re' - buzz=1; ///sets buzz to one, to prevent multiple notes playing. + display.drawRect(32,2,8,16,1); //Draw 4 Bars + if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. + buzzer.period(1/293.66); //Plays note 'Re' + buzz=1; //sets buzz to one, to prevent multiple notes playing. } } if(distance>90) { - display.drawRect(22,2,8,16,1); ///Draw 3 Bars - if(mode==0 && buzz==0) { ///when mode is normal buzz is zero, play a note. - buzzer.period(1/261.63); ///Plays note 'Do' - buzz=1; ///sets buzz to one, to prevent multiple notes playing. + display.drawRect(22,2,8,16,1); //Draw 3 Bars + if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. + buzzer.period(1/261.63); //Plays note 'Do' + buzz=1; //sets buzz to one, to prevent multiple notes playing. } }if(distance>60) { - display.drawRect(12,2,8,16,1); ///Draw 2 Bars - if(mode==0 && buzz==0) { ///when mode is normal buzz is zero, play a note. - buzzer.period(1/261.63); ///Plays note 'Do' - buzz=1; ///sets buzz to one, to prevent multiple notes playing. + display.drawRect(12,2,8,16,1); //Draw 2 Bars + if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. + buzzer.period(1/261.63); //Plays note 'Do' + buzz=1; //sets buzz to one, to prevent multiple notes playing. } } if(distance>=30) { - display.drawRect(2,2,8,16,1); ///Draw 1 Bar - if(mode==0 && buzz==0) { ///when mode is normal buzz is zero, play a note. - buzzer.period(1/246.94); ///Plays note 'Si' - buzz=1; ///sets buzz to one, to prevent multiple notes playing. + display.drawRect(2,2,8,16,1); //Draw 1 Bar + if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. + buzzer.period(1/246.94); //Plays note 'Si' + buzz=1; //sets buzz to one, to prevent multiple notes playing. } } if(distance<30) { - if(mode==0 && buzz==0) { ///when mode is normal buzz is zero, play a note. - buzzer.period(1/246.94); ///Plays note 'Si' - buzz=1; ///sets buzz to one, to prevent multiple notes playing. + if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. + buzzer.period(1/246.94); //Plays note 'Si' + buzz=1; //sets buzz to one, to prevent multiple notes playing. } } } void error(int code) -///ERROR CODE +//ERROR CODE { while(1) { leds = 0; @@ -98,19 +98,19 @@ void setTime() { -/// print time for debugging +// print time for debugging pc.printf("set_time - %s",rxString); -/// atoi() converts a string to an integer +// atoi() converts a string to an integer int time = atoi(rxString); -/// update the time +// update the time set_time(time); } void serialISR() { -/// when a serial interrupt occurs, read rx string into buffer +// when a serial interrupt occurs, read rx string into buffer pc.gets(rxString,16); -/// set flag +// set flag setTimeFlag = 1; } @@ -118,26 +118,26 @@ { if (mode<2){ leds = 7; //logging indicator - logLED = 1; /// turn on LEDs for feedback + logLED = 1; // turn on LEDs for feedback } - pc.printf("Data Logged\n"); ///DEBUG MESSAGE - FILE *fp = fopen("/local/log.csv", "a"); /// open 'log.txt' for appending -/// if the file doesn't exist it is created, if it exists, data is appended to the end - fprintf(fp,"%s %d\n",data,dataDistance); /// print string to file - fclose(fp); /// close file + pc.printf("Data Logged\n"); //DEBUG MESSAGE + FILE *fp = fopen("/local/log.csv", "a"); // open 'log.txt' for appending +// if the file doesn't exist it is created, if it exists, data is appended to the end + fprintf(fp,"%s %d\n",data,dataDistance); // print string to file + fclose(fp); // close file if (mode<2) { leds = 1; } } -///When timer expires set flag to equal 1. +//When timer expires set flag to equal 1. void timerExpired() { timerflag = 1; } float avgDist() -///After 10 readings calculate the average and define that as the total distance. +//After 10 readings calculate the average and define that as the total distance. { float d1 = SRF02.getDistanceCm(); float d2 = SRF02.getDistanceCm(); @@ -156,69 +156,69 @@ void buttonPressed() { wait(0.2); - mode++; ///count up mode when button is pressed. + mode++; //count up mode when button is pressed. leds = 15; } int main() { - PHY_PowerDown(); ///Powers down the ethernet. - pc.baud(9600); ///setting the baud rate - timer.attach(&timerExpired,1); ///every one second runs timer expired - welcomeScreen(); /// Displays the welcome screen. - pc.attach(&serialISR); /// attach serial ISR - char buffer[30]; /// buffer used to store time string - set_time(0); /// initialise time to 1st January 1970 + PHY_PowerDown(); ///1) Powers down the ethernet, function taken from the Ethernet Power Cotrnol library by Michael Wei. + pc.baud(9600); ///2) Sets the baud rate to 9600 (default) + timer.attach(&timerExpired,1); ///3) Configures & Initalises Timer + welcomeScreen(); ///4) Displays welcome screen. + pc.attach(&serialISR); ///5) Attaches PC so when an interupt occurs over the serial the function "serialISR" is executed. + char buffer[30]; ///6) Buffer created to store time string + set_time(0); ///7) Initialise time to 1st January 1970 - Button.rise(&buttonPressed); ///change mode when button is pressed. + Button.rise(&buttonPressed); ///8) Configures & Initalises button pressed function. while(1) { + ///9) When timer expires and the timerflag boolean value is true, run the following. if(timerflag) { - leds = 1; - timerflag = 0; // Reset timer flag. - distance = avgDist(); ///Set the distance variable to the averaged value from the sensor. - time_t seconds = time(NULL); /// get current time - /// format time into a string (time and date) - strftime(buffer, 30 , "%R %d/%m/%y", localtime(&seconds)); - /// print over serial - pc.printf("%s , %d cm\n",buffer,distance); ///print the distance value and the date/time to the serial. - display.clear(); ///Clear the display, updating bar graph. + leds = 1; ///10) sets LED indication. + timerflag = 0; ///11) Reset timer flag. + distance = avgDist(); ///12) Set the distance variable to the averaged value from the sensor. + time_t seconds = time(NULL); ///13) Get current time + strftime(buffer, 30 , "%R %d/%m/%y", localtime(&seconds)); ///14) Format time into a string (time and date) + pc.printf("%s , %d cm\n",buffer,distance); ///15) Print the distance value and the date/time to the serial. + display.clear(); ///16) Clear the display, updating bar graph. + ///17) Checks which mode the device is in and prints the mode name to the screen and executes the required actions of that mode. if(mode==0) { - display.printString("Normal Mode",8,5); ///Print mode name to screen. - buzzer =0.5; + display.printString("Normal Mode",8,5); //Print mode name to screen. + buzzer =0.5; //turns buzzer on } else if(mode==1) { - display.printString("Quiet Mode",10,5); ///Print mode name to screen. - buzzer =0; + display.printString("Quiet Mode",10,5); //Print mode name to screen. + buzzer =0; //turns buzzer off } else if(mode==2) { - display.printString("Power Saving",8,5); ///Print mode name to screen. + display.printString("Power Saving",8,5); //Print mode name to screen. backlight = 0; //sets the backlight off - Sleep(); ///mbed power saving sleep mode. - leds = 0; + Sleep(); //mbed power saving sleep mode. + leds = 0; //led indication OFF } else if(mode==3) { - ///when mode becomes an integer value of 3, reset mode. + ///17b) when mode becomes an integer value of 3, reset mode and backlight. backlight =1; //sets the backlight on - mode=0; - leds = 0; + mode=0; // resets mode flag. + leds = 0; //LED indication reset } - char buffer2[50]; /// buffer2 is used to store the distance string. - sprintf (buffer2, "%d cm", distance); ///Composes a printf compatable string and stores the distance in the buffer2. - display.printString(buffer2,25,3); ///prints the string stored in buffer2 - float L = Switch.read(); ///check switch value + char buffer2[50]; ///18) Buffer2 is used to store the distance string. + sprintf (buffer2, "%d cm", distance); ///19) Composes a printf compatable string and stores the distance in the buffer2. + display.printString(buffer2,25,3); ///20) Prints the string stored in buffer2 + float L = Switch.read(); ///21) Checks switch value/position. if(L>0.9) { - ///When switch is on, save the data to file and turn indicator on. + ///22) When switch is on, save the data to file and turn indicator on. writeDataToFile(buffer,distance); - pc.printf("Logging Data\n"); ///DEBUG MESSAGE + pc.printf("Logging Data\n"); //DEBUG MESSAGE } else { - ///When switch is off, don't save the data to file and turn indictor off. + ///22b) When switch is off, don't save the data to file and turn indictor off. logLED = 0; pc.printf("Not Logging Data\n"); } - barChart(); ///draw bar chart indicator. + barChart(); ///23) Function which creates and updates the visual and audible indication of the distance. } - if (setTimeFlag) { /// if updated time has been sent - setTimeFlag = 0; /// clear flag - setTime(); /// update time + if (setTimeFlag) { ///24) If setTimeFlag is TRUE then set the time and reset the flag. + setTimeFlag = 0; // clear flag + setTime(); // update time } - sleep(); ///mbed power saving sleep mode. + sleep(); ///25) Return the mbed to power saving sleep mode. } }