![](/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
main.cpp@15:619c711e9b53, 2015-05-08 (annotated)
- Committer:
- el13sr
- Date:
- Fri May 08 10:58:40 2015 +0000
- Revision:
- 15:619c711e9b53
- Parent:
- 14:acb6e07992bd
1.0c - Fixed an error where the time once set over the serial would reset due to an initial condition within the main function.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el13sr | 7:65bdd2d0d6ad | 1 | /** |
el13sr | 7:65bdd2d0d6ad | 2 | * @file main.cpp |
el13sr | 7:65bdd2d0d6ad | 3 | * @brief Distance Sensor Project |
el13sr | 7:65bdd2d0d6ad | 4 | * @brief Main file containing all of the functions and the int main(). |
el13sr | 15:619c711e9b53 | 5 | * @brief Version: 1.0c |
el13sr | 7:65bdd2d0d6ad | 6 | * @author Sam Russell |
el13sr | 7:65bdd2d0d6ad | 7 | * @date March 2015 |
el13sr | 7:65bdd2d0d6ad | 8 | */ |
el13sr | 0:7d01de89a1ff | 9 | |
el13sr | 1:387d5e6fa05f | 10 | #include "main.h" |
el13sr | 0:7d01de89a1ff | 11 | |
el13sr | 7:65bdd2d0d6ad | 12 | |
el13sr | 4:0bd5c189f2f3 | 13 | void welcomeScreen() |
el13sr | 4:0bd5c189f2f3 | 14 | { |
el13sr | 12:babcce84f9c8 | 15 | display.init(); // Initialise the display. |
el13sr | 12:babcce84f9c8 | 16 | display.clear(); // Clears the starting pattern from the screen. |
el13sr | 12:babcce84f9c8 | 17 | display.printString("--++--++--++--",0,0); //Print string of "" at x,y locations. |
el13sr | 4:0bd5c189f2f3 | 18 | display.printString("Distance",18,1); |
el13sr | 4:0bd5c189f2f3 | 19 | display.printString("Sensor",22,2); |
el13sr | 4:0bd5c189f2f3 | 20 | display.printString("--++--++--++--",0,3); |
el13sr | 4:0bd5c189f2f3 | 21 | display.printString("Sam Russell",10,4); |
el13sr | 4:0bd5c189f2f3 | 22 | display.printString("--++--++--++--",0,5); |
el13sr | 12:babcce84f9c8 | 23 | wait(4); //Delay between the introduction and the begining of the game. |
el13sr | 12:babcce84f9c8 | 24 | display.clear(); //Clears the display. |
el13sr | 4:0bd5c189f2f3 | 25 | } |
el13sr | 4:0bd5c189f2f3 | 26 | |
el13sr | 4:0bd5c189f2f3 | 27 | void barChart() |
el13sr | 4:0bd5c189f2f3 | 28 | { |
el13sr | 7:65bdd2d0d6ad | 29 | int buzz=0; |
el13sr | 12:babcce84f9c8 | 30 | display.drawRect(0,0,82,20,0); //Draw initial bar graph border. |
el13sr | 7:65bdd2d0d6ad | 31 | if (distance>240) { |
el13sr | 12:babcce84f9c8 | 32 | display.drawRect(72,2,8,16,1); //Draw 8 Bars |
el13sr | 12:babcce84f9c8 | 33 | if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. |
el13sr | 12:babcce84f9c8 | 34 | buzzer.period(1/349.23); //Plays note 'Fa' |
el13sr | 12:babcce84f9c8 | 35 | buzz=1; //sets buzz to one, to prevent multiple notes playing. |
el13sr | 6:d6afc4026a1d | 36 | } |
el13sr | 7:65bdd2d0d6ad | 37 | }if(distance>210) { |
el13sr | 12:babcce84f9c8 | 38 | display.drawRect(62,2,8,16,1); //Draw 7 Bars |
el13sr | 7:65bdd2d0d6ad | 39 | if(mode==0&&buzz==0) { |
el13sr | 12:babcce84f9c8 | 40 | buzzer.period(1/329.63); //Plays note 'Mi' |
el13sr | 12:babcce84f9c8 | 41 | buzz=1; //sets buzz to one, to prevent multiple notes playing. |
el13sr | 6:d6afc4026a1d | 42 | } |
el13sr | 7:65bdd2d0d6ad | 43 | }if(distance>180) { |
el13sr | 12:babcce84f9c8 | 44 | display.drawRect(52,2,8,16,1); //Draw 6 Bars |
el13sr | 12:babcce84f9c8 | 45 | if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. |
el13sr | 12:babcce84f9c8 | 46 | buzzer.period(1/329.63); //Plays note 'Mi' |
el13sr | 12:babcce84f9c8 | 47 | buzz=1; //sets buzz to one, to prevent multiple notes playing. |
el13sr | 6:d6afc4026a1d | 48 | } |
el13sr | 7:65bdd2d0d6ad | 49 | } if(distance>150) { |
el13sr | 12:babcce84f9c8 | 50 | display.drawRect(42,2,8,16,1); //Draw 5 Bars |
el13sr | 12:babcce84f9c8 | 51 | if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. |
el13sr | 12:babcce84f9c8 | 52 | buzzer.period(1/293.66); //Plays note 'Re' |
el13sr | 12:babcce84f9c8 | 53 | buzz=1; //sets buzz to one, to prevent multiple notes playing. |
el13sr | 6:d6afc4026a1d | 54 | } |
el13sr | 7:65bdd2d0d6ad | 55 | } if(distance>120) { |
el13sr | 12:babcce84f9c8 | 56 | display.drawRect(32,2,8,16,1); //Draw 4 Bars |
el13sr | 12:babcce84f9c8 | 57 | if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. |
el13sr | 12:babcce84f9c8 | 58 | buzzer.period(1/293.66); //Plays note 'Re' |
el13sr | 12:babcce84f9c8 | 59 | buzz=1; //sets buzz to one, to prevent multiple notes playing. |
el13sr | 6:d6afc4026a1d | 60 | } |
el13sr | 7:65bdd2d0d6ad | 61 | } if(distance>90) { |
el13sr | 12:babcce84f9c8 | 62 | display.drawRect(22,2,8,16,1); //Draw 3 Bars |
el13sr | 12:babcce84f9c8 | 63 | if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. |
el13sr | 12:babcce84f9c8 | 64 | buzzer.period(1/261.63); //Plays note 'Do' |
el13sr | 12:babcce84f9c8 | 65 | buzz=1; //sets buzz to one, to prevent multiple notes playing. |
el13sr | 6:d6afc4026a1d | 66 | } |
el13sr | 7:65bdd2d0d6ad | 67 | }if(distance>60) { |
el13sr | 12:babcce84f9c8 | 68 | display.drawRect(12,2,8,16,1); //Draw 2 Bars |
el13sr | 12:babcce84f9c8 | 69 | if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. |
el13sr | 12:babcce84f9c8 | 70 | buzzer.period(1/261.63); //Plays note 'Do' |
el13sr | 12:babcce84f9c8 | 71 | buzz=1; //sets buzz to one, to prevent multiple notes playing. |
el13sr | 6:d6afc4026a1d | 72 | } |
el13sr | 10:b61013738793 | 73 | } if(distance>=30) { |
el13sr | 12:babcce84f9c8 | 74 | display.drawRect(2,2,8,16,1); //Draw 1 Bar |
el13sr | 12:babcce84f9c8 | 75 | if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. |
el13sr | 12:babcce84f9c8 | 76 | buzzer.period(1/246.94); //Plays note 'Si' |
el13sr | 12:babcce84f9c8 | 77 | buzz=1; //sets buzz to one, to prevent multiple notes playing. |
el13sr | 6:d6afc4026a1d | 78 | } |
el13sr | 7:65bdd2d0d6ad | 79 | } |
el13sr | 7:65bdd2d0d6ad | 80 | if(distance<30) { |
el13sr | 12:babcce84f9c8 | 81 | if(mode==0 && buzz==0) { //when mode is normal buzz is zero, play a note. |
el13sr | 12:babcce84f9c8 | 82 | buzzer.period(1/246.94); //Plays note 'Si' |
el13sr | 12:babcce84f9c8 | 83 | buzz=1; //sets buzz to one, to prevent multiple notes playing. |
el13sr | 7:65bdd2d0d6ad | 84 | } |
el13sr | 7:65bdd2d0d6ad | 85 | } |
el13sr | 4:0bd5c189f2f3 | 86 | } |
el13sr | 4:0bd5c189f2f3 | 87 | |
el13sr | 0:7d01de89a1ff | 88 | void error(int code) |
el13sr | 12:babcce84f9c8 | 89 | //ERROR CODE |
el13sr | 0:7d01de89a1ff | 90 | { |
el13sr | 0:7d01de89a1ff | 91 | while(1) { |
el13sr | 0:7d01de89a1ff | 92 | leds = 0; |
el13sr | 0:7d01de89a1ff | 93 | wait(0.25); |
el13sr | 0:7d01de89a1ff | 94 | leds = code; |
el13sr | 0:7d01de89a1ff | 95 | wait(0.25); |
el13sr | 0:7d01de89a1ff | 96 | } |
el13sr | 0:7d01de89a1ff | 97 | } |
el13sr | 0:7d01de89a1ff | 98 | |
el13sr | 1:387d5e6fa05f | 99 | void setTime() |
el13sr | 1:387d5e6fa05f | 100 | { |
el13sr | 12:babcce84f9c8 | 101 | // print time for debugging |
el13sr | 1:387d5e6fa05f | 102 | pc.printf("set_time - %s",rxString); |
el13sr | 12:babcce84f9c8 | 103 | // atoi() converts a string to an integer |
el13sr | 1:387d5e6fa05f | 104 | int time = atoi(rxString); |
el13sr | 12:babcce84f9c8 | 105 | // update the time |
el13sr | 1:387d5e6fa05f | 106 | set_time(time); |
el13sr | 1:387d5e6fa05f | 107 | } |
el13sr | 1:387d5e6fa05f | 108 | |
el13sr | 1:387d5e6fa05f | 109 | void serialISR() |
el13sr | 1:387d5e6fa05f | 110 | { |
el13sr | 12:babcce84f9c8 | 111 | // when a serial interrupt occurs, read rx string into buffer |
el13sr | 1:387d5e6fa05f | 112 | pc.gets(rxString,16); |
el13sr | 12:babcce84f9c8 | 113 | // set flag |
el13sr | 1:387d5e6fa05f | 114 | setTimeFlag = 1; |
el13sr | 1:387d5e6fa05f | 115 | } |
el13sr | 1:387d5e6fa05f | 116 | |
el13sr | 3:4e8fb4fb54df | 117 | void writeDataToFile(char* data, int dataDistance) |
el13sr | 1:387d5e6fa05f | 118 | { |
el13sr | 11:41052065c916 | 119 | if (mode<2){ |
el13sr | 11:41052065c916 | 120 | leds = 7; //logging indicator |
el13sr | 12:babcce84f9c8 | 121 | logLED = 1; // turn on LEDs for feedback |
el13sr | 11:41052065c916 | 122 | } |
el13sr | 12:babcce84f9c8 | 123 | pc.printf("Data Logged\n"); //DEBUG MESSAGE |
el13sr | 12:babcce84f9c8 | 124 | FILE *fp = fopen("/local/log.csv", "a"); // open 'log.txt' for appending |
el13sr | 12:babcce84f9c8 | 125 | // if the file doesn't exist it is created, if it exists, data is appended to the end |
el13sr | 13:86520f8c3b72 | 126 | fprintf(fp,"%s,%d\n",data,dataDistance); // print string to file |
el13sr | 12:babcce84f9c8 | 127 | fclose(fp); // close file |
el13sr | 11:41052065c916 | 128 | if (mode<2) { |
el13sr | 11:41052065c916 | 129 | leds = 1; |
el13sr | 11:41052065c916 | 130 | } |
el13sr | 1:387d5e6fa05f | 131 | } |
el13sr | 1:387d5e6fa05f | 132 | |
el13sr | 12:babcce84f9c8 | 133 | //When timer expires set flag to equal 1. |
el13sr | 1:387d5e6fa05f | 134 | void timerExpired() |
el13sr | 1:387d5e6fa05f | 135 | { |
el13sr | 1:387d5e6fa05f | 136 | timerflag = 1; |
el13sr | 1:387d5e6fa05f | 137 | } |
el13sr | 1:387d5e6fa05f | 138 | |
el13sr | 0:7d01de89a1ff | 139 | float avgDist() |
el13sr | 12:babcce84f9c8 | 140 | //After 10 readings calculate the average and define that as the total distance. |
el13sr | 0:7d01de89a1ff | 141 | { |
el13sr | 0:7d01de89a1ff | 142 | float d1 = SRF02.getDistanceCm(); |
el13sr | 0:7d01de89a1ff | 143 | float d2 = SRF02.getDistanceCm(); |
el13sr | 0:7d01de89a1ff | 144 | float d3 = SRF02.getDistanceCm(); |
el13sr | 0:7d01de89a1ff | 145 | float d4 = SRF02.getDistanceCm(); |
el13sr | 0:7d01de89a1ff | 146 | float d5 = SRF02.getDistanceCm(); |
el13sr | 0:7d01de89a1ff | 147 | float d6 = SRF02.getDistanceCm(); |
el13sr | 0:7d01de89a1ff | 148 | float d7 = SRF02.getDistanceCm(); |
el13sr | 0:7d01de89a1ff | 149 | float d8 = SRF02.getDistanceCm(); |
el13sr | 0:7d01de89a1ff | 150 | float d9 = SRF02.getDistanceCm(); |
el13sr | 0:7d01de89a1ff | 151 | float d10 = SRF02.getDistanceCm(); |
el13sr | 0:7d01de89a1ff | 152 | float distance = (d1+d2+d3+d4+d5+d6+d7+d8+d9+d10)/10; |
el13sr | 0:7d01de89a1ff | 153 | return distance; |
el13sr | 0:7d01de89a1ff | 154 | } |
el13sr | 0:7d01de89a1ff | 155 | |
el13sr | 4:0bd5c189f2f3 | 156 | void buttonPressed() |
el13sr | 4:0bd5c189f2f3 | 157 | { |
el13sr | 5:c57cd4c57c40 | 158 | wait(0.2); |
el13sr | 12:babcce84f9c8 | 159 | mode++; //count up mode when button is pressed. |
el13sr | 11:41052065c916 | 160 | leds = 15; |
el13sr | 4:0bd5c189f2f3 | 161 | } |
el13sr | 4:0bd5c189f2f3 | 162 | |
el13sr | 1:387d5e6fa05f | 163 | int main() |
el13sr | 1:387d5e6fa05f | 164 | { |
el13sr | 12:babcce84f9c8 | 165 | PHY_PowerDown(); ///1) Powers down the ethernet, function taken from the Ethernet Power Cotrnol library by Michael Wei. |
el13sr | 12:babcce84f9c8 | 166 | pc.baud(9600); ///2) Sets the baud rate to 9600 (default) |
el13sr | 12:babcce84f9c8 | 167 | timer.attach(&timerExpired,1); ///3) Configures & Initalises Timer |
el13sr | 12:babcce84f9c8 | 168 | welcomeScreen(); ///4) Displays welcome screen. |
el13sr | 12:babcce84f9c8 | 169 | pc.attach(&serialISR); ///5) Attaches PC so when an interupt occurs over the serial the function "serialISR" is executed. |
el13sr | 12:babcce84f9c8 | 170 | char buffer[30]; ///6) Buffer created to store time string |
el13sr | 2:ebcfecb68cca | 171 | |
el13sr | 12:babcce84f9c8 | 172 | Button.rise(&buttonPressed); ///8) Configures & Initalises button pressed function. |
el13sr | 4:0bd5c189f2f3 | 173 | |
el13sr | 1:387d5e6fa05f | 174 | while(1) { |
el13sr | 12:babcce84f9c8 | 175 | ///9) When timer expires and the timerflag boolean value is true, run the following. |
el13sr | 1:387d5e6fa05f | 176 | if(timerflag) { |
el13sr | 12:babcce84f9c8 | 177 | leds = 1; ///10) sets LED indication. |
el13sr | 12:babcce84f9c8 | 178 | timerflag = 0; ///11) Reset timer flag. |
el13sr | 12:babcce84f9c8 | 179 | distance = avgDist(); ///12) Set the distance variable to the averaged value from the sensor. |
el13sr | 12:babcce84f9c8 | 180 | time_t seconds = time(NULL); ///13) Get current time |
el13sr | 12:babcce84f9c8 | 181 | strftime(buffer, 30 , "%R %d/%m/%y", localtime(&seconds)); ///14) Format time into a string (time and date) |
el13sr | 12:babcce84f9c8 | 182 | pc.printf("%s , %d cm\n",buffer,distance); ///15) Print the distance value and the date/time to the serial. |
el13sr | 12:babcce84f9c8 | 183 | display.clear(); ///16) Clear the display, updating bar graph. |
el13sr | 12:babcce84f9c8 | 184 | ///17) Checks which mode the device is in and prints the mode name to the screen and executes the required actions of that mode. |
el13sr | 4:0bd5c189f2f3 | 185 | if(mode==0) { |
el13sr | 12:babcce84f9c8 | 186 | display.printString("Normal Mode",8,5); //Print mode name to screen. |
el13sr | 12:babcce84f9c8 | 187 | buzzer =0.5; //turns buzzer on |
el13sr | 4:0bd5c189f2f3 | 188 | } else if(mode==1) { |
el13sr | 12:babcce84f9c8 | 189 | display.printString("Quiet Mode",10,5); //Print mode name to screen. |
el13sr | 12:babcce84f9c8 | 190 | buzzer =0; //turns buzzer off |
el13sr | 4:0bd5c189f2f3 | 191 | } else if(mode==2) { |
el13sr | 12:babcce84f9c8 | 192 | display.printString("Power Saving",8,5); //Print mode name to screen. |
el13sr | 11:41052065c916 | 193 | backlight = 0; //sets the backlight off |
el13sr | 12:babcce84f9c8 | 194 | Sleep(); //mbed power saving sleep mode. |
el13sr | 12:babcce84f9c8 | 195 | leds = 0; //led indication OFF |
el13sr | 5:c57cd4c57c40 | 196 | } else if(mode==3) { |
el13sr | 12:babcce84f9c8 | 197 | ///17b) when mode becomes an integer value of 3, reset mode and backlight. |
el13sr | 11:41052065c916 | 198 | backlight =1; //sets the backlight on |
el13sr | 12:babcce84f9c8 | 199 | mode=0; // resets mode flag. |
el13sr | 12:babcce84f9c8 | 200 | leds = 0; //LED indication reset |
el13sr | 4:0bd5c189f2f3 | 201 | } |
el13sr | 12:babcce84f9c8 | 202 | char buffer2[50]; ///18) Buffer2 is used to store the distance string. |
el13sr | 12:babcce84f9c8 | 203 | sprintf (buffer2, "%d cm", distance); ///19) Composes a printf compatable string and stores the distance in the buffer2. |
el13sr | 12:babcce84f9c8 | 204 | display.printString(buffer2,25,3); ///20) Prints the string stored in buffer2 |
el13sr | 12:babcce84f9c8 | 205 | float L = Switch.read(); ///21) Checks switch value/position. |
el13sr | 1:387d5e6fa05f | 206 | if(L>0.9) { |
el13sr | 12:babcce84f9c8 | 207 | ///22) When switch is on, save the data to file and turn indicator on. |
el13sr | 1:387d5e6fa05f | 208 | writeDataToFile(buffer,distance); |
el13sr | 12:babcce84f9c8 | 209 | pc.printf("Logging Data\n"); //DEBUG MESSAGE |
el13sr | 2:ebcfecb68cca | 210 | } else { |
el13sr | 12:babcce84f9c8 | 211 | ///22b) When switch is off, don't save the data to file and turn indictor off. |
el13sr | 2:ebcfecb68cca | 212 | logLED = 0; |
el13sr | 2:ebcfecb68cca | 213 | pc.printf("Not Logging Data\n"); |
el13sr | 1:387d5e6fa05f | 214 | } |
el13sr | 12:babcce84f9c8 | 215 | barChart(); ///23) Function which creates and updates the visual and audible indication of the distance. |
el13sr | 1:387d5e6fa05f | 216 | } |
el13sr | 12:babcce84f9c8 | 217 | if (setTimeFlag) { ///24) If setTimeFlag is TRUE then set the time and reset the flag. |
el13sr | 12:babcce84f9c8 | 218 | setTimeFlag = 0; // clear flag |
el13sr | 12:babcce84f9c8 | 219 | setTime(); // update time |
el13sr | 1:387d5e6fa05f | 220 | } |
el13sr | 12:babcce84f9c8 | 221 | sleep(); ///25) Return the mbed to power saving sleep mode. |
el13sr | 1:387d5e6fa05f | 222 | } |
el13sr | 1:387d5e6fa05f | 223 | } |