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.h@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.h |
el13sr | 7:65bdd2d0d6ad | 3 | * @brief Distance Sensor Project |
el13sr | 7:65bdd2d0d6ad | 4 | * @brief Header file containing functions, definitions and global variables. |
el13sr | 14:acb6e07992bd | 5 | * @brief Version: 1.0b |
el13sr | 7:65bdd2d0d6ad | 6 | * @author Sam Russell |
el13sr | 7:65bdd2d0d6ad | 7 | * @date March 2015 |
el13sr | 7:65bdd2d0d6ad | 8 | */ |
el13sr | 7:65bdd2d0d6ad | 9 | |
el13sr | 12:babcce84f9c8 | 10 | /*********************************/ |
el13sr | 12:babcce84f9c8 | 11 | // * Libaries & Definitions * // |
el13sr | 12:babcce84f9c8 | 12 | /********************************/ |
el13sr | 0:7d01de89a1ff | 13 | |
el13sr | 0:7d01de89a1ff | 14 | #include "mbed.h" |
el13sr | 12:babcce84f9c8 | 15 | |
el13sr | 0:7d01de89a1ff | 16 | #include "SRF02.h" |
el13sr | 12:babcce84f9c8 | 17 | // @brief Power Control Library that simplifies interacting with the SRF02 distance sensor. |
el13sr | 12:babcce84f9c8 | 18 | // @author Craig.A.Evans |
el13sr | 12:babcce84f9c8 | 19 | |
el13sr | 12:babcce84f9c8 | 20 | |
el13sr | 0:7d01de89a1ff | 21 | #include "N5110.h" |
el13sr | 12:babcce84f9c8 | 22 | // @brief N5110 Display Library that simplifies and initalizes interactions with Nokia N5110 display. |
el13sr | 12:babcce84f9c8 | 23 | // @author Craig.A.Evans |
el13sr | 12:babcce84f9c8 | 24 | |
el13sr | 6:d6afc4026a1d | 25 | #include "PowerControl/PowerControl.h" |
el13sr | 12:babcce84f9c8 | 26 | // @brief Power Control Library that allows for the mbed to be put into different power saving modes. |
el13sr | 12:babcce84f9c8 | 27 | // @author Michael.Wei |
el13sr | 12:babcce84f9c8 | 28 | |
el13sr | 12:babcce84f9c8 | 29 | |
el13sr | 6:d6afc4026a1d | 30 | #include "PowerControl/EthernetPowerControl.h" |
el13sr | 12:babcce84f9c8 | 31 | // @brief Ethernet Power Control Library that allows for the mbed ethernet to be powered down. |
el13sr | 12:babcce84f9c8 | 32 | // @author Michael.Wei |
el13sr | 12:babcce84f9c8 | 33 | |
el13sr | 12:babcce84f9c8 | 34 | /** |
el13sr | 12:babcce84f9c8 | 35 | * @namespace Local |
el13sr | 12:babcce84f9c8 | 36 | * @brief Creates a local file system on the mbed flash memory. |
el13sr | 12:babcce84f9c8 | 37 | */ |
el13sr | 12:babcce84f9c8 | 38 | LocalFileSystem local("local"); |
el13sr | 12:babcce84f9c8 | 39 | |
el13sr | 12:babcce84f9c8 | 40 | /** |
el13sr | 12:babcce84f9c8 | 41 | * @namespace Timer |
el13sr | 12:babcce84f9c8 | 42 | * @brief Configures the ticker and defines the ticker as timer. |
el13sr | 12:babcce84f9c8 | 43 | */ |
el13sr | 12:babcce84f9c8 | 44 | Ticker timer; |
el13sr | 0:7d01de89a1ff | 45 | |
el13sr | 10:b61013738793 | 46 | /** |
el13sr | 10:b61013738793 | 47 | * @namespace PC |
el13sr | 12:babcce84f9c8 | 48 | * @brief Defines the serial which is used to transmit/recieve data to/from the PC via USB. |
el13sr | 10:b61013738793 | 49 | */ |
el13sr | 10:b61013738793 | 50 | Serial pc(USBTX,USBRX); |
el13sr | 10:b61013738793 | 51 | |
el13sr | 12:babcce84f9c8 | 52 | /********************/ |
el13sr | 12:babcce84f9c8 | 53 | // * Inputs * // |
el13sr | 12:babcce84f9c8 | 54 | /********************/ |
el13sr | 12:babcce84f9c8 | 55 | |
el13sr | 12:babcce84f9c8 | 56 | SRF02 SRF02(p28,p27); |
el13sr | 7:65bdd2d0d6ad | 57 | /** |
el13sr | 7:65bdd2d0d6ad | 58 | * @namespace SRF02 |
el13sr | 7:65bdd2d0d6ad | 59 | * @brief Data (SDA) and clock (SCL) for the sensor. |
el13sr | 7:65bdd2d0d6ad | 60 | */ |
el13sr | 12:babcce84f9c8 | 61 | |
el13sr | 12:babcce84f9c8 | 62 | AnalogIn Switch(p16); |
el13sr | 7:65bdd2d0d6ad | 63 | /** |
el13sr | 7:65bdd2d0d6ad | 64 | * @namespace Switch |
el13sr | 7:65bdd2d0d6ad | 65 | * @brief On/Off (BOOLEAN) states, OFF: Does not log data to file ON: Dos Log Data to file. |
el13sr | 7:65bdd2d0d6ad | 66 | */ |
el13sr | 12:babcce84f9c8 | 67 | |
el13sr | 12:babcce84f9c8 | 68 | InterruptIn Button(p17); |
el13sr | 7:65bdd2d0d6ad | 69 | /** |
el13sr | 7:65bdd2d0d6ad | 70 | * @namespace Button |
el13sr | 7:65bdd2d0d6ad | 71 | * @brief Three states, Toggles between Normal, Quiet and Power Saving modes. |
el13sr | 7:65bdd2d0d6ad | 72 | */ |
el13sr | 0:7d01de89a1ff | 73 | |
el13sr | 12:babcce84f9c8 | 74 | /********************/ |
el13sr | 12:babcce84f9c8 | 75 | // * Outputs * // |
el13sr | 12:babcce84f9c8 | 76 | /********************/ |
el13sr | 12:babcce84f9c8 | 77 | |
el13sr | 12:babcce84f9c8 | 78 | AnalogOut logLED(p18); |
el13sr | 7:65bdd2d0d6ad | 79 | /** |
el13sr | 10:b61013738793 | 80 | * @namespace logLED |
el13sr | 7:65bdd2d0d6ad | 81 | * @brief Indicates if the data recorded is being logged to file or not. |
el13sr | 7:65bdd2d0d6ad | 82 | */ |
el13sr | 12:babcce84f9c8 | 83 | |
el13sr | 12:babcce84f9c8 | 84 | // vcc,sce,rst,dc,mosi,clk,led |
el13sr | 12:babcce84f9c8 | 85 | N5110 display(p7,p8,p9,p10,p11,p13,p26); |
el13sr | 7:65bdd2d0d6ad | 86 | /** |
el13sr | 7:65bdd2d0d6ad | 87 | * @namespace Display |
el13sr | 7:65bdd2d0d6ad | 88 | * @brief Defines the Nokia N5110 as the output display and all the corresponding pins. |
el13sr | 7:65bdd2d0d6ad | 89 | */ |
el13sr | 12:babcce84f9c8 | 90 | |
el13sr | 12:babcce84f9c8 | 91 | PwmOut buzzer(p21); |
el13sr | 7:65bdd2d0d6ad | 92 | /** |
el13sr | 7:65bdd2d0d6ad | 93 | * @namespace Buzzer |
el13sr | 7:65bdd2d0d6ad | 94 | * @brief defines the buzzer pulse width modulation output. |
el13sr | 7:65bdd2d0d6ad | 95 | */ |
el13sr | 12:babcce84f9c8 | 96 | |
el13sr | 12:babcce84f9c8 | 97 | PwmOut backlight(p26); |
el13sr | 7:65bdd2d0d6ad | 98 | /** |
el13sr | 7:65bdd2d0d6ad | 99 | * @namespace Baclight |
el13sr | 7:65bdd2d0d6ad | 100 | * @brief defines the baclight of the display pulse width modulation output. |
el13sr | 7:65bdd2d0d6ad | 101 | */ |
el13sr | 12:babcce84f9c8 | 102 | |
el13sr | 12:babcce84f9c8 | 103 | BusOut leds (LED1,LED2,LED3,LED4); |
el13sr | 7:65bdd2d0d6ad | 104 | /** |
el13sr | 7:65bdd2d0d6ad | 105 | * @namespace LEDs |
el13sr | 7:65bdd2d0d6ad | 106 | * @brief Defines the set of on board LEDs. |
el13sr | 7:65bdd2d0d6ad | 107 | */ |
el13sr | 12:babcce84f9c8 | 108 | |
el13sr | 12:babcce84f9c8 | 109 | /***********************************/ |
el13sr | 12:babcce84f9c8 | 110 | // * Flags/Variables/Buffers * // |
el13sr | 12:babcce84f9c8 | 111 | /***********************************/ |
el13sr | 12:babcce84f9c8 | 112 | |
el13sr | 12:babcce84f9c8 | 113 | int timerflag = 0; /*!<BOOLEAN: initialise timer flag to equal 0, when this flag expires run void timerExpired(). . */ |
el13sr | 12:babcce84f9c8 | 114 | int distance = 0; /*!<Initialise distance integer value to 0. */ |
el13sr | 12:babcce84f9c8 | 115 | int buttonFlag = 0; /*!<BOOLEAN: initialise buttonFlag to be 0, when this flag is toggled run void buttonPressed(). */ |
el13sr | 12:babcce84f9c8 | 116 | int setTimeFlag = 0; /*!<BOOLEAN: initialise setTimeFlag to equal 0, when this flag is toggled run void setTime(). */ |
el13sr | 12:babcce84f9c8 | 117 | int mode = 1; /*!<Initialise mode to equal 0 (normal mode), this value will define which mode the device is running.. */ |
el13sr | 12:babcce84f9c8 | 118 | char rxString[16]; /*!< Buffer to store the recieved string. */ |
el13sr | 12:babcce84f9c8 | 119 | char buffer[30]; /*!< Buffer to store received UNIX time string */ |
el13sr | 12:babcce84f9c8 | 120 | char buffer2[50]; /*!< Buffer to store the converted (float to string) distance value so it can be printed to the display. */ |
el13sr | 12:babcce84f9c8 | 121 | |
el13sr | 12:babcce84f9c8 | 122 | |
el13sr | 12:babcce84f9c8 | 123 | /********************/ |
el13sr | 12:babcce84f9c8 | 124 | // * Funcitons * // |
el13sr | 12:babcce84f9c8 | 125 | /********************/ |
el13sr | 0:7d01de89a1ff | 126 | |
el13sr | 12:babcce84f9c8 | 127 | /** |
el13sr | 12:babcce84f9c8 | 128 | * @brief Creates the void which takes the two types of data into the function. |
el13sr | 12:babcce84f9c8 | 129 | * @author Craig.A.Evans |
el13sr | 12:babcce84f9c8 | 130 | */ |
el13sr | 12:babcce84f9c8 | 131 | void writeDataToFile(char* data, float Distancedata); |
el13sr | 12:babcce84f9c8 | 132 | |
el13sr | 12:babcce84f9c8 | 133 | /** |
el13sr | 12:babcce84f9c8 | 134 | * @brief Function that generates and clears the welcome screen, using the N5110 library designed by Craig.A.Evans |
el13sr | 12:babcce84f9c8 | 135 | */ |
el13sr | 12:babcce84f9c8 | 136 | void welcomeScreen(); |
el13sr | 12:babcce84f9c8 | 137 | |
el13sr | 12:babcce84f9c8 | 138 | /** |
el13sr | 12:babcce84f9c8 | 139 | * @brief Function which creates and updates the visual and audible indication of the distance. |
el13sr | 12:babcce84f9c8 | 140 | * @param int buzz |
el13sr | 12:babcce84f9c8 | 141 | * @param int mode |
el13sr | 12:babcce84f9c8 | 142 | * @param float distance |
el13sr | 12:babcce84f9c8 | 143 | */ |
el13sr | 12:babcce84f9c8 | 144 | void barChart(); |
el13sr | 0:7d01de89a1ff | 145 | |
el13sr | 12:babcce84f9c8 | 146 | /** |
el13sr | 12:babcce84f9c8 | 147 | * @brief DEBUG CODE: Function which when an error is found, all the on board LED's willl flash. |
el13sr | 12:babcce84f9c8 | 148 | */ |
el13sr | 12:babcce84f9c8 | 149 | void error(int code); |
el13sr | 12:babcce84f9c8 | 150 | |
el13sr | 12:babcce84f9c8 | 151 | /** |
el13sr | 12:babcce84f9c8 | 152 | * @brief Function that sets the time when the setTimeFlag is high. |
el13sr | 12:babcce84f9c8 | 153 | */ |
el13sr | 12:babcce84f9c8 | 154 | void setTime(); |
el13sr | 12:babcce84f9c8 | 155 | |
el13sr | 12:babcce84f9c8 | 156 | /** |
el13sr | 12:babcce84f9c8 | 157 | * @brief ISR (Interupt Service Routine): Interupts when a time is set over the serial and prepares to set that time by setting the setTimeFlag high. |
el13sr | 12:babcce84f9c8 | 158 | */ |
el13sr | 12:babcce84f9c8 | 159 | void serialISR(); |
el13sr | 12:babcce84f9c8 | 160 | |
el13sr | 12:babcce84f9c8 | 161 | /** |
el13sr | 12:babcce84f9c8 | 162 | * @Brief When the timer expires this funciton will set the timerFlag high indicating to the main function that a specified amount of time has passed (1 second). |
el13sr | 12:babcce84f9c8 | 163 | */ |
el13sr | 12:babcce84f9c8 | 164 | void timerExpired(); |
el13sr | 0:7d01de89a1ff | 165 | |
el13sr | 12:babcce84f9c8 | 166 | /** |
el13sr | 12:babcce84f9c8 | 167 | * @brief Function that gets 10 distance values from the sensor and averages them to get an accurate distance reading ready to use, using the SRF02 library designed by Craig.A.Evans |
el13sr | 12:babcce84f9c8 | 168 | * @param float d1 |
el13sr | 12:babcce84f9c8 | 169 | * @param float d2 |
el13sr | 12:babcce84f9c8 | 170 | * @param float d3 |
el13sr | 12:babcce84f9c8 | 171 | * @param float d4 |
el13sr | 12:babcce84f9c8 | 172 | * @param float d5 |
el13sr | 12:babcce84f9c8 | 173 | * @param float d6 |
el13sr | 12:babcce84f9c8 | 174 | * @param float d7 |
el13sr | 12:babcce84f9c8 | 175 | * @param float d8 |
el13sr | 12:babcce84f9c8 | 176 | * @param float d9 |
el13sr | 12:babcce84f9c8 | 177 | * @param float d10 |
el13sr | 12:babcce84f9c8 | 178 | * @return float distance |
el13sr | 12:babcce84f9c8 | 179 | */ |
el13sr | 12:babcce84f9c8 | 180 | float avgDist(); |
el13sr | 4:0bd5c189f2f3 | 181 | |
el13sr | 12:babcce84f9c8 | 182 | /** |
el13sr | 12:babcce84f9c8 | 183 | * @brief ISR (Interupt Service Routine): Whenever the button is pressed this funciton is executed which toggles the mode integer value, allowing the user to cycle through modes. |
el13sr | 12:babcce84f9c8 | 184 | */ |
el13sr | 12:babcce84f9c8 | 185 | void buttonPressed(); |