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
main.cpp
- Committer:
- el13sr
- Date:
- 2015-04-09
- Revision:
- 4:0bd5c189f2f3
- Parent:
- 3:4e8fb4fb54df
- Child:
- 5:c57cd4c57c40
File content as of revision 4:0bd5c189f2f3:
// Distance Sensor Project // Main File "main.cpp" // Designed By Sam Russell (200773195) // Date: 07/04/2015 || Version: 0.4a #include "main.h" void welcomeScreen() { 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(2); //Delay between the introduction and the begining of the game. display.clear(); //Clears the display. } void barChart() { 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); //Draw 1 Bar } 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); //Draw 2 Bars } 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); //Draw 3 Bars } else if(distance<150) { 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); //Draw 4 Bars } 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); //Draw 5 Bars } 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); //Draw 6 Bars } 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); //Draw 7 Bars } else { 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); //Draw 8 Bars } } void error(int code) //ERROR CODE { while(1) { leds = 0; wait(0.25); leds = code; wait(0.25); } } void setTime() { // print time for debugging pc.printf("set_time - %s",rxString); // atoi() converts a string to an integer int time = atoi(rxString); // update the time set_time(time); } void serialISR() { // when a serial interrupt occurs, read rx string into buffer pc.gets(rxString,16); // set flag setTimeFlag = 1; } void writeDataToFile(char* data, int 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 // 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 } //When timer expires set flag to equal 1. void timerExpired() { timerflag = 1; } float avgDist() //Read the distance from the sensor every 10ms. //After 10 readings calculate the average and define that as the total distance. { float d1 = SRF02.getDistanceCm(); wait(0.01); float d2 = SRF02.getDistanceCm(); wait(0.01); float d3 = SRF02.getDistanceCm(); wait(0.01); float d4 = SRF02.getDistanceCm(); wait(0.01); float d5 = SRF02.getDistanceCm(); wait(0.01); float d6 = SRF02.getDistanceCm(); wait(0.01); float d7 = SRF02.getDistanceCm(); wait(0.01); float d8 = SRF02.getDistanceCm(); wait(0.01); float d9 = SRF02.getDistanceCm(); wait(0.01); float d10 = SRF02.getDistanceCm(); float distance = (d1+d2+d3+d4+d5+d6+d7+d8+d9+d10)/10; return distance; } void buttonPressed() { mode++; //count up mode when button is pressed. if(mode>2) { //when mode becomes an integer value of 3, reset mode. mode=0; } } 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. welcomeScreen(); pc.attach(&serialISR); // attach serial ISR char buffer[30]; // buffer used to store time string set_time(0); // initialise time to 1st January 1970 Button.rise(&buttonPressed); //change mode when button is pressed. while(1) { if(timerflag) { timerflag = 0; ///Read sensor distance in cm and print to the serial port. 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 temperature value and the date/time to the serial. display.clear(); //Clear the display, updating bar graph. if(mode==0) { display.printString("Normal Mode",8,5); //Print mode name to screen. leds = 3; //mbed LED indication of mode. } else if(mode==1) { display.printString("Quiet Mode",10,5); //Print mode name to screen. leds = 1; //mbed LED indication of mode. } else if(mode==2) { display.printString("Power Saving",8,5); //Print mode name to screen. leds = 0; //mbed LED indication of mode. } 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 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"); } barChart(); //draw bar chart indicator. } if (setTimeFlag) { // if updated time has been sent setTimeFlag = 0; // clear flag setTime(); // update time } } }