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:
- 2:ebcfecb68cca
- Parent:
- 1:387d5e6fa05f
- Child:
- 3:4e8fb4fb54df
--- a/main.cpp Wed Apr 08 18:41:31 2015 +0000 +++ b/main.cpp Wed Apr 08 20:10:26 2015 +0000 @@ -36,12 +36,12 @@ void writeDataToFile(char* data, float dataDistance) { - 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 + 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 %.2f\n",data,dataDistance); // print string to file - fclose(fp); // close file + fprintf(fp,"%s %.2f\n",data,dataDistance); // print string to file + fclose(fp); // close file } //When timer expires set flag to equal 1. @@ -80,6 +80,7 @@ int main() { pc.baud(9600); //setting the baud rate + Buzzer.period(0.001); timer.attach(&timerExpired,1); display.init(); // Initialise the display. display.clear(); // Clears the starting pattern from the screen. @@ -91,11 +92,10 @@ display.printString("--++--++--++--",0,5); wait(2); //Delay between the introduction and the begining of the game. display.clear(); //Clears the display. - display.drawRect(0,0,82,20,0); //Draw initial bar graph border. pc.attach(&serialISR); // attach serial ISR char buffer[30]; // buffer used to store time string set_time(0); // initialise time to 1st January 1970 - + while(1) { if(timerflag) { timerflag = 0; @@ -106,32 +106,79 @@ strftime(buffer, 30 , "%R %d/%m/%y", localtime(&seconds)); // print over serial pc.printf("%s , %.2f cm\n",buffer,distance); //print the temperature value and the date/time to the serial. + display.clear(); //Clear the display, updating bar graph. + 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(); - pc.printf("L = %.2f\n", L); if(L>0.9) { //When switch is on, save the data to file and turn indicator on. writeDataToFile(buffer,distance); pc.printf("Logging Data\n"); //DEBUG MESSAGE - } - else { - //When switch is off, don't save the data to file and turn indictor off. - logLED = 0; - pc.printf("Not Logging Data\n"); + } else { + //When switch is off, don't save the data to file and turn indictor off. + logLED = 0; + pc.printf("Not Logging Data\n"); } - - if(distance<75) { - leds = 15; + if(distance<30) { + display.drawRect(0,0,82,20,0); //Draw initial bar graph border. + } else if(distance<60) { + display.drawRect(0,0,82,20,0); //Draw initial bar graph border. + display.drawRect(2,2,8,16,1); + } else if(distance<90) { + display.drawRect(0,0,82,20,0); //Draw initial bar graph border. + display.drawRect(2,2,8,16,1); + display.drawRect(12,2,8,16,1); + } else if(distance<120) { + display.drawRect(0,0,82,20,0); //Draw initial bar graph border. + display.drawRect(2,2,8,16,1); + display.drawRect(12,2,8,16,1); + display.drawRect(22,2,8,16,1); } else if(distance<150) { - leds = 7; - } else if(distance<275) { - leds = 3; + display.drawRect(0,0,82,20,0); //Draw initial bar graph border. + display.drawRect(2,2,8,16,1); + display.drawRect(12,2,8,16,1); + display.drawRect(22,2,8,16,1); + display.drawRect(32,2,8,16,1); + } else if(distance<180) { + display.drawRect(0,0,82,20,0); //Draw initial bar graph border. + display.drawRect(2,2,8,16,1); + display.drawRect(12,2,8,16,1); + display.drawRect(22,2,8,16,1); + display.drawRect(32,2,8,16,1); + display.drawRect(42,2,8,16,1); + } else if(distance<210) { + display.drawRect(0,0,82,20,0); //Draw initial bar graph border. + display.drawRect(2,2,8,16,1); + display.drawRect(12,2,8,16,1); + display.drawRect(22,2,8,16,1); + display.drawRect(32,2,8,16,1); + display.drawRect(42,2,8,16,1); + display.drawRect(52,2,8,16,1); + } else if(distance<240) { + display.drawRect(0,0,82,20,0); //Draw initial bar graph border. + display.drawRect(2,2,8,16,1); + display.drawRect(12,2,8,16,1); + display.drawRect(22,2,8,16,1); + display.drawRect(32,2,8,16,1); + display.drawRect(42,2,8,16,1); + display.drawRect(52,2,8,16,1); + display.drawRect(62,2,8,16,1); } else { - leds = 1; + display.drawRect(0,0,82,20,0); //Draw initial bar graph border. + display.drawRect(2,2,8,16,1); + display.drawRect(12,2,8,16,1); + display.drawRect(22,2,8,16,1); + display.drawRect(32,2,8,16,1); + display.drawRect(42,2,8,16,1); + display.drawRect(52,2,8,16,1); + display.drawRect(62,2,8,16,1); + display.drawRect(72,2,8,16,1); } } if (setTimeFlag) { // if updated time has been sent - setTimeFlag = 0; // clear flag - setTime(); // update time + setTimeFlag = 0; // clear flag + setTime(); // update time } } }