Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: N5110WN PowerControl SRF02 mbed
Diff: main.cpp
- Revision:
- 8:ffcde84b8cf3
- Parent:
- 7:962b5f044a9b
- Child:
- 9:163159830eaf
diff -r 962b5f044a9b -r ffcde84b8cf3 main.cpp
--- a/main.cpp Sun Apr 12 22:08:16 2015 +0000
+++ b/main.cpp Thu Apr 30 17:12:41 2015 +0000
@@ -8,7 +8,7 @@
int main()
{
PHY_PowerDown();
- semihost_powerdown();
+ //semihost_powerdown();
pc.baud(9600); ///sets baud rate
timer.attach(&timerExpired,0.2); /// Sets the speed of the timer and attaches a function to run
timer2.attach(&timer2Expired,1); /// Sets the speed of the timer and attaches a function to run
@@ -17,13 +17,11 @@
UnitTog.rise(&unitToggle); /// attaches a function to the rise of the UnitTog button
VisTog.rise(&visToggle); /// attaches a function to the rise of the VisTog button
pc.attach(&serialISR); /// attach serial ISR
- set_time(1427846400); /// initialise time to 1st April 2015 00:00:00
-
BLED.period(0.02); ///sets the frequency of the Backlight at 50Hz
BLED=BLEDLevel.read(); ///sets the brightness
- lcd.init(); ///to initilize the display
+ lcd.init(); ///initilizes the display
introTune(); /// plays the intro tune
- lcd.printString("---+--+--+---",3,0);
+ lcd.printString("---+--+--+---",3,0);/// Displays an introduction message
lcd.printString("Distance",18,1);
lcd.printString("Sensor",25,2);
lcd.printString("By Jakobi",16,3);
@@ -52,114 +50,105 @@
pc.printf("Scan Speed = 5 \n"); /// prints the scan speed across the serial port
theMain(); /// Contains the main code
}
- } else {
- error(1); //displays an error message across the mBed leds
+ } else { /// otherwise
+ error(1); /// displays an error message across the mBed leds
}
- Sleep();
+ Sleep();/// Whilst the code is not doing anything else, it will sleep
}
}
void timerExpired()
{
- timerFlag=1;
+ timerFlag=1; /// sets the timer flag high
}
void timer2Expired()
{
- timer2Flag=1;
+ timer2Flag=1;/// sets the timer flag high
}
void timer3Expired()
{
- timer3Flag=1;
+ timer3Flag=1;/// sets the timer flag high
}
void theMain()
{
BLED=BLEDLevel.read(); ///sets the brightness
- timerFlag =0; ///resets flags
+ timerFlag =0; ///resets the timer flags
timer2Flag=0;
timer3Flag=0;
- time_t seconds = time(NULL); /// get current time
- /// format time into a string (time and date)
+ time_t seconds = time(NULL); /// gets the current time
+ /// formats that time into a string (time and date)
strftime(buffer, 30 , "%d/%m/%y %R", localtime(&seconds));
float dist = getDistance()*unitX; ///reads the distance and sets it to a float value.
///prints the value to the serial port.
- if(state==1) {
- pc.printf("%s , Distance = %.2f %s \n",buffer,dist,units); ///prints the value to the serial port.
+ if(state==1) {
+ pc.printf("%s , Distance = %.2f %s \n",buffer,dist,units);
} else if(state !=1) {
- pc.printf("%s , Distance = %.0f %s \n",buffer,dist,units); ///prints the value to the serial port.
+ pc.printf("%s , Distance = %.0f %s \n",buffer,dist,units);
}
- logging(buffer,distance);
- if(visual==0) {
- LCDVis0();
- } else if(visual==1) {
- LCDVis1();
- } else if(visual==2) {
- LCDVis2();
- } else if(visual==3) {
- LCDVis3();
- } else {
- error(2);
+ logging(buffer,distance); /// runs the logging function
+ if(visual==0) { /// if the visual toggle is set in the first mode,
+ LCDVis0(); /// runs the first Visual function, Changing the Display
+ } else if(visual==1) {/// if the visual toggle is set in the second mode,
+ LCDVis1(); /// runs the second Visual function, Changing the Display
+ } else if(visual==2) {/// if the visual toggle is set in the third mode,
+ LCDVis2(); /// runs the third Visual function, Changing the Display
+ } else if(visual==3) {/// if the visual toggle is set in the forth mode,
+ LCDVis3(); /// runs the forth Visual function, Changing the Display
+ } else { /// if there is no Visual mode selected then
+ error(2); /// there is an error and so the error function runs
}
-
}
void warnings()
{
- WLED=1;
- volume= BuzVol.read();
- if(distance<20) {
- Buzzer.ContNote(1000.0, volume);
+ WLED=1;/// Warning LED is turned on
+ volume= BuzVol.read(); /// sets the volume of the buzzer
+ if(distance<20) { /// if the distance is less than 20 cm
+ Buzzer.ContNote(1000.0, volume); /// uses the new member to create a continuous note
+ ///(new member was written and added to speaker.h)
- } else {
- Buzzer.PlayNote(1000.0, 0.1, volume);
+ } else {/// if the distance isnt less than 20
+ Buzzer.PlayNote(1000.0, 0.1, volume);/// plays a note for 0.1 seconds
+ WLED=0;/// and turns off the warning LED (creating a beep and a flash)
}
- WLED=0;
}
+
float getDistance()
{
- int dist0 = sensor.getDistanceCm();
-
+ int dist0 = sensor.getDistanceCm(); /// gets the distance 10 times
int dist1 = sensor.getDistanceCm();
-
int dist2 = sensor.getDistanceCm();
-
int dist3 = sensor.getDistanceCm();
-
int dist4 = sensor.getDistanceCm();
-
int dist5 = sensor.getDistanceCm();
+ int dist6 = sensor.getDistanceCm();
+ int dist7 = sensor.getDistanceCm();
+ int dist8 = sensor.getDistanceCm();
+ int dist9 = sensor.getDistanceCm();
+ distance=(dist0+dist1+dist2+dist3+dist4+dist5+dist6+dist7+dist8+dist9)/10;/// averages those distances
- int dist6 = sensor.getDistanceCm();
-
- int dist7 = sensor.getDistanceCm();
-
- int dist8 = sensor.getDistanceCm();
-
- int dist9 = sensor.getDistanceCm();
-
- distance=(dist0+dist1+dist2+dist3+dist4+dist5+dist6+dist7+dist8+dist9)/10;
-
- warnings();
- return distance;
+ warnings(); /// runs the warnings function
+ return distance; /// returns the average distance
}
void unitToggle()
{
- state = fsm[state].nextState[UnitTog]; /// read input and update current state
- unitX = fsm[state].unitMultiple; /// set output depending on current state
- units= fsm[state].Unit; ///
+ state = fsm[state].nextState[UnitTog]; /// reads the input and updates the current state
+ unitX = fsm[state].unitMultiple; /// sets the unit multiple depending on the current state
+ units= fsm[state].Unit; /// sets the unit depending on the current state
wait(0.2);
pc.printf("unit multiple = %.2f \n",unitX);
pc.printf("Units = %s \n",units);
}
-void error(int E) /// up to 14. IF 15 shown it is a ack bit error for the SRF02
-{
- while(1) {
+void error(int E) /// has up to 14 possible errors.
+{ ///If a 15 is shown it is a ack bit error for the SRF02
+ while(1) { /// loops flashing the error number on the onboard LED's
leds=0;
wait(0.2);
leds=E;
@@ -169,41 +158,41 @@
void logToggle()
{
- logButtonFlag = !logButtonFlag; //flips the flag
- if(logButtonFlag) {
- LLED=1; // Logging LED will be on if the device is logging
- pc.printf("-------\n Data Logging\n\r");
- } else {
- LLED=0; // Logging LED will be off if the device isn't logging
- pc.printf("-------\n Stopped Logging Data\n\r");
+ logButtonFlag = !logButtonFlag; ///flips the flag
+ if(logButtonFlag) { /// if flag is high the device is logging so
+ LLED=1; /// Logging LED will be on
+ pc.printf("-------\n Data Logging\n\r");/// prints a message to the serial port
+ } else { /// if flag is low the device isn't logging so
+ LLED=0; /// Logging LED is turned off
+ pc.printf("-------\n Stopped Logging Data\n\r");/// prints a message to the serial port
}
wait(0.2);
}
void logging(char* data,float data1)
{
- if(logButtonFlag) {
- FILE *fp = fopen("/local/log.csv", "a"); /// open 'log.csv' for appending
+ if(logButtonFlag) { //if the logging flag is high
+ FILE *fp = fopen("/local/log.csv", "a"); /// opens 'log.csv' 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,data1); /// print string to file
fclose(fp); /// close file
- pc.printf("Data Logged\n");
+ pc.printf("Data Logged\n");/// prints a message to the serial port
LLED=1; /// keeps the led on if it is logging
- } else {
- pc.printf("WARNING : Unlogged Data\n");
- LLED=0;
+ } else {/// otherwise
+ pc.printf("WARNING : Unlogged Data\n");/// prints a message to the serial port
+ LLED=0; ///sets the logging LED to off
}
}
void setTime()
{
-/// print time for debugging
- pc.printf("set time - %s",rxString);
+/// prints Unix time for debugging
+ pc.printf("\n \r Unix set time - %s\n",rxString);
/// atoi() converts a string to an integer
int time = atoi(rxString);
/// update the time
set_time(time);
- pc.printf("set time - %d",time);
+ pc.printf("Set time - %s \n \r ",buffer);///prints the time to the serial port
}
void serialISR()
{
@@ -214,7 +203,7 @@
}
void introTune()
-{
+{/// plays several notes to create a tune
Buzzer.PlayNote(1319.0,0.084,1.0);
wait(0.01);
Buzzer.PlayNote(1319.0,0.084,1.0);
@@ -230,100 +219,90 @@
Buzzer.PlayNote(784.0,0.084,1.0);
}
-void checkerBoard() /// creates a checked pattern on the lcd
-{
- for(int i=0; i<84; i+=2) {
- for(int j=0; j<48; j+=2) {
- lcd.setPixel(i,j);
- }
- }
- lcd.refresh();
-}
-void LCDVis0() /// the max range is 249 (according to the data sheet) and there are 84 pixels wide on the LCD so this creates the amount of cm for each pixel
-{
- lcd.clear();
- if(logButtonFlag) {
- lcd.printString("-L-",0,5);
+void LCDVis0() /// the max range is 249 (according to the data sheet)
+{///and there are 84 pixels wide on the LCD so this can be used to map the amount of cm for each pixel
+ lcd.clear();/// clears the screen
+ if(logButtonFlag) { /// if the logging flag is high
+ lcd.printString("-L-",0,5); /// prints an L onto the botton left of the screen
}
int I = distance*0.337; /// and then multiplies the current distance by that value as (84/249) =0.337...
I=I+3;
if(I>=79){I=79;}
+ pc.printf("bar width - %d pixels \n",I);/// prints the width to the serial port
+ lcd.drawRect(0,16,83,16,0); ///draws a frame for the bar
+ lcd.drawRect(2,18,I,12,1); /// draws a rectangle inside the frame at the calculated width
+ if(state==1) {
+ sprintf(Dbuffer, "%.2f", distance*unitX); /// prints the value to the serial port.
+ } else if(state !=1) {
+ sprintf(Dbuffer, "%.0f", distance*unitX);
+ }
+ lcd.printString (buffer,0,0);/// prints the time and date at the top of the screen
+ lcd.printString (Dbuffer,26,5); /// prints the distance
+ lcd.printString (units,50,5); /// and the units
+ lcd.refresh();/// it then refreshes the screen
+}
+
+void LCDVis1() /// the max range is 249 (according to the data sheet)
+{///and there are 48 pixels high on the LCD so this can be used to map the amount of cm for each pixel
+ lcd.clear(); /// clears the screen
+ if(logButtonFlag) {/// if the logging flag is high
+ lcd.printString("-L-",0,5);/// prints an L onto the botton left of the screen
+ }
+
+ int J = distance*0.14; /// and then multiplies the current distance by that value as (36/249) = 0.14..(as top 12 pixels are for time)
+ if(J>=36) {
+ J=36;
+ }
+ pc.printf("bar height - %d pixels \n",J);// prints the height to the serial port
+ lcd.drawRect(24,9,34,38,0);///draws a frame for the bar
+ lcd.drawRect(26,47-J,30,J-2,1);/// draws a rectangle inside the frame at the calculated height
+ if(state==1) {
+ sprintf(Dbuffer, "%.2f", distance*unitX); /// prints the value to the serial port.
+ } else if(state !=1) {
+ sprintf(Dbuffer, "%.0f", distance*unitX);
+ }
+ lcd.printString (buffer,0,0);/// prints the time and date at the top of the screen
+ lcd.printString (Dbuffer,60,3);/// prints the distance
+ lcd.printString (units,64,4);/// and the units
+ lcd.refresh(); /// it then refreshes the screen
+}
+
+void LCDVis2()
+{
+ lcd.clear();/// clears the screen
+ if(logButtonFlag) {/// if the logging flag is high
+ lcd.printString("-L-",0,5);/// prints an L onto the botton left of the screen
+ }
+ car(); /// displays a small car shape
+ int I = distance*0.277; /// and then multiplies the current distance by that value as (69/249) =0.277...
+ if(I>=84) {
+ I=84;
+ }
pc.printf("bar width - %d pixels \n",I);
- lcd.drawRect(0,16,83,16,0);
- lcd.drawRect(2,18,I,12,1);
+ lcd.drawRect(I+15,9,69,29,1); /// draws a rectangle inside the frame at the calculated width
if(state==1) {
sprintf(Dbuffer, "%.2f", distance*unitX); /// prints the value to the serial port.
} else if(state !=1) {
sprintf(Dbuffer, "%.0f", distance*unitX); /// prints the value to the serial port.
}
- lcd.printString (buffer,0,0);
- lcd.printString (Dbuffer,26,5);
- lcd.printString (units,50,5);
- lcd.refresh();
-}
-
-void LCDVis1() /// the max range is 249 (according to the data sheet) and there are 48 pixels high on the LCD so this creates the amount of cm for each pixel
-{
- lcd.clear();
- if(logButtonFlag) {
- lcd.printString("-L-",0,5);
- }
-
- int J = distance*0.14; /// and then multiplies the current distance by that value as (36/249) = 0.19..(as top 12 pixels are for time)
- if(J>=36) {
- J=36;
- }
- pc.printf("bar height - %d pixels \n",J);
- lcd.drawRect(24,9,34,38,0);
- lcd.drawRect(26,47-J,30,J-2,1);
- if(state==1) {
- sprintf(Dbuffer, "%.2f", distance*unitX); /// prints the value to the serial port.
- } else if(state !=1) {
- sprintf(Dbuffer, "%.0f", distance*unitX); /// prints the value to the serial port.
- }
- lcd.printString (buffer,0,0);
- lcd.printString (Dbuffer,60,3);
- lcd.printString (units,64,4);
- lcd.refresh();
-}
-
-void LCDVis2()
-{
- lcd.clear();
- if(logButtonFlag) {
- lcd.printString("-L-",0,5);
- }
-
- car();
- int I = distance*0.277; /// and then multiplies the current distance by that value as (69/249) =0.277...
- if(I>=84) {
- I=84;
- }
- pc.printf("bar width - %d pixels \n",I);
- lcd.drawRect(I+15,9,69,29,1);
- if(state==1) {
- sprintf(Dbuffer, "%.2f", distance*unitX); /// prints the value to the serial port.
- } else if(state !=1) {
- sprintf(Dbuffer, "%.0f", distance*unitX); /// prints the value to the serial port.
- }
- lcd.printString (buffer,0,0);
- lcd.printString (Dbuffer,26,5);
- lcd.printString (units,50,5);
- lcd.refresh();
+ lcd.printString (buffer,0,0);/// prints the time and date at the top of the screen
+ lcd.printString (Dbuffer,26,5);///prints the distance
+ lcd.printString (units,50,5);///and the units
+ lcd.refresh(); /// it then refreshes the screen
}
void LCDVis3()
{
- lcd.clear();
- if(logButtonFlag) {
- lcd.printString("-L-",0,5);
+ lcd.clear(); /// clears the screen
+ if(logButtonFlag) {/// if the logging flag is high
+ lcd.printString("-L-",0,5);/// prints an L onto the botton left of the screen
}
lcd.printString (buffer,0,0);
- int Dist=distance;
- if(state==0) {
- lcd.printString("CM",54,3);
- }
+ int Dist=distance; /// assigns each digit of the distance
+ if(state==0) { /// to an integer and then depending on the value of that integer
+ lcd.printString("CM",54,3); /// prints a large number to a position on the screen
+ } /// (large numbers designed and added to the N5110 library)
if(state==1) {
lcd.decimal();
lcd.printString("M",54,3);
@@ -386,7 +365,7 @@
int H=Dist/100 %10;
pc.printf("Hundreds - %d \n",H);
if(H==0) {
-
+ if(state==1){ lcd.number0(0);}
} else if(H==1) {
lcd.number1(0);
} else if(H==2) {
@@ -417,7 +396,7 @@
void car()
-{
+{ /// sets pixels in the design of a car
lcd.setPixel(4,19);lcd.setPixel(5,19);lcd.setPixel(6,19);lcd.setPixel(10,19);lcd.setPixel(11,19);lcd.setPixel(12,19);
lcd.setPixel(4,20);lcd.setPixel(5,20);lcd.setPixel(6,20);lcd.setPixel(10,20);lcd.setPixel(11,20);lcd.setPixel(12,20);
lcd.setPixel(1,21);lcd.setPixel(2,21);lcd.setPixel(3,21);lcd.setPixel(4,21);lcd.setPixel(5,21);lcd.setPixel(6,21);
@@ -440,11 +419,12 @@
void visToggle()
{
+ lcd.init();
Vstate = Vfsm[Vstate].nextState[VisTog]; /// read input and update current state
visual = Vfsm[Vstate].visual; /// set output depending on current state
wait(0.2);
if(visual==0) {
- pc.printf("----Left to Right----\n\r");
+ pc.printf("----Left to Right----\n\r"); /// prints to the serial which mode is selected
}
if(visual==1) {
pc.printf("----Up to Down----\n\r");
@@ -456,7 +436,6 @@
pc.printf("----Numbers----\n\r");
}
}
-
int semihost_powerdown(){
uint32_t arg;
return __semihost(USR_POWERDOWN, &arg);