Simple Car parking device
Dependencies: N5110 PinDetect PowerControl SRF02 mbed
main.cpp@0:969893638914, 2015-05-11 (annotated)
- Committer:
- el13eag
- Date:
- Mon May 11 18:33:03 2015 +0000
- Revision:
- 0:969893638914
revision 1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el13eag | 0:969893638914 | 1 | /** |
el13eag | 0:969893638914 | 2 | @file main.cpp |
el13eag | 0:969893638914 | 3 | @brief ELEC2645 Embedded Systems Project |
el13eag | 0:969893638914 | 4 | @brief Ultrasonic Distance sensor/Car Parker |
el13eag | 0:969893638914 | 5 | @brief Designed to help car owners park thier cars more effectively |
el13eag | 0:969893638914 | 6 | it basically uses burst of sound waves to measure the distance of objects |
el13eag | 0:969893638914 | 7 | from the device |
el13eag | 0:969893638914 | 8 | @brief Revision 1.0 |
el13eag | 0:969893638914 | 9 | @author Adolphus George Emmanuel |
el13eag | 0:969893638914 | 10 | @date May 2015 |
el13eag | 0:969893638914 | 11 | */ |
el13eag | 0:969893638914 | 12 | |
el13eag | 0:969893638914 | 13 | #ifndef MAIN_H |
el13eag | 0:969893638914 | 14 | #define MAIN_H |
el13eag | 0:969893638914 | 15 | |
el13eag | 0:969893638914 | 16 | #include "mbed.h" |
el13eag | 0:969893638914 | 17 | #include "N5110.h" |
el13eag | 0:969893638914 | 18 | #include "SRF02.h" |
el13eag | 0:969893638914 | 19 | #include "PowerControl/PowerControl.h" |
el13eag | 0:969893638914 | 20 | #include "PowerControl/EthernetPowerControl.h" |
el13eag | 0:969893638914 | 21 | |
el13eag | 0:969893638914 | 22 | /** |
el13eag | 0:969893638914 | 23 | @see URL: http://mbed.org/users/AjK/libraries/PinDetect/lkyxpw for the PinDetect |
el13eag | 0:969893638914 | 24 | */ |
el13eag | 0:969893638914 | 25 | #include "PinDetect.h" |
el13eag | 0:969893638914 | 26 | |
el13eag | 0:969893638914 | 27 | |
el13eag | 0:969893638914 | 28 | #define USR_POWERDOWN (0x104) |
el13eag | 0:969893638914 | 29 | |
el13eag | 0:969893638914 | 30 | /** |
el13eag | 0:969893638914 | 31 | @namespace Nokia N5110 LCD, LEDs, Temperatuure Sensor, Distnce Sensor, Buzzer, Button |
el13eag | 0:969893638914 | 32 | @brief GPIOs for LCD, LEDs, Temperature and Distance sensor, Printing of serial bus,Button debounce, LDR, buzzer, etc. |
el13eag | 0:969893638914 | 33 | */ |
el13eag | 0:969893638914 | 34 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26); |
el13eag | 0:969893638914 | 35 | BusOut leds(LED1, LED2, LED3, LED4); // Creat a bus out for on board LEDs |
el13eag | 0:969893638914 | 36 | I2C tmp102(p28,p27); // Creat a new I2C instance for temperature sensor and initialise |
el13eag | 0:969893638914 | 37 | SRF02 srf(p28, p27); // Creat a new I2C intsance for Distance sensor and initialise |
el13eag | 0:969893638914 | 38 | Serial serial(USBTX,USBRX); // for serial output through USB |
el13eag | 0:969893638914 | 39 | PinDetect pin (p15); // an interupt button |
el13eag | 0:969893638914 | 40 | AnalogIn ain(p16); // analogue in for the LDR used to control the sleep mode |
el13eag | 0:969893638914 | 41 | AnalogIn ain2(p20); |
el13eag | 0:969893638914 | 42 | InterruptIn button(p15); |
el13eag | 0:969893638914 | 43 | Timeout timer; |
el13eag | 0:969893638914 | 44 | PwmOut buzzer(p21); // buzzer output |
el13eag | 0:969893638914 | 45 | ////////////////////////////////////////////// |
el13eag | 0:969893638914 | 46 | PwmOut led(p26); // pwm to control LCD backlight |
el13eag | 0:969893638914 | 47 | DigitalOut sce(p8); // for LCD SCe |
el13eag | 0:969893638914 | 48 | SPI spi(p11,NC, p13); //create a new SPI intance and initialise |
el13eag | 0:969893638914 | 49 | DigitalOut pwr(p7); // for LCD power control |
el13eag | 0:969893638914 | 50 | DigitalOut dc(p10); // for LCD Dc input |
el13eag | 0:969893638914 | 51 | PwmOut led2(p24); // Pwm for LED alert |
el13eag | 0:969893638914 | 52 | |
el13eag | 0:969893638914 | 53 | Ticker timer2; // creat ticker function |
el13eag | 0:969893638914 | 54 | Ticker timer3; |
el13eag | 0:969893638914 | 55 | ////////////////////////////////////////////////////// |
el13eag | 0:969893638914 | 56 | Timeout callUpTime; |
el13eag | 0:969893638914 | 57 | |
el13eag | 0:969893638914 | 58 | /** |
el13eag | 0:969893638914 | 59 | Creates a flag to be used by the button |
el13eag | 0:969893638914 | 60 | @returns an integer |
el13eag | 0:969893638914 | 61 | */ |
el13eag | 0:969893638914 | 62 | int buttonFlag= 0; |
el13eag | 0:969893638914 | 63 | void buttonPressed() |
el13eag | 0:969893638914 | 64 | { |
el13eag | 0:969893638914 | 65 | buttonFlag = 1; |
el13eag | 0:969893638914 | 66 | } |
el13eag | 0:969893638914 | 67 | |
el13eag | 0:969893638914 | 68 | /** |
el13eag | 0:969893638914 | 69 | Defines the frequency of the pwm and the beat for sound and LED flashing for alerts |
el13eag | 0:969893638914 | 70 | @param frequncy - float for frequency |
el13eag | 0:969893638914 | 71 | @param beat - to set the tempo |
el13eag | 0:969893638914 | 72 | @see https://developer.mbed.org/media/uploads/phill/mbed_course_notes_-_pulse_width_modulation.pdf |
el13eag | 0:969893638914 | 73 | */ |
el13eag | 0:969893638914 | 74 | float frequency[]= {659,554}; //frequency array for both sound and LED (Taken from the mbed website) |
el13eag | 0:969893638914 | 75 | float beat[]= {1,1}; //beat array for both sound and LED |
el13eag | 0:969893638914 | 76 | ////////////////////////////////////////////////////// |
el13eag | 0:969893638914 | 77 | /** |
el13eag | 0:969893638914 | 78 | Defines the x,y pixels for the lcd display |
el13eag | 0:969893638914 | 79 | */ |
el13eag | 0:969893638914 | 80 | int next[84][48]; |
el13eag | 0:969893638914 | 81 | int nx=84; |
el13eag | 0:969893638914 | 82 | int ny =48; |
el13eag | 0:969893638914 | 83 | int i=0; |
el13eag | 0:969893638914 | 84 | int j=0; |
el13eag | 0:969893638914 | 85 | |
el13eag | 0:969893638914 | 86 | /** |
el13eag | 0:969893638914 | 87 | Gets the distance measurement from the sensor |
el13eag | 0:969893638914 | 88 | */ |
el13eag | 0:969893638914 | 89 | int distance = srf.getDistanceCm(); |
el13eag | 0:969893638914 | 90 | ////////////////////////////////////////////////////// |
el13eag | 0:969893638914 | 91 | |
el13eag | 0:969893638914 | 92 | void error(); // Error laert function for the range finer |
el13eag | 0:969893638914 | 93 | void setBrightness(float brightness); // Set Brightness function |
el13eag | 0:969893638914 | 94 | void clearRAM(); // Clear RAM function |
el13eag | 0:969893638914 | 95 | void setXYAddress(int x, int y); // Set XY Address function |
el13eag | 0:969893638914 | 96 | void resting(); // Function to get distance and display and to geive alerts and also place the car image according to the distance |
el13eag | 0:969893638914 | 97 | void turnOfDisplay(); // To turn of display |
el13eag | 0:969893638914 | 98 | void turnOnDisplay(); // To turn on display |
el13eag | 0:969893638914 | 99 | void sendCommand(unsigned char command); |
el13eag | 0:969893638914 | 100 | void buzzerAlert(); // Buzzer alert function |
el13eag | 0:969893638914 | 101 | void initialise(); // Function to initialise LCd and display welcome messag |
el13eag | 0:969893638914 | 102 | void initSPI(); // To initialse SPI |
el13eag | 0:969893638914 | 103 | void flipped(); |
el13eag | 0:969893638914 | 104 | void flippedStop(); |
el13eag | 0:969893638914 | 105 | void setPixel(); |
el13eag | 0:969893638914 | 106 | void controlBrightness(); |
el13eag | 0:969893638914 | 107 | ///////////////////////////////////// |
el13eag | 0:969893638914 | 108 | |
el13eag | 0:969893638914 | 109 | void initTMP102(); // To initialise Temperature |
el13eag | 0:969893638914 | 110 | void stopper(); // To get and dispaly temprature |
el13eag | 0:969893638914 | 111 | float vol; // float voltage for the potentiometer |
el13eag | 0:969893638914 | 112 | //////////////////////////////// |
el13eag | 0:969893638914 | 113 | |
el13eag | 0:969893638914 | 114 | void firstImage(); // To draw the vertical brick wall |
el13eag | 0:969893638914 | 115 | //////////////////////// |
el13eag | 0:969893638914 | 116 | // Debounce for button pressed to call up the time and Brightness with battery life infromation |
el13eag | 0:969893638914 | 117 | void p15Pressed(void); |
el13eag | 0:969893638914 | 118 | void p15Released(void); |
el13eag | 0:969893638914 | 119 | /////////////////////////////////////// |
el13eag | 0:969893638914 | 120 | |
el13eag | 0:969893638914 | 121 | void serialISR(); // ISR that is called when serial data is received |
el13eag | 0:969893638914 | 122 | void setTime(); // function to set the UNIX time |
el13eag | 0:969893638914 | 123 | int setTimeFlag = 0; // flag for ISR |
el13eag | 0:969893638914 | 124 | char rxString[16]; // buffer to store received string |
el13eag | 0:969893638914 | 125 | char buffer[30]; // creat a charracter |
el13eag | 0:969893638914 | 126 | void timerInterrupt(); |
el13eag | 0:969893638914 | 127 | /////////////////////////////// |
el13eag | 0:969893638914 | 128 | |
el13eag | 0:969893638914 | 129 | void setter(int, int, int, int, int, int, int, int, int,int, int, int, int,int, int, int); // Intergers to store the position of the car images |
el13eag | 0:969893638914 | 130 | void movingCar(int); |
el13eag | 0:969893638914 | 131 | void nextGeneration(); // TO clear and set the pixels according to the next set of conditions |
el13eag | 0:969893638914 | 132 | void drawLine(); //For horizontal line |
el13eag | 0:969893638914 | 133 | //////////////////////////////////////////////////////////// |
el13eag | 0:969893638914 | 134 | float brightness; // float for brightness values |
el13eag | 0:969893638914 | 135 | void battery(); // to indicate battery life |
el13eag | 0:969893638914 | 136 | //#Temperature Declearation |
el13eag | 0:969893638914 | 137 | void error(); |
el13eag | 0:969893638914 | 138 | float getTemperature(); // float to get temperature |
el13eag | 0:969893638914 | 139 | void ledAlert(int); |
el13eag | 0:969893638914 | 140 | |
el13eag | 0:969893638914 | 141 | /** |
el13eag | 0:969893638914 | 142 | @brief Defines the temprature sensors Registers and Address |
el13eag | 0:969893638914 | 143 | @Tag for Temperature sensor |
el13eag | 0:969893638914 | 144 | @address for ADD0 connected |
el13eag | 0:969893638914 | 145 | */ |
el13eag | 0:969893638914 | 146 | #define TMP102_ADD 0x48 |
el13eag | 0:969893638914 | 147 | #define TMP102_R_ADD 0x91 |
el13eag | 0:969893638914 | 148 | #define TMP102_W_ADD 0x90 |
el13eag | 0:969893638914 | 149 | |
el13eag | 0:969893638914 | 150 | // Register address for temperature sensor |
el13eag | 0:969893638914 | 151 | #define TEMP_REG 0x00 |
el13eag | 0:969893638914 | 152 | #define CONFIG_REG 0x01 |
el13eag | 0:969893638914 | 153 | #define THIGH_REG 0x02 |
el13eag | 0:969893638914 | 154 | #define TLOW_REG 0x03 |
el13eag | 0:969893638914 | 155 | //////////////////////////////////////////////////////////////// |
el13eag | 0:969893638914 | 156 | /** |
el13eag | 0:969893638914 | 157 | @brief to power Down the Ethernet |
el13eag | 0:969893638914 | 158 | */ |
el13eag | 0:969893638914 | 159 | int semihost_powerdown() |
el13eag | 0:969893638914 | 160 | { |
el13eag | 0:969893638914 | 161 | uint32_t arg; |
el13eag | 0:969893638914 | 162 | return __semihost(USR_POWERDOWN, &arg); |
el13eag | 0:969893638914 | 163 | } |
el13eag | 0:969893638914 | 164 | #endif |
el13eag | 0:969893638914 | 165 | |
el13eag | 0:969893638914 | 166 | int main() |
el13eag | 0:969893638914 | 167 | { |
el13eag | 0:969893638914 | 168 | // To intialise lcd and dispaly welcome messages. |
el13eag | 0:969893638914 | 169 | initialise(); |
el13eag | 0:969893638914 | 170 | ////////////////////////////////// |
el13eag | 0:969893638914 | 171 | // For getting time and date and Temperature |
el13eag | 0:969893638914 | 172 | initTMP102(); |
el13eag | 0:969893638914 | 173 | serial.attach(&serialISR); // attach serial ISR |
el13eag | 0:969893638914 | 174 | //set_time(1431109980); // initialise time to what what is inputed |
el13eag | 0:969893638914 | 175 | /////////////////////////////////////// |
el13eag | 0:969893638914 | 176 | |
el13eag | 0:969893638914 | 177 | // to define button thats calls up the time and date it works with the button using debounce |
el13eag | 0:969893638914 | 178 | pin.mode(PullDown); // To pull down the button to zero |
el13eag | 0:969893638914 | 179 | pin.attach_asserted(&p15Pressed); |
el13eag | 0:969893638914 | 180 | pin.attach_deasserted(&p15Released); |
el13eag | 0:969893638914 | 181 | pin.setSampleFrequency(); |
el13eag | 0:969893638914 | 182 | |
el13eag | 0:969893638914 | 183 | ///////////////////////////////////// |
el13eag | 0:969893638914 | 184 | // To power down USB and Ethernet |
el13eag | 0:969893638914 | 185 | int result = semihost_powerdown(); |
el13eag | 0:969893638914 | 186 | PHY_PowerDown(); |
el13eag | 0:969893638914 | 187 | |
el13eag | 0:969893638914 | 188 | button.rise(&buttonPressed); // For button |
el13eag | 0:969893638914 | 189 | |
el13eag | 0:969893638914 | 190 | while(1) { |
el13eag | 0:969893638914 | 191 | // resting(); |
el13eag | 0:969893638914 | 192 | |
el13eag | 0:969893638914 | 193 | nextGeneration(); |
el13eag | 0:969893638914 | 194 | float voltage = 5.0 * ain.read(); // to declear and initialise the LDR |
el13eag | 0:969893638914 | 195 | if(voltage <=2.50) { // If there is light |
el13eag | 0:969893638914 | 196 | resting(); |
el13eag | 0:969893638914 | 197 | setBrightness( brightness); |
el13eag | 0:969893638914 | 198 | |
el13eag | 0:969893638914 | 199 | |
el13eag | 0:969893638914 | 200 | } else if(voltage >= 2.55) { // If the garrage is dark |
el13eag | 0:969893638914 | 201 | turnOfDisplay(); |
el13eag | 0:969893638914 | 202 | led2 = 0; |
el13eag | 0:969893638914 | 203 | buzzer = 0; |
el13eag | 0:969893638914 | 204 | timer2.detach(); |
el13eag | 0:969893638914 | 205 | |
el13eag | 0:969893638914 | 206 | } |
el13eag | 0:969893638914 | 207 | Sleep(); |
el13eag | 0:969893638914 | 208 | } |
el13eag | 0:969893638914 | 209 | } |
el13eag | 0:969893638914 | 210 | |
el13eag | 0:969893638914 | 211 | /** |
el13eag | 0:969893638914 | 212 | @brief This function is used to set the X-Y address in RAM for futire data write to the LCD |
el13eag | 0:969893638914 | 213 | */ |
el13eag | 0:969893638914 | 214 | void setXYAddress(int x, int y) |
el13eag | 0:969893638914 | 215 | { |
el13eag | 0:969893638914 | 216 | |
el13eag | 0:969893638914 | 217 | if (x > 83); |
el13eag | 0:969893638914 | 218 | x = 83; |
el13eag | 0:969893638914 | 219 | if (y > 5); |
el13eag | 0:969893638914 | 220 | y = 5; |
el13eag | 0:969893638914 | 221 | if(x < 0); |
el13eag | 0:969893638914 | 222 | x =0; |
el13eag | 0:969893638914 | 223 | if (y < 0); |
el13eag | 0:969893638914 | 224 | y=0; |
el13eag | 0:969893638914 | 225 | |
el13eag | 0:969893638914 | 226 | } |
el13eag | 0:969893638914 | 227 | |
el13eag | 0:969893638914 | 228 | /** |
el13eag | 0:969893638914 | 229 | @brief This functions both the setPixel and drawLine function draws the horizontal line which acts as the floor or main road |
el13eag | 0:969893638914 | 230 | which the car is moving on |
el13eag | 0:969893638914 | 231 | */ |
el13eag | 0:969893638914 | 232 | void setPixel() |
el13eag | 0:969893638914 | 233 | { |
el13eag | 0:969893638914 | 234 | for (i = 0; i < WIDTH; i++) { |
el13eag | 0:969893638914 | 235 | lcd.setPixel(i, 24); |
el13eag | 0:969893638914 | 236 | } |
el13eag | 0:969893638914 | 237 | lcd.refresh(); |
el13eag | 0:969893638914 | 238 | } |
el13eag | 0:969893638914 | 239 | |
el13eag | 0:969893638914 | 240 | // Function to draw horizontal line |
el13eag | 0:969893638914 | 241 | void drawLine() |
el13eag | 0:969893638914 | 242 | { |
el13eag | 0:969893638914 | 243 | for (i = 0; i < WIDTH; i++) { |
el13eag | 0:969893638914 | 244 | lcd.setPixel(i, 25); |
el13eag | 0:969893638914 | 245 | } |
el13eag | 0:969893638914 | 246 | lcd.refresh(); |
el13eag | 0:969893638914 | 247 | } |
el13eag | 0:969893638914 | 248 | |
el13eag | 0:969893638914 | 249 | /** |
el13eag | 0:969893638914 | 250 | @brief This turns of the LCD completely |
el13eag | 0:969893638914 | 251 | */ |
el13eag | 0:969893638914 | 252 | void turnOfDisplay() |
el13eag | 0:969893638914 | 253 | { |
el13eag | 0:969893638914 | 254 | setBrightness(0.0); // turn backlight off |
el13eag | 0:969893638914 | 255 | clearRAM(); // clear RAM to ensure specified current consumption |
el13eag | 0:969893638914 | 256 | |
el13eag | 0:969893638914 | 257 | } |
el13eag | 0:969893638914 | 258 | |
el13eag | 0:969893638914 | 259 | /** |
el13eag | 0:969893638914 | 260 | @brief By turning the potentiometer, this function allows you to control the LCD brightneess |
el13eag | 0:969893638914 | 261 | the potentiometer uses the 3.3V output from the mbed as its input and uses an analouge in to feedback to the mbed |
el13eag | 0:969893638914 | 262 | but it must be noted that the signal or voltage the mbed recieves is between 0V to 1V it can not read in anything above this from the potenteiometer |
el13eag | 0:969893638914 | 263 | */ |
el13eag | 0:969893638914 | 264 | void setBrightness(float brightness) |
el13eag | 0:969893638914 | 265 | { |
el13eag | 0:969893638914 | 266 | float vol = 3.3*ain2; |
el13eag | 0:969893638914 | 267 | float voltage = 5.0*ain; |
el13eag | 0:969893638914 | 268 | if ((vol >= 0.0) && (vol < 0.3)) { |
el13eag | 0:969893638914 | 269 | brightness = 0.0; |
el13eag | 0:969893638914 | 270 | } |
el13eag | 0:969893638914 | 271 | if ((vol >= 0.3) && (vol <0.6)) { |
el13eag | 0:969893638914 | 272 | brightness = 0.1; |
el13eag | 0:969893638914 | 273 | } |
el13eag | 0:969893638914 | 274 | if ((vol >= 0.6) && (vol < 0.9)) { |
el13eag | 0:969893638914 | 275 | brightness = 0.2; |
el13eag | 0:969893638914 | 276 | } |
el13eag | 0:969893638914 | 277 | if ((vol >= 0.9) && (vol <1.2)) { |
el13eag | 0:969893638914 | 278 | brightness = 0.3; |
el13eag | 0:969893638914 | 279 | } |
el13eag | 0:969893638914 | 280 | if ((vol >= 1.2) && (vol <1.5)) { |
el13eag | 0:969893638914 | 281 | brightness = 0.4; |
el13eag | 0:969893638914 | 282 | } |
el13eag | 0:969893638914 | 283 | |
el13eag | 0:969893638914 | 284 | if ((vol >= 1.5) && (vol < 1.8)) { |
el13eag | 0:969893638914 | 285 | brightness = 0.5; |
el13eag | 0:969893638914 | 286 | } |
el13eag | 0:969893638914 | 287 | if ((vol >= 1.8) && (vol <2.1)) { |
el13eag | 0:969893638914 | 288 | brightness = 0.6; |
el13eag | 0:969893638914 | 289 | } |
el13eag | 0:969893638914 | 290 | if ((vol >= 2.1) && (vol < 2.4)) { |
el13eag | 0:969893638914 | 291 | brightness = 0.7; |
el13eag | 0:969893638914 | 292 | } |
el13eag | 0:969893638914 | 293 | if ((vol >= 2.4)&& (vol < 2.7)) { |
el13eag | 0:969893638914 | 294 | brightness = 0.8; |
el13eag | 0:969893638914 | 295 | } |
el13eag | 0:969893638914 | 296 | if ((vol >= 2.7)&& (vol < 3)) { |
el13eag | 0:969893638914 | 297 | brightness = 0.9; |
el13eag | 0:969893638914 | 298 | } |
el13eag | 0:969893638914 | 299 | if ((vol >= 3.0)&& (vol <=3.3)) { |
el13eag | 0:969893638914 | 300 | brightness = 1.0; |
el13eag | 0:969893638914 | 301 | } |
el13eag | 0:969893638914 | 302 | if ((voltage >= 2.55)&& (vol == vol)) { |
el13eag | 0:969893638914 | 303 | brightness = 0.0; |
el13eag | 0:969893638914 | 304 | } |
el13eag | 0:969893638914 | 305 | led.write(brightness); |
el13eag | 0:969893638914 | 306 | |
el13eag | 0:969893638914 | 307 | } |
el13eag | 0:969893638914 | 308 | |
el13eag | 0:969893638914 | 309 | /** |
el13eag | 0:969893638914 | 310 | @brief This function writes a 0 to the 504 bytes to clear the RAM in other to reduce the current waste |
el13eag | 0:969893638914 | 311 | */ |
el13eag | 0:969893638914 | 312 | void clearRAM() |
el13eag | 0:969893638914 | 313 | { |
el13eag | 0:969893638914 | 314 | int i; |
el13eag | 0:969893638914 | 315 | sce.write(0); |
el13eag | 0:969893638914 | 316 | for(i = 0; i <504; i++) { |
el13eag | 0:969893638914 | 317 | spi.write(0x00); |
el13eag | 0:969893638914 | 318 | } |
el13eag | 0:969893638914 | 319 | sce.write(1); |
el13eag | 0:969893638914 | 320 | } |
el13eag | 0:969893638914 | 321 | |
el13eag | 0:969893638914 | 322 | /** |
el13eag | 0:969893638914 | 323 | @brief This function "resting" contians codes which gets the distance from the sensor and displays it on the LCD |
el13eag | 0:969893638914 | 324 | Also still in this function other functions like the getTempearature are called as they all are display at same time on the screen |
el13eag | 0:969893638914 | 325 | And all the alerts both sound and virtual associated with the car parking aspect of the device are implement here |
el13eag | 0:969893638914 | 326 | |
el13eag | 0:969893638914 | 327 | for PWM out code for buzzer sound, |
el13eag | 0:969893638914 | 328 | @see https://developer.mbed.org/media/uploads/phill/mbed_course_notes_-_pulse_width_modulation.pdf |
el13eag | 0:969893638914 | 329 | */ |
el13eag | 0:969893638914 | 330 | void resting() |
el13eag | 0:969893638914 | 331 | { |
el13eag | 0:969893638914 | 332 | drawLine(); |
el13eag | 0:969893638914 | 333 | firstImage(); |
el13eag | 0:969893638914 | 334 | led2 = 0; |
el13eag | 0:969893638914 | 335 | buzzer =0; |
el13eag | 0:969893638914 | 336 | stopper(); |
el13eag | 0:969893638914 | 337 | ///////////////////////////////////////// |
el13eag | 0:969893638914 | 338 | //Get Distance from sensor dispaly and depending on the conditions, get the appropriate alert and information |
el13eag | 0:969893638914 | 339 | // This is where the values for the x and y cordinates to draw the car is also inputted and also the alert the triggers the buzzer and lights up the LED |
el13eag | 0:969893638914 | 340 | int distance = srf.getDistanceCm(); // get distance in cm and save in distance |
el13eag | 0:969893638914 | 341 | |
el13eag | 0:969893638914 | 342 | char buffer[14]; |
el13eag | 0:969893638914 | 343 | int length = sprintf(buffer, "%2d cm",distance); // To define the length of the charracter which must not be more than 14 charracters |
el13eag | 0:969893638914 | 344 | |
el13eag | 0:969893638914 | 345 | if (length <= 14) { |
el13eag | 0:969893638914 | 346 | if (distance > 249) { // Distance above 249cm which is the max range the sensor can detect |
el13eag | 0:969893638914 | 347 | lcd.printString("Out of Range", 4, 1); |
el13eag | 0:969893638914 | 348 | } |
el13eag | 0:969893638914 | 349 | if ((distance >= 232) && (distance <=249)) { |
el13eag | 0:969893638914 | 350 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 351 | setter(68, 78, 65, 81, 18, 21, 21, 23, 68, 70,76 ,78, 23 ,25, 23, 25); // These are the values that draws the car |
el13eag | 0:969893638914 | 352 | lcd.printString("Very safe", 15, 1); |
el13eag | 0:969893638914 | 353 | } |
el13eag | 0:969893638914 | 354 | if ((distance >= 215) && (distance <= 232)) { |
el13eag | 0:969893638914 | 355 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 356 | setter(63, 73, 60, 76, 18, 21, 21, 23, 63, 65, 71 ,73, 23 ,25, 23, 25); |
el13eag | 0:969893638914 | 357 | lcd.printString("Very Safe", 15, 1); |
el13eag | 0:969893638914 | 358 | } |
el13eag | 0:969893638914 | 359 | if((distance >= 198) && (distance <= 215)) { |
el13eag | 0:969893638914 | 360 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 361 | setter(58, 68, 55, 71, 18, 21, 21, 23, 58, 60, 66 ,68, 23 ,25, 23, 25); |
el13eag | 0:969893638914 | 362 | lcd.printString("Very Safe", 15, 1); |
el13eag | 0:969893638914 | 363 | } |
el13eag | 0:969893638914 | 364 | |
el13eag | 0:969893638914 | 365 | |
el13eag | 0:969893638914 | 366 | if ((distance >= 181) && (distance <=198)) { |
el13eag | 0:969893638914 | 367 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 368 | setter(53, 63, 50, 66, 18, 21, 21, 23, 53, 55, 61 ,63,23 ,25, 23, 25); |
el13eag | 0:969893638914 | 369 | lcd.printString("Very Safe", 15, 1); |
el13eag | 0:969893638914 | 370 | } |
el13eag | 0:969893638914 | 371 | if((distance >= 164) && (distance <=181)) { |
el13eag | 0:969893638914 | 372 | |
el13eag | 0:969893638914 | 373 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 374 | setter(48, 58, 45, 61, 18, 21, 21, 23, 48, 50, 56 ,58, 23 ,25, 23, 25); |
el13eag | 0:969893638914 | 375 | lcd.printString("Safe Distance", 4, 1); |
el13eag | 0:969893638914 | 376 | |
el13eag | 0:969893638914 | 377 | } |
el13eag | 0:969893638914 | 378 | |
el13eag | 0:969893638914 | 379 | if ((distance >= 150) && (distance <= 164)) { |
el13eag | 0:969893638914 | 380 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 381 | setter(43, 53, 40, 56, 18, 21, 21, 23, 43, 45, 51 ,53, 23 ,25, 23, 25); |
el13eag | 0:969893638914 | 382 | lcd.printString("Safe Distance", 4, 1); |
el13eag | 0:969893638914 | 383 | |
el13eag | 0:969893638914 | 384 | } |
el13eag | 0:969893638914 | 385 | if((distance >= 133) && (distance <=150)) { |
el13eag | 0:969893638914 | 386 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 387 | setter(38, 48, 35, 51, 18, 21, 21, 23, 38, 40, 46 ,48,23 ,25, 23, 25); |
el13eag | 0:969893638914 | 388 | lcd.printString("Safe Distance", 4, 1); |
el13eag | 0:969893638914 | 389 | |
el13eag | 0:969893638914 | 390 | } |
el13eag | 0:969893638914 | 391 | if((distance >= 116) && (distance <= 133)) { |
el13eag | 0:969893638914 | 392 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 393 | setter(33, 43, 30, 46, 18, 21, 21, 23, 33, 35, 41 ,43, 23 ,25, 23, 25); |
el13eag | 0:969893638914 | 394 | lcd.printString("Safe Distance", 4, 1); |
el13eag | 0:969893638914 | 395 | |
el13eag | 0:969893638914 | 396 | } |
el13eag | 0:969893638914 | 397 | if((distance >= 99) && (distance <=116)) { |
el13eag | 0:969893638914 | 398 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 399 | setter(28, 38, 25, 41, 18, 21, 21, 23, 28, 30, 36 ,38, 23 ,25, 23, 25); |
el13eag | 0:969893638914 | 400 | lcd.printString("Safe Distance", 4, 1); |
el13eag | 0:969893638914 | 401 | |
el13eag | 0:969893638914 | 402 | } |
el13eag | 0:969893638914 | 403 | if((distance >= 82) && (distance <=99)) { |
el13eag | 0:969893638914 | 404 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 405 | setter(23, 33, 20, 36, 18, 21, 21, 23, 23, 25, 31 ,33, 23 ,25, 23, 25); |
el13eag | 0:969893638914 | 406 | lcd.printString("Getting Close", 4, 1); |
el13eag | 0:969893638914 | 407 | for (int i=0; i<=1; i++) { |
el13eag | 0:969893638914 | 408 | buzzer.period(1/(frequency[i])); // set PWM period |
el13eag | 0:969893638914 | 409 | buzzer=0.9; // set duty cycle |
el13eag | 0:969893638914 | 410 | wait(0.5*beat[i]); // hold for beat period |
el13eag | 0:969893638914 | 411 | led2 = 1; |
el13eag | 0:969893638914 | 412 | } |
el13eag | 0:969893638914 | 413 | } |
el13eag | 0:969893638914 | 414 | if((distance >= 65) && (distance <= 82)) { |
el13eag | 0:969893638914 | 415 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 416 | setter(18, 28, 15, 31, 18, 21, 21, 23, 18, 20, 26 ,28,23 ,25, 23, 25); |
el13eag | 0:969893638914 | 417 | lcd.printString("Getting Close", 4, 1); |
el13eag | 0:969893638914 | 418 | for (int i=0; i<=1; i++) { |
el13eag | 0:969893638914 | 419 | buzzer.period(1/(frequency[i])); // set PWM period |
el13eag | 0:969893638914 | 420 | buzzer=0.9; // set duty cycle |
el13eag | 0:969893638914 | 421 | wait(0.5*beat[i]); // hold for beat period |
el13eag | 0:969893638914 | 422 | led2 = 1; |
el13eag | 0:969893638914 | 423 | } |
el13eag | 0:969893638914 | 424 | } |
el13eag | 0:969893638914 | 425 | if((distance >= 48) && (distance<=65)) { |
el13eag | 0:969893638914 | 426 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 427 | setter(13, 23, 10, 26,18, 21, 21, 23, 13, 15, 21 ,23,23 ,25, 23, 25); |
el13eag | 0:969893638914 | 428 | lcd.printString("Too Close", 15, 1); |
el13eag | 0:969893638914 | 429 | for (int i=0; i<=1; i++) { |
el13eag | 0:969893638914 | 430 | buzzer.period(1/(frequency[i])); // set PWM period |
el13eag | 0:969893638914 | 431 | buzzer=0.9; // set duty cycle |
el13eag | 0:969893638914 | 432 | wait(0.1*beat[i]); // hold for beat period |
el13eag | 0:969893638914 | 433 | led2 = 1; |
el13eag | 0:969893638914 | 434 | } |
el13eag | 0:969893638914 | 435 | } |
el13eag | 0:969893638914 | 436 | if ((distance >= 31) && (distance <=48)) { |
el13eag | 0:969893638914 | 437 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 438 | setter(8, 18, 5, 21, 18, 21, 21, 23, 8, 10, 16 ,18,23 ,25, 23, 25); |
el13eag | 0:969893638914 | 439 | lcd.printString("Too Close", 15, 1); |
el13eag | 0:969893638914 | 440 | for (int i=0; i<=1; i++) { |
el13eag | 0:969893638914 | 441 | buzzer.period(1/(frequency[i])); // set PWM period |
el13eag | 0:969893638914 | 442 | buzzer=0.9; // set duty cycle |
el13eag | 0:969893638914 | 443 | wait(0.1*beat[i]); // hold for beat period |
el13eag | 0:969893638914 | 444 | led2 = 1; |
el13eag | 0:969893638914 | 445 | } |
el13eag | 0:969893638914 | 446 | |
el13eag | 0:969893638914 | 447 | } |
el13eag | 0:969893638914 | 448 | if (distance <=31) { // though the sensor can detect up to 13cm but it was set to 31cm so as to always give drivers safe distance from other objects |
el13eag | 0:969893638914 | 449 | lcd.printString(buffer, 25,0); |
el13eag | 0:969893638914 | 450 | setter(5, 15, 2, 18, 18, 21, 21, 23, 5, 7, 13 ,15, 23 ,25, 23, 25); |
el13eag | 0:969893638914 | 451 | lcd.printString("Limit Reached", 4, 1); |
el13eag | 0:969893638914 | 452 | for (int i=0; i<=1; i++) { |
el13eag | 0:969893638914 | 453 | buzzer.period(1/(frequency[i])); // set PWM period |
el13eag | 0:969893638914 | 454 | buzzer=0.09; // set duty cycle |
el13eag | 0:969893638914 | 455 | wait(0.01*beat[i]); // hold for beat period |
el13eag | 0:969893638914 | 456 | led2 = 1; |
el13eag | 0:969893638914 | 457 | |
el13eag | 0:969893638914 | 458 | } |
el13eag | 0:969893638914 | 459 | } |
el13eag | 0:969893638914 | 460 | } |
el13eag | 0:969893638914 | 461 | wait (0.5); // A little delay though not really needed for the distance but was added to help to be more accurate. Ticker timer would be better off |
el13eag | 0:969893638914 | 462 | // but at the time of this project was not really working one of the future aims is to find a way to make it work |
el13eag | 0:969893638914 | 463 | |
el13eag | 0:969893638914 | 464 | lcd.refresh(); |
el13eag | 0:969893638914 | 465 | |
el13eag | 0:969893638914 | 466 | } |
el13eag | 0:969893638914 | 467 | /** |
el13eag | 0:969893638914 | 468 | @brief This function is used to draw the vertical brick wall which show the limit the car sensor can car and to alert the driver that he or she is getting close to an object |
el13eag | 0:969893638914 | 469 | */ |
el13eag | 0:969893638914 | 470 | void firstImage() |
el13eag | 0:969893638914 | 471 | { |
el13eag | 0:969893638914 | 472 | for (j = 0; j < 26; j++) { |
el13eag | 0:969893638914 | 473 | lcd.setPixel(0, j); |
el13eag | 0:969893638914 | 474 | } |
el13eag | 0:969893638914 | 475 | for (j = 0; j < 26; j++) { |
el13eag | 0:969893638914 | 476 | lcd.setPixel(1, j); |
el13eag | 0:969893638914 | 477 | } |
el13eag | 0:969893638914 | 478 | lcd.refresh(); |
el13eag | 0:969893638914 | 479 | |
el13eag | 0:969893638914 | 480 | |
el13eag | 0:969893638914 | 481 | } |
el13eag | 0:969893638914 | 482 | |
el13eag | 0:969893638914 | 483 | /** |
el13eag | 0:969893638914 | 484 | @brief This is declears variables which are later used to draw the moving car depending on the values of the x and y cordinates inputted |
el13eag | 0:969893638914 | 485 | */ |
el13eag | 0:969893638914 | 486 | void setter(int xVert, int xHor, int xVert2, int xHor2, int yVert, int yHor, int yVert2, int yHor2, int xVert3, int xHor3, int xVert4, int xHor4, int yVert3, int yHor3, int yVert4, int yHor4) |
el13eag | 0:969893638914 | 487 | { |
el13eag | 0:969893638914 | 488 | int i,j; |
el13eag | 0:969893638914 | 489 | |
el13eag | 0:969893638914 | 490 | // Take the given values and draw the car |
el13eag | 0:969893638914 | 491 | for (i = xVert; i < xHor; i++) { |
el13eag | 0:969893638914 | 492 | for (j = yVert; j < yHor; j++) { |
el13eag | 0:969893638914 | 493 | |
el13eag | 0:969893638914 | 494 | lcd.setPixel(i, j); |
el13eag | 0:969893638914 | 495 | } |
el13eag | 0:969893638914 | 496 | } |
el13eag | 0:969893638914 | 497 | for (i = xVert2; i < xHor2; i++) { |
el13eag | 0:969893638914 | 498 | for (j = yVert2; j < yHor2; j++) { |
el13eag | 0:969893638914 | 499 | lcd.setPixel(i, j); |
el13eag | 0:969893638914 | 500 | |
el13eag | 0:969893638914 | 501 | } |
el13eag | 0:969893638914 | 502 | } |
el13eag | 0:969893638914 | 503 | for (i = xVert3; i < xHor3; i++) { |
el13eag | 0:969893638914 | 504 | for (j = yVert3; j < yHor3; j++) { |
el13eag | 0:969893638914 | 505 | |
el13eag | 0:969893638914 | 506 | lcd.setPixel(i, j); |
el13eag | 0:969893638914 | 507 | } |
el13eag | 0:969893638914 | 508 | } |
el13eag | 0:969893638914 | 509 | for (i = xVert4; i < xHor4; i++) { |
el13eag | 0:969893638914 | 510 | for (j = yVert4; j < yHor4; j++) { |
el13eag | 0:969893638914 | 511 | lcd.setPixel(i, j); |
el13eag | 0:969893638914 | 512 | |
el13eag | 0:969893638914 | 513 | } |
el13eag | 0:969893638914 | 514 | lcd.refresh(); |
el13eag | 0:969893638914 | 515 | } |
el13eag | 0:969893638914 | 516 | |
el13eag | 0:969893638914 | 517 | |
el13eag | 0:969893638914 | 518 | |
el13eag | 0:969893638914 | 519 | lcd.refresh(); |
el13eag | 0:969893638914 | 520 | } |
el13eag | 0:969893638914 | 521 | |
el13eag | 0:969893638914 | 522 | |
el13eag | 0:969893638914 | 523 | |
el13eag | 0:969893638914 | 524 | void nextGeneration() // Function for generting the next set of pixels based on the conditions |
el13eag | 0:969893638914 | 525 | { |
el13eag | 0:969893638914 | 526 | int nx =84; |
el13eag | 0:969893638914 | 527 | int ny =48; |
el13eag | 0:969893638914 | 528 | for (int i=0; i<nx; i++) { |
el13eag | 0:969893638914 | 529 | for (int j=0; j<ny; j++) { |
el13eag | 0:969893638914 | 530 | if (next[i][j]==1) { |
el13eag | 0:969893638914 | 531 | lcd.setPixel(i,j); |
el13eag | 0:969893638914 | 532 | } else if(next[i][j]==0) { |
el13eag | 0:969893638914 | 533 | lcd.clearPixel(i,j); |
el13eag | 0:969893638914 | 534 | } |
el13eag | 0:969893638914 | 535 | } |
el13eag | 0:969893638914 | 536 | } |
el13eag | 0:969893638914 | 537 | lcd.refresh(); |
el13eag | 0:969893638914 | 538 | } |
el13eag | 0:969893638914 | 539 | |
el13eag | 0:969893638914 | 540 | /** |
el13eag | 0:969893638914 | 541 | @brief Here the LCD initialisation function is called and the welcome messages and also authors details are |
el13eag | 0:969893638914 | 542 | implmemet which runs only once whenever the device is powered on |
el13eag | 0:969893638914 | 543 | */ |
el13eag | 0:969893638914 | 544 | void initialise() |
el13eag | 0:969893638914 | 545 | { |
el13eag | 0:969893638914 | 546 | lcd.init(); // initialise lcd |
el13eag | 0:969893638914 | 547 | initTMP102(); |
el13eag | 0:969893638914 | 548 | wait(1.0); // delay |
el13eag | 0:969893638914 | 549 | lcd.printString("Welcome",20,3); // welcome screen |
el13eag | 0:969893638914 | 550 | wait(2.0); // dealy |
el13eag | 0:969893638914 | 551 | lcd.clear(); |
el13eag | 0:969893638914 | 552 | lcd.printString("Adolphus G. E.",1,1); // welcome screen |
el13eag | 0:969893638914 | 553 | wait(0.5); // dealy |
el13eag | 0:969893638914 | 554 | |
el13eag | 0:969893638914 | 555 | lcd.printString("SID: 200183574",1,2); // welcome screen |
el13eag | 0:969893638914 | 556 | wait(0.5); // dealy |
el13eag | 0:969893638914 | 557 | lcd.printString("ELEC2645",1,4); // welcome screen |
el13eag | 0:969893638914 | 558 | wait(2.0); // dealy |
el13eag | 0:969893638914 | 559 | lcd.clear(); // clear welcome screen |
el13eag | 0:969893638914 | 560 | setXYAddress(0,0); // set lcd address |
el13eag | 0:969893638914 | 561 | lcd.refresh(); // refresh |
el13eag | 0:969893638914 | 562 | |
el13eag | 0:969893638914 | 563 | } |
el13eag | 0:969893638914 | 564 | |
el13eag | 0:969893638914 | 565 | /** |
el13eag | 0:969893638914 | 566 | @brief Functions to get and display the time |
el13eag | 0:969893638914 | 567 | */ |
el13eag | 0:969893638914 | 568 | void tempTime() |
el13eag | 0:969893638914 | 569 | { |
el13eag | 0:969893638914 | 570 | |
el13eag | 0:969893638914 | 571 | time_t seconds = time(NULL); |
el13eag | 0:969893638914 | 572 | strftime(buffer,32,"%I:%M %p",localtime(&seconds)); |
el13eag | 0:969893638914 | 573 | int length = sprintf(buffer,"%s", buffer); |
el13eag | 0:969893638914 | 574 | if (length <= 14) { |
el13eag | 0:969893638914 | 575 | lcd.printString(buffer,0,0); // To display time |
el13eag | 0:969893638914 | 576 | timerInterrupt(); // To dispaly Date |
el13eag | 0:969893638914 | 577 | controlBrightness(); |
el13eag | 0:969893638914 | 578 | battery(); |
el13eag | 0:969893638914 | 579 | |
el13eag | 0:969893638914 | 580 | if (setTimeFlag) { // if updated time has been sent |
el13eag | 0:969893638914 | 581 | setTimeFlag = 0; // clear flag |
el13eag | 0:969893638914 | 582 | setTime(); // update time |
el13eag | 0:969893638914 | 583 | } |
el13eag | 0:969893638914 | 584 | |
el13eag | 0:969893638914 | 585 | } |
el13eag | 0:969893638914 | 586 | |
el13eag | 0:969893638914 | 587 | } |
el13eag | 0:969893638914 | 588 | /** |
el13eag | 0:969893638914 | 589 | @brief Functions to get and display the date |
el13eag | 0:969893638914 | 590 | */ |
el13eag | 0:969893638914 | 591 | void timerInterrupt() |
el13eag | 0:969893638914 | 592 | { |
el13eag | 0:969893638914 | 593 | char buffer[30]; // buffer used to store time string |
el13eag | 0:969893638914 | 594 | time_t seconds = time(NULL); // get current time |
el13eag | 0:969893638914 | 595 | // format time into a string (time and date) |
el13eag | 0:969893638914 | 596 | strftime(buffer, 30 , "%D", localtime(&seconds)); |
el13eag | 0:969893638914 | 597 | int length = sprintf(buffer,"%s", buffer); |
el13eag | 0:969893638914 | 598 | if (length <= 14) { |
el13eag | 0:969893638914 | 599 | lcd.printString(buffer,0,1); // dispaly Date |
el13eag | 0:969893638914 | 600 | } |
el13eag | 0:969893638914 | 601 | lcd.refresh(); |
el13eag | 0:969893638914 | 602 | |
el13eag | 0:969893638914 | 603 | } |
el13eag | 0:969893638914 | 604 | |
el13eag | 0:969893638914 | 605 | void setTime() |
el13eag | 0:969893638914 | 606 | { |
el13eag | 0:969893638914 | 607 | // print time for debugging |
el13eag | 0:969893638914 | 608 | serial.printf("set_time - %s",rxString); |
el13eag | 0:969893638914 | 609 | // atoi() converts a string to an integer |
el13eag | 0:969893638914 | 610 | int time = atoi(rxString); |
el13eag | 0:969893638914 | 611 | // update the time |
el13eag | 0:969893638914 | 612 | set_time(time); |
el13eag | 0:969893638914 | 613 | } |
el13eag | 0:969893638914 | 614 | |
el13eag | 0:969893638914 | 615 | void serialISR() |
el13eag | 0:969893638914 | 616 | { |
el13eag | 0:969893638914 | 617 | // when a serial interrupt occurs, read rx string into buffer |
el13eag | 0:969893638914 | 618 | serial.gets(rxString,16); |
el13eag | 0:969893638914 | 619 | // set flag |
el13eag | 0:969893638914 | 620 | setTimeFlag = 1; |
el13eag | 0:969893638914 | 621 | } |
el13eag | 0:969893638914 | 622 | |
el13eag | 0:969893638914 | 623 | /** |
el13eag | 0:969893638914 | 624 | @brief Displays the temperature and also calls contains the alert messages for the temperature aspect of the device |
el13eag | 0:969893638914 | 625 | */ |
el13eag | 0:969893638914 | 626 | void stopper() |
el13eag | 0:969893638914 | 627 | { |
el13eag | 0:969893638914 | 628 | float temperature = getTemperature(); |
el13eag | 0:969893638914 | 629 | char buffer[14]; |
el13eag | 0:969893638914 | 630 | int length = sprintf(buffer,"Temp: %2.f C", temperature); |
el13eag | 0:969893638914 | 631 | if (length <= 14) { |
el13eag | 0:969893638914 | 632 | lcd.printString(buffer,13,4); |
el13eag | 0:969893638914 | 633 | if (temperature < 10) { |
el13eag | 0:969893638914 | 634 | |
el13eag | 0:969893638914 | 635 | lcd.printString("Cold Weather", 8, 5); |
el13eag | 0:969893638914 | 636 | } |
el13eag | 0:969893638914 | 637 | if (temperature >=10 && temperature <=20) { |
el13eag | 0:969893638914 | 638 | |
el13eag | 0:969893638914 | 639 | lcd.printString("Fairly Warm", 8, 5); |
el13eag | 0:969893638914 | 640 | } |
el13eag | 0:969893638914 | 641 | if (temperature >20) { |
el13eag | 0:969893638914 | 642 | |
el13eag | 0:969893638914 | 643 | lcd.printString("Warm Weather", 8, 5); |
el13eag | 0:969893638914 | 644 | } |
el13eag | 0:969893638914 | 645 | } |
el13eag | 0:969893638914 | 646 | lcd.refresh(); |
el13eag | 0:969893638914 | 647 | |
el13eag | 0:969893638914 | 648 | } |
el13eag | 0:969893638914 | 649 | /** |
el13eag | 0:969893638914 | 650 | @brief hangs in infinite loop flashing error code when something gors wrong for instance is the distance sensor was not detected |
el13eag | 0:969893638914 | 651 | */ |
el13eag | 0:969893638914 | 652 | void error(int code) |
el13eag | 0:969893638914 | 653 | { |
el13eag | 0:969893638914 | 654 | while(1) { |
el13eag | 0:969893638914 | 655 | leds = 0; |
el13eag | 0:969893638914 | 656 | wait(0.25); |
el13eag | 0:969893638914 | 657 | leds = code; |
el13eag | 0:969893638914 | 658 | wait(0.25); |
el13eag | 0:969893638914 | 659 | } |
el13eag | 0:969893638914 | 660 | } |
el13eag | 0:969893638914 | 661 | |
el13eag | 0:969893638914 | 662 | /** |
el13eag | 0:969893638914 | 663 | @brief Function to initialise the temperature sensor |
el13eag | 0:969893638914 | 664 | */ |
el13eag | 0:969893638914 | 665 | void initTMP102() |
el13eag | 0:969893638914 | 666 | { |
el13eag | 0:969893638914 | 667 | tmp102.frequency(400000); // set bus speed to 400 kHz |
el13eag | 0:969893638914 | 668 | int ack; // used to store acknowledgement bit |
el13eag | 0:969893638914 | 669 | char data[2]; // array for data |
el13eag | 0:969893638914 | 670 | char reg = CONFIG_REG; // register address |
el13eag | 0:969893638914 | 671 | //////// Read current status of configuration register /////// |
el13eag | 0:969893638914 | 672 | |
el13eag | 0:969893638914 | 673 | ack = tmp102.write(TMP102_W_ADD,®,1); // send the slave write address and the configuration register address |
el13eag | 0:969893638914 | 674 | if (ack) |
el13eag | 0:969893638914 | 675 | error(1); // if we don't receive acknowledgement, flash error message |
el13eag | 0:969893638914 | 676 | ack = tmp102.read(TMP102_R_ADD,data,2); // read default 2 bytes from configuration register and store in buffer |
el13eag | 0:969893638914 | 677 | if (ack) |
el13eag | 0:969893638914 | 678 | error(2); // if we don't receive acknowledgement, flash error message |
el13eag | 0:969893638914 | 679 | ///////// Configure the register ////////// |
el13eag | 0:969893638914 | 680 | // set conversion rate to 1 Hz |
el13eag | 0:969893638914 | 681 | data[1] |= (1 << 6); // set bit 6 |
el13eag | 0:969893638914 | 682 | data[1] &= ~(1 << 7); // clear bit 7 |
el13eag | 0:969893638914 | 683 | |
el13eag | 0:969893638914 | 684 | //////// Send the configured register to the slave //////////// |
el13eag | 0:969893638914 | 685 | |
el13eag | 0:969893638914 | 686 | ack = tmp102.write(TMP102_W_ADD,®,1); // send the slave write address and the configuration register address |
el13eag | 0:969893638914 | 687 | if (ack) |
el13eag | 0:969893638914 | 688 | error(3); // if we don't receive acknowledgement, flash error message |
el13eag | 0:969893638914 | 689 | ack = tmp102.write(TMP102_W_ADD,data,2); // send 2 data bytes to the configuration register |
el13eag | 0:969893638914 | 690 | if (ack) |
el13eag | 0:969893638914 | 691 | error(4); // if we don't receive acknowledgement, flash error message |
el13eag | 0:969893638914 | 692 | |
el13eag | 0:969893638914 | 693 | } |
el13eag | 0:969893638914 | 694 | |
el13eag | 0:969893638914 | 695 | |
el13eag | 0:969893638914 | 696 | /** |
el13eag | 0:969893638914 | 697 | @brief Function to the temperature from the Temearature sensor |
el13eag | 0:969893638914 | 698 | */ |
el13eag | 0:969893638914 | 699 | float getTemperature() |
el13eag | 0:969893638914 | 700 | |
el13eag | 0:969893638914 | 701 | { |
el13eag | 0:969893638914 | 702 | int ack; // used to store acknowledgement bit |
el13eag | 0:969893638914 | 703 | char data[2]; // array for data |
el13eag | 0:969893638914 | 704 | char reg = TEMP_REG; // temperature register address |
el13eag | 0:969893638914 | 705 | ack = tmp102.write(TMP102_W_ADD,®,1); // send temperature register address |
el13eag | 0:969893638914 | 706 | if (ack) |
el13eag | 0:969893638914 | 707 | error(5); // if we don't receive acknowledgement, flash error message |
el13eag | 0:969893638914 | 708 | ack = tmp102.read(TMP102_R_ADD,data,2); // read 2 bytes from temperature register and store in array |
el13eag | 0:969893638914 | 709 | if (ack) |
el13eag | 0:969893638914 | 710 | error(6); // if we don't receive acknowledgement, flash error message |
el13eag | 0:969893638914 | 711 | int temperature = (data[0] << 4) | (data[1] >> 4); |
el13eag | 0:969893638914 | 712 | return temperature*0.0625; |
el13eag | 0:969893638914 | 713 | } |
el13eag | 0:969893638914 | 714 | |
el13eag | 0:969893638914 | 715 | /** |
el13eag | 0:969893638914 | 716 | @brief To clear the screen and prepare to show the date and time when the button is pressed |
el13eag | 0:969893638914 | 717 | */ |
el13eag | 0:969893638914 | 718 | void p15Pressed(void)// To clear the screen before calling up the function |
el13eag | 0:969893638914 | 719 | { |
el13eag | 0:969893638914 | 720 | lcd.clear(); |
el13eag | 0:969893638914 | 721 | setXYAddress(0,0); |
el13eag | 0:969893638914 | 722 | wait(0.5); |
el13eag | 0:969893638914 | 723 | lcd.refresh(); |
el13eag | 0:969893638914 | 724 | } |
el13eag | 0:969893638914 | 725 | |
el13eag | 0:969893638914 | 726 | /** |
el13eag | 0:969893638914 | 727 | @brief The function is part of the PinDetect library which dose the callback function |
el13eag | 0:969893638914 | 728 | */ |
el13eag | 0:969893638914 | 729 | void p15Released(void) // To show the content of the function (Date and time, Brightness and battey) |
el13eag | 0:969893638914 | 730 | { |
el13eag | 0:969893638914 | 731 | buzzer =0; |
el13eag | 0:969893638914 | 732 | led2 =0; |
el13eag | 0:969893638914 | 733 | tempTime(); // This is the name of the function that contains the informations to be dispalyed |
el13eag | 0:969893638914 | 734 | |
el13eag | 0:969893638914 | 735 | wait(3.0); |
el13eag | 0:969893638914 | 736 | lcd.clear(); |
el13eag | 0:969893638914 | 737 | lcd.refresh(); |
el13eag | 0:969893638914 | 738 | } |
el13eag | 0:969893638914 | 739 | |
el13eag | 0:969893638914 | 740 | /** |
el13eag | 0:969893638914 | 741 | @biref When the buton is pressed, this function enables the user to view the percentage of the brightness |
el13eag | 0:969893638914 | 742 | */ |
el13eag | 0:969893638914 | 743 | void controlBrightness() |
el13eag | 0:969893638914 | 744 | { |
el13eag | 0:969893638914 | 745 | float vol = 3.3*ain2; |
el13eag | 0:969893638914 | 746 | if ((vol >= 0.0) && (vol < 0.3)) { |
el13eag | 0:969893638914 | 747 | lcd.printString("Brightness=0%",0,3); |
el13eag | 0:969893638914 | 748 | } |
el13eag | 0:969893638914 | 749 | if ((vol >= 0.3) && (vol <0.6)) { |
el13eag | 0:969893638914 | 750 | lcd.printString("Brightness=10%",0,3); |
el13eag | 0:969893638914 | 751 | } |
el13eag | 0:969893638914 | 752 | if ((vol >= 0.6) && (vol < 0.9)) { |
el13eag | 0:969893638914 | 753 | lcd.printString("Brightness=20%",0,3); |
el13eag | 0:969893638914 | 754 | } |
el13eag | 0:969893638914 | 755 | if ((vol >= 0.9) && (vol <1.2)) { |
el13eag | 0:969893638914 | 756 | lcd.printString("Brightness=30%",0,3); |
el13eag | 0:969893638914 | 757 | } |
el13eag | 0:969893638914 | 758 | if ((vol >= 1.2) && (vol <1.5)) { |
el13eag | 0:969893638914 | 759 | lcd.printString("Brightness=40%",0,3); |
el13eag | 0:969893638914 | 760 | } |
el13eag | 0:969893638914 | 761 | |
el13eag | 0:969893638914 | 762 | if ((vol >= 1.5) && (vol < 1.8)) { |
el13eag | 0:969893638914 | 763 | lcd.printString("Brightness=50%",0,3); |
el13eag | 0:969893638914 | 764 | } |
el13eag | 0:969893638914 | 765 | if ((vol >= 1.8) && (vol <2.1)) { |
el13eag | 0:969893638914 | 766 | lcd.printString("Brightness=60%",0,3); |
el13eag | 0:969893638914 | 767 | } |
el13eag | 0:969893638914 | 768 | if ((vol >= 2.1) && (vol < 2.4)) { |
el13eag | 0:969893638914 | 769 | lcd.printString("Brightness=70%",0,3); |
el13eag | 0:969893638914 | 770 | } |
el13eag | 0:969893638914 | 771 | if ((vol >= 2.4)&& (vol < 2.7)) { |
el13eag | 0:969893638914 | 772 | lcd.printString("Brightness=80%",0,3); |
el13eag | 0:969893638914 | 773 | } |
el13eag | 0:969893638914 | 774 | if ((vol >= 2.7)&& (vol < 3)) { |
el13eag | 0:969893638914 | 775 | lcd.printString("Brightness=90%",0,3); |
el13eag | 0:969893638914 | 776 | } |
el13eag | 0:969893638914 | 777 | if ((vol >= 3.0)&& (vol <=3.3)) { |
el13eag | 0:969893638914 | 778 | lcd.printString("Brightness=100%",0,3); |
el13eag | 0:969893638914 | 779 | } |
el13eag | 0:969893638914 | 780 | } |
el13eag | 0:969893638914 | 781 | |
el13eag | 0:969893638914 | 782 | /** |
el13eag | 0:969893638914 | 783 | @brief Function to draw the battery symbol, though at the time of this project a way of getting the battery life was not included but will be added in the next revision |
el13eag | 0:969893638914 | 784 | */ |
el13eag | 0:969893638914 | 785 | void battery() |
el13eag | 0:969893638914 | 786 | { |
el13eag | 0:969893638914 | 787 | lcd.printString("100%", 23, 5); |
el13eag | 0:969893638914 | 788 | |
el13eag | 0:969893638914 | 789 | for (j = 37; j < 47; j++) { |
el13eag | 0:969893638914 | 790 | lcd.setPixel(3, j); |
el13eag | 0:969893638914 | 791 | } |
el13eag | 0:969893638914 | 792 | for (j = 37; j <47; j++) { |
el13eag | 0:969893638914 | 793 | lcd.setPixel(20, j); |
el13eag | 0:969893638914 | 794 | } |
el13eag | 0:969893638914 | 795 | |
el13eag | 0:969893638914 | 796 | |
el13eag | 0:969893638914 | 797 | for (i = 3; i < 21; i++) { |
el13eag | 0:969893638914 | 798 | lcd.setPixel(i, 37); |
el13eag | 0:969893638914 | 799 | } |
el13eag | 0:969893638914 | 800 | for (i = 3; i <21; i++) { |
el13eag | 0:969893638914 | 801 | lcd.setPixel(i, 46); |
el13eag | 0:969893638914 | 802 | } |
el13eag | 0:969893638914 | 803 | |
el13eag | 0:969893638914 | 804 | |
el13eag | 0:969893638914 | 805 | for (j = 40; j < 44; j++) { |
el13eag | 0:969893638914 | 806 | lcd.setPixel(21, j); |
el13eag | 0:969893638914 | 807 | } |
el13eag | 0:969893638914 | 808 | for (j = 40; j <44; j++) { |
el13eag | 0:969893638914 | 809 | lcd.setPixel(22, j); |
el13eag | 0:969893638914 | 810 | } |
el13eag | 0:969893638914 | 811 | |
el13eag | 0:969893638914 | 812 | lcd.refresh(); |
el13eag | 0:969893638914 | 813 | } |