ultrasonic distance sensor - time headerfile
Dependencies: N5110 PowerControl SRF02 beep mbed
Fork of Ultrasonicdistancesensor by
ultrasonic.cpp@1:920749af1300, 2015-05-05 (annotated)
- Committer:
- probrania7
- Date:
- Tue May 05 03:08:14 2015 +0000
- Revision:
- 1:920749af1300
- Parent:
- 0:7a82ad48df33
- Child:
- 2:184524f95eb1
ver2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
probrania7 | 0:7a82ad48df33 | 1 | #include "mbed.h" |
probrania7 | 0:7a82ad48df33 | 2 | #include "N5110.h" |
probrania7 | 0:7a82ad48df33 | 3 | #include "beep.h" |
probrania7 | 0:7a82ad48df33 | 4 | #include "SRF02.h" |
probrania7 | 0:7a82ad48df33 | 5 | #include "timeMgr.h" |
probrania7 | 0:7a82ad48df33 | 6 | |
probrania7 | 0:7a82ad48df33 | 7 | |
probrania7 | 0:7a82ad48df33 | 8 | // VCC,SCE,RST,D/C,MOSI,SCLK,LED |
probrania7 | 0:7a82ad48df33 | 9 | N5110 lcd(p7,p8,p9,p10,p11,p13,p26); |
probrania7 | 0:7a82ad48df33 | 10 | // Can also power (VCC) directly from VOUT (3.3 V) - |
probrania7 | 0:7a82ad48df33 | 11 | // Can give better performance due to current limitation from GPIO pin |
probrania7 | 0:7a82ad48df33 | 12 | |
probrania7 | 0:7a82ad48df33 | 13 | // SDA, SCL |
probrania7 | 0:7a82ad48df33 | 14 | SRF02 sensor(p28,p27); |
probrania7 | 0:7a82ad48df33 | 15 | |
probrania7 | 0:7a82ad48df33 | 16 | Beep beep1(p21); |
probrania7 | 0:7a82ad48df33 | 17 | AnalogIn potIn (p20); |
probrania7 | 0:7a82ad48df33 | 18 | |
probrania7 | 0:7a82ad48df33 | 19 | DigitalOut Led1 (p24); |
probrania7 | 0:7a82ad48df33 | 20 | DigitalOut Led2 (p14); |
probrania7 | 0:7a82ad48df33 | 21 | DigitalOut Led3 (p25); |
probrania7 | 0:7a82ad48df33 | 22 | DigitalOut Led4 (p23); |
probrania7 | 0:7a82ad48df33 | 23 | //========================= |
probrania7 | 0:7a82ad48df33 | 24 | InterruptIn button1 (p17); |
probrania7 | 0:7a82ad48df33 | 25 | InterruptIn button2 (p29); |
probrania7 | 0:7a82ad48df33 | 26 | InterruptIn button3 (p30); |
probrania7 | 0:7a82ad48df33 | 27 | |
probrania7 | 1:920749af1300 | 28 | |
probrania7 | 1:920749af1300 | 29 | |
probrania7 | 0:7a82ad48df33 | 30 | |
probrania7 | 0:7a82ad48df33 | 31 | //=============================== |
probrania7 | 0:7a82ad48df33 | 32 | int button1Flag = 0; |
probrania7 | 0:7a82ad48df33 | 33 | int button2Flag = 0; |
probrania7 | 0:7a82ad48df33 | 34 | int button3Flag = 0; |
probrania7 | 0:7a82ad48df33 | 35 | int msgFlag = 1; |
probrania7 | 0:7a82ad48df33 | 36 | |
probrania7 | 0:7a82ad48df33 | 37 | //function declarations |
probrania7 | 0:7a82ad48df33 | 38 | //========================= |
probrania7 | 0:7a82ad48df33 | 39 | void introMessage(); |
probrania7 | 0:7a82ad48df33 | 40 | void endMessage(); |
probrania7 | 0:7a82ad48df33 | 41 | void errorHandler(); |
probrania7 | 0:7a82ad48df33 | 42 | void menu(); |
probrania7 | 0:7a82ad48df33 | 43 | void demo(); |
probrania7 | 0:7a82ad48df33 | 44 | void cursorSelect(int, int, int, int); |
probrania7 | 0:7a82ad48df33 | 45 | void cursorMove(int); |
probrania7 | 0:7a82ad48df33 | 46 | void beeper(float, float); |
probrania7 | 0:7a82ad48df33 | 47 | void introMessageTimer(); |
probrania7 | 0:7a82ad48df33 | 48 | void menuLevel(float); |
probrania7 | 0:7a82ad48df33 | 49 | void startMessage(); |
probrania7 | 0:7a82ad48df33 | 50 | void displayClock(); |
probrania7 | 0:7a82ad48df33 | 51 | //==================================== |
probrania7 | 0:7a82ad48df33 | 52 | void distMeasure(); |
probrania7 | 0:7a82ad48df33 | 53 | void saveData(); |
probrania7 | 0:7a82ad48df33 | 54 | void adjustBrightness(float); |
probrania7 | 0:7a82ad48df33 | 55 | void systemTest(); |
probrania7 | 0:7a82ad48df33 | 56 | //------------------------ |
probrania7 | 0:7a82ad48df33 | 57 | void button1Pressed(); |
probrania7 | 0:7a82ad48df33 | 58 | void button2Pressed(); |
probrania7 | 0:7a82ad48df33 | 59 | void button3Pressed(); |
probrania7 | 0:7a82ad48df33 | 60 | //Global variables initialisation |
probrania7 | 0:7a82ad48df33 | 61 | //================================= |
probrania7 | 0:7a82ad48df33 | 62 | //Timer introTimer; |
probrania7 | 0:7a82ad48df33 | 63 | |
probrania7 | 0:7a82ad48df33 | 64 | int menuLevelFlag = 0; |
probrania7 | 0:7a82ad48df33 | 65 | int brightFlag = 0; |
probrania7 | 0:7a82ad48df33 | 66 | int distFlag = 0; |
probrania7 | 0:7a82ad48df33 | 67 | int clockFlag = 0; |
probrania7 | 0:7a82ad48df33 | 68 | //------------------------------ |
probrania7 | 0:7a82ad48df33 | 69 | |
probrania7 | 1:920749af1300 | 70 | Ticker distTimer; |
probrania7 | 1:920749af1300 | 71 | int dstFlg = 0; |
probrania7 | 1:920749af1300 | 72 | void distChk() |
probrania7 | 1:920749af1300 | 73 | { |
probrania7 | 1:920749af1300 | 74 | dstFlg = 1; |
probrania7 | 1:920749af1300 | 75 | } |
probrania7 | 1:920749af1300 | 76 | |
probrania7 | 1:920749af1300 | 77 | |
probrania7 | 1:920749af1300 | 78 | Ticker clkTimer; |
probrania7 | 1:920749af1300 | 79 | int clkFlg = 0; |
probrania7 | 1:920749af1300 | 80 | void clkChk() |
probrania7 | 1:920749af1300 | 81 | { |
probrania7 | 1:920749af1300 | 82 | clkFlg = 1; |
probrania7 | 1:920749af1300 | 83 | } |
probrania7 | 1:920749af1300 | 84 | |
probrania7 | 1:920749af1300 | 85 | |
probrania7 | 0:7a82ad48df33 | 86 | |
probrania7 | 0:7a82ad48df33 | 87 | int main() |
probrania7 | 0:7a82ad48df33 | 88 | { |
probrania7 | 0:7a82ad48df33 | 89 | // first need to initialise display |
probrania7 | 0:7a82ad48df33 | 90 | lcd.init(); |
probrania7 | 0:7a82ad48df33 | 91 | |
probrania7 | 0:7a82ad48df33 | 92 | // button2.rise(&button2Pressed); |
probrania7 | 0:7a82ad48df33 | 93 | |
probrania7 | 1:920749af1300 | 94 | //beeper(1000, 0.5); |
probrania7 | 0:7a82ad48df33 | 95 | |
probrania7 | 0:7a82ad48df33 | 96 | serial.attach(&serialISR); // attach serial ISR |
probrania7 | 1:920749af1300 | 97 | timer2.attach(&timer2Expired,1.0); |
probrania7 | 0:7a82ad48df33 | 98 | |
probrania7 | 0:7a82ad48df33 | 99 | set_time(1424535031); // initialise time to 1st January 1970 |
probrania7 | 0:7a82ad48df33 | 100 | |
probrania7 | 0:7a82ad48df33 | 101 | |
probrania7 | 0:7a82ad48df33 | 102 | introMessage(); |
probrania7 | 0:7a82ad48df33 | 103 | |
probrania7 | 0:7a82ad48df33 | 104 | button1.rise(&button1Pressed); |
probrania7 | 0:7a82ad48df33 | 105 | button3.rise(&button3Pressed); |
probrania7 | 0:7a82ad48df33 | 106 | //----------------------------------------------------- |
probrania7 | 0:7a82ad48df33 | 107 | |
probrania7 | 0:7a82ad48df33 | 108 | //----------------------------------------------------- |
probrania7 | 1:920749af1300 | 109 | distTimer.attach(&distChk, 1.0); //call ISR every 5 sec |
probrania7 | 1:920749af1300 | 110 | clkTimer.attach(&clkChk, 1.0); //call ISR every 5 sec |
probrania7 | 0:7a82ad48df33 | 111 | while(1) |
probrania7 | 0:7a82ad48df33 | 112 | { |
probrania7 | 0:7a82ad48df33 | 113 | if (msgFlag) |
probrania7 | 0:7a82ad48df33 | 114 | { |
probrania7 | 0:7a82ad48df33 | 115 | startMessage(); |
probrania7 | 0:7a82ad48df33 | 116 | } |
probrania7 | 0:7a82ad48df33 | 117 | |
probrania7 | 0:7a82ad48df33 | 118 | if (button1Flag) |
probrania7 | 0:7a82ad48df33 | 119 | { |
probrania7 | 0:7a82ad48df33 | 120 | msgFlag=0; |
probrania7 | 0:7a82ad48df33 | 121 | menu(); |
probrania7 | 0:7a82ad48df33 | 122 | menuLevelFlag = 1; |
probrania7 | 0:7a82ad48df33 | 123 | } |
probrania7 | 0:7a82ad48df33 | 124 | |
probrania7 | 0:7a82ad48df33 | 125 | if (menuLevelFlag) |
probrania7 | 0:7a82ad48df33 | 126 | { |
probrania7 | 0:7a82ad48df33 | 127 | button1Flag =0; |
probrania7 | 1:920749af1300 | 128 | menuLevel(potIn); |
probrania7 | 0:7a82ad48df33 | 129 | } |
probrania7 | 0:7a82ad48df33 | 130 | |
probrania7 | 1:920749af1300 | 131 | if (distFlag) |
probrania7 | 0:7a82ad48df33 | 132 | { |
probrania7 | 0:7a82ad48df33 | 133 | menuLevelFlag = 0; |
probrania7 | 1:920749af1300 | 134 | if (dstFlg) |
probrania7 | 0:7a82ad48df33 | 135 | { |
probrania7 | 1:920749af1300 | 136 | dstFlg = 0; |
probrania7 | 1:920749af1300 | 137 | distMeasure(); |
probrania7 | 1:920749af1300 | 138 | if(button1) |
probrania7 | 1:920749af1300 | 139 | { |
probrania7 | 1:920749af1300 | 140 | distFlag = 0; |
probrania7 | 1:920749af1300 | 141 | } |
probrania7 | 1:920749af1300 | 142 | } |
probrania7 | 0:7a82ad48df33 | 143 | } |
probrania7 | 0:7a82ad48df33 | 144 | |
probrania7 | 0:7a82ad48df33 | 145 | if (brightFlag) |
probrania7 | 0:7a82ad48df33 | 146 | { |
probrania7 | 0:7a82ad48df33 | 147 | menuLevelFlag = 0; |
probrania7 | 1:920749af1300 | 148 | |
probrania7 | 1:920749af1300 | 149 | |
probrania7 | 1:920749af1300 | 150 | adjustBrightness(potIn); |
probrania7 | 1:920749af1300 | 151 | if (button1) |
probrania7 | 1:920749af1300 | 152 | { |
probrania7 | 1:920749af1300 | 153 | brightFlag = 0; |
probrania7 | 1:920749af1300 | 154 | } |
probrania7 | 1:920749af1300 | 155 | |
probrania7 | 0:7a82ad48df33 | 156 | } |
probrania7 | 0:7a82ad48df33 | 157 | if (clockFlag) |
probrania7 | 0:7a82ad48df33 | 158 | { |
probrania7 | 0:7a82ad48df33 | 159 | menuLevelFlag = 0; |
probrania7 | 1:920749af1300 | 160 | if (clkFlg) |
probrania7 | 0:7a82ad48df33 | 161 | { |
probrania7 | 1:920749af1300 | 162 | clkFlg = 0; |
probrania7 | 1:920749af1300 | 163 | displayClock(); |
probrania7 | 1:920749af1300 | 164 | if (button1) |
probrania7 | 1:920749af1300 | 165 | { |
probrania7 | 1:920749af1300 | 166 | clockFlag = 0; |
probrania7 | 1:920749af1300 | 167 | } |
probrania7 | 1:920749af1300 | 168 | } |
probrania7 | 1:920749af1300 | 169 | } |
probrania7 | 1:920749af1300 | 170 | |
probrania7 | 1:920749af1300 | 171 | |
probrania7 | 0:7a82ad48df33 | 172 | } |
probrania7 | 0:7a82ad48df33 | 173 | } |
probrania7 | 0:7a82ad48df33 | 174 | |
probrania7 | 0:7a82ad48df33 | 175 | void introMessage() |
probrania7 | 0:7a82ad48df33 | 176 | { |
probrania7 | 0:7a82ad48df33 | 177 | //introducto text a |
probrania7 | 0:7a82ad48df33 | 178 | lcd.drawLine(4, 0, 79, 0, 1); |
probrania7 | 0:7a82ad48df33 | 179 | lcd.printString("Ultrasonic",13,1); |
probrania7 | 0:7a82ad48df33 | 180 | lcd.printString("Distance",18,2); |
probrania7 | 0:7a82ad48df33 | 181 | lcd.printString("Sensor",23,3); |
probrania7 | 0:7a82ad48df33 | 182 | lcd.drawLine(4, 45, 79, 45, 1); |
probrania7 | 0:7a82ad48df33 | 183 | |
probrania7 | 0:7a82ad48df33 | 184 | wait(1.3); |
probrania7 | 0:7a82ad48df33 | 185 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 186 | |
probrania7 | 0:7a82ad48df33 | 187 | //introductory text b |
probrania7 | 0:7a82ad48df33 | 188 | lcd.drawLine(4, 0, 79, 0, 1); |
probrania7 | 0:7a82ad48df33 | 189 | lcd.printString("University",13,1); |
probrania7 | 0:7a82ad48df33 | 190 | lcd.printString("Of",35,2); |
probrania7 | 0:7a82ad48df33 | 191 | lcd.printString("Leeds",26,3); |
probrania7 | 0:7a82ad48df33 | 192 | lcd.drawLine(4, 45, 79, 45, 1); |
probrania7 | 0:7a82ad48df33 | 193 | |
probrania7 | 0:7a82ad48df33 | 194 | wait(1.3); |
probrania7 | 0:7a82ad48df33 | 195 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 196 | |
probrania7 | 0:7a82ad48df33 | 197 | //introductory text c |
probrania7 | 0:7a82ad48df33 | 198 | lcd.drawLine(4, 0, 79, 0, 1); |
probrania7 | 0:7a82ad48df33 | 199 | lcd.printString("Ibrahim Adam",5,1); |
probrania7 | 0:7a82ad48df33 | 200 | lcd.printString("200789110",10,2); |
probrania7 | 0:7a82ad48df33 | 201 | lcd.printString("Yr2 - 2015",8,3); |
probrania7 | 0:7a82ad48df33 | 202 | lcd.drawLine(4, 45, 79, 45, 1); |
probrania7 | 0:7a82ad48df33 | 203 | |
probrania7 | 0:7a82ad48df33 | 204 | wait(1.3); |
probrania7 | 0:7a82ad48df33 | 205 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 206 | |
probrania7 | 0:7a82ad48df33 | 207 | //introductory text c |
probrania7 | 0:7a82ad48df33 | 208 | lcd.drawLine(4, 0, 79, 0, 1); |
probrania7 | 0:7a82ad48df33 | 209 | lcd.printString("Elec 2645",15,1); |
probrania7 | 0:7a82ad48df33 | 210 | lcd.printString("Embedded SYS",8,2); |
probrania7 | 0:7a82ad48df33 | 211 | lcd.printString("Project",21,3); |
probrania7 | 0:7a82ad48df33 | 212 | lcd.drawLine(4, 45, 79, 45, 1); |
probrania7 | 0:7a82ad48df33 | 213 | |
probrania7 | 0:7a82ad48df33 | 214 | msgFlag = 1; |
probrania7 | 0:7a82ad48df33 | 215 | |
probrania7 | 0:7a82ad48df33 | 216 | wait(1.3); |
probrania7 | 0:7a82ad48df33 | 217 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 218 | } |
probrania7 | 0:7a82ad48df33 | 219 | |
probrania7 | 0:7a82ad48df33 | 220 | void mainMenu() |
probrania7 | 0:7a82ad48df33 | 221 | { |
probrania7 | 0:7a82ad48df33 | 222 | |
probrania7 | 0:7a82ad48df33 | 223 | } |
probrania7 | 0:7a82ad48df33 | 224 | |
probrania7 | 0:7a82ad48df33 | 225 | void endMessage() |
probrania7 | 0:7a82ad48df33 | 226 | { |
probrania7 | 0:7a82ad48df33 | 227 | //end text |
probrania7 | 0:7a82ad48df33 | 228 | lcd.drawLine(4, 0, 79, 0, 1); |
probrania7 | 0:7a82ad48df33 | 229 | lcd.printString("Thank You",12,1); |
probrania7 | 0:7a82ad48df33 | 230 | lcd.printString("Goodbye",19,2); |
probrania7 | 0:7a82ad48df33 | 231 | |
probrania7 | 0:7a82ad48df33 | 232 | lcd.drawLine(4, 45, 79, 45, 1); |
probrania7 | 0:7a82ad48df33 | 233 | |
probrania7 | 0:7a82ad48df33 | 234 | wait(1); |
probrania7 | 0:7a82ad48df33 | 235 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 236 | } |
probrania7 | 0:7a82ad48df33 | 237 | |
probrania7 | 0:7a82ad48df33 | 238 | void errorHandler() |
probrania7 | 0:7a82ad48df33 | 239 | { |
probrania7 | 0:7a82ad48df33 | 240 | //end text |
probrania7 | 0:7a82ad48df33 | 241 | lcd.drawLine(4, 0, 79, 0, 1); |
probrania7 | 0:7a82ad48df33 | 242 | lcd.printString("Error Help",12,1); |
probrania7 | 0:7a82ad48df33 | 243 | |
probrania7 | 0:7a82ad48df33 | 244 | |
probrania7 | 0:7a82ad48df33 | 245 | wait(0.2); |
probrania7 | 0:7a82ad48df33 | 246 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 247 | } |
probrania7 | 0:7a82ad48df33 | 248 | |
probrania7 | 0:7a82ad48df33 | 249 | void adjustBrightness(float x) |
probrania7 | 0:7a82ad48df33 | 250 | { |
probrania7 | 0:7a82ad48df33 | 251 | if ((x >= 0) && (x < 0.2)) |
probrania7 | 0:7a82ad48df33 | 252 | { |
probrania7 | 1:920749af1300 | 253 | |
probrania7 | 0:7a82ad48df33 | 254 | lcd.setBrightness(0); |
probrania7 | 0:7a82ad48df33 | 255 | lcd.printString("Light 0%",13,1); |
probrania7 | 0:7a82ad48df33 | 256 | } |
probrania7 | 0:7a82ad48df33 | 257 | |
probrania7 | 0:7a82ad48df33 | 258 | if ((x >= 0.2) && (x < 0.4)) |
probrania7 | 0:7a82ad48df33 | 259 | { |
probrania7 | 1:920749af1300 | 260 | |
probrania7 | 0:7a82ad48df33 | 261 | lcd.setBrightness(0.25); |
probrania7 | 0:7a82ad48df33 | 262 | lcd.printString("Light 25%",13,1); |
probrania7 | 0:7a82ad48df33 | 263 | } |
probrania7 | 0:7a82ad48df33 | 264 | |
probrania7 | 0:7a82ad48df33 | 265 | if ((x >= 0.4) && (x < 0.6)) |
probrania7 | 0:7a82ad48df33 | 266 | { |
probrania7 | 1:920749af1300 | 267 | |
probrania7 | 0:7a82ad48df33 | 268 | lcd.setBrightness(0.5); |
probrania7 | 0:7a82ad48df33 | 269 | lcd.printString("Light 50%",13,1); |
probrania7 | 0:7a82ad48df33 | 270 | } |
probrania7 | 0:7a82ad48df33 | 271 | |
probrania7 | 0:7a82ad48df33 | 272 | if ((x >= 0.6) && (x < 0.8)) |
probrania7 | 0:7a82ad48df33 | 273 | { |
probrania7 | 1:920749af1300 | 274 | |
probrania7 | 0:7a82ad48df33 | 275 | lcd.setBrightness(0.75); |
probrania7 | 0:7a82ad48df33 | 276 | lcd.printString("Light 75%",13,1); |
probrania7 | 0:7a82ad48df33 | 277 | |
probrania7 | 0:7a82ad48df33 | 278 | } |
probrania7 | 0:7a82ad48df33 | 279 | if (x >= 0.8) |
probrania7 | 0:7a82ad48df33 | 280 | { |
probrania7 | 1:920749af1300 | 281 | |
probrania7 | 0:7a82ad48df33 | 282 | lcd.setBrightness(1); |
probrania7 | 0:7a82ad48df33 | 283 | lcd.printString("Light 100%",13,1); |
probrania7 | 0:7a82ad48df33 | 284 | } |
probrania7 | 1:920749af1300 | 285 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 286 | } |
probrania7 | 0:7a82ad48df33 | 287 | |
probrania7 | 0:7a82ad48df33 | 288 | void menu() |
probrania7 | 0:7a82ad48df33 | 289 | { |
probrania7 | 0:7a82ad48df33 | 290 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 291 | lcd.printString("-MAIN MENU-",9,0); |
probrania7 | 0:7a82ad48df33 | 292 | lcd.drawLine(4, 7, 79, 7, 1); |
probrania7 | 0:7a82ad48df33 | 293 | lcd.printString("Dist read",15,1); |
probrania7 | 0:7a82ad48df33 | 294 | lcd.printString("Save data",15,2); |
probrania7 | 0:7a82ad48df33 | 295 | lcd.printString("brightness",15,3); |
probrania7 | 0:7a82ad48df33 | 296 | lcd.printString("SYS test",15,4); |
probrania7 | 0:7a82ad48df33 | 297 | } |
probrania7 | 0:7a82ad48df33 | 298 | |
probrania7 | 0:7a82ad48df33 | 299 | void cursorSelect(int xDimm, int xLimm, int yDimm, int yLimm) |
probrania7 | 0:7a82ad48df33 | 300 | { |
probrania7 | 0:7a82ad48df33 | 301 | int i,j; |
probrania7 | 0:7a82ad48df33 | 302 | |
probrania7 | 0:7a82ad48df33 | 303 | //clear selecton column |
probrania7 | 0:7a82ad48df33 | 304 | for (i = 1; i < 6; i++) |
probrania7 | 0:7a82ad48df33 | 305 | { |
probrania7 | 0:7a82ad48df33 | 306 | for(j = 1; j < 46; j++) |
probrania7 | 0:7a82ad48df33 | 307 | { |
probrania7 | 0:7a82ad48df33 | 308 | lcd.clearPixel(i,j); |
probrania7 | 0:7a82ad48df33 | 309 | } |
probrania7 | 0:7a82ad48df33 | 310 | } |
probrania7 | 0:7a82ad48df33 | 311 | |
probrania7 | 0:7a82ad48df33 | 312 | //particular selection |
probrania7 | 0:7a82ad48df33 | 313 | for (i = xDimm; i < xLimm; i++) |
probrania7 | 0:7a82ad48df33 | 314 | { |
probrania7 | 0:7a82ad48df33 | 315 | for(j = yDimm; j < yLimm; j++) |
probrania7 | 0:7a82ad48df33 | 316 | { |
probrania7 | 0:7a82ad48df33 | 317 | lcd.setPixel(i,j); |
probrania7 | 0:7a82ad48df33 | 318 | } |
probrania7 | 0:7a82ad48df33 | 319 | } |
probrania7 | 0:7a82ad48df33 | 320 | lcd.refresh(); |
probrania7 | 0:7a82ad48df33 | 321 | } |
probrania7 | 0:7a82ad48df33 | 322 | |
probrania7 | 0:7a82ad48df33 | 323 | void demo() |
probrania7 | 0:7a82ad48df33 | 324 | { |
probrania7 | 0:7a82ad48df33 | 325 | // these are default settings so not strictly needed |
probrania7 | 0:7a82ad48df33 | 326 | lcd.normalMode(); // normal colour mode |
probrania7 | 0:7a82ad48df33 | 327 | lcd.setBrightness(0.5); // put LED backlight on 50% |
probrania7 | 0:7a82ad48df33 | 328 | |
probrania7 | 0:7a82ad48df33 | 329 | // can directly print strings at specified co-ordinates |
probrania7 | 0:7a82ad48df33 | 330 | lcd.printString("Hello, World!",0,0); |
probrania7 | 0:7a82ad48df33 | 331 | |
probrania7 | 0:7a82ad48df33 | 332 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
probrania7 | 0:7a82ad48df33 | 333 | // so can display a string of a maximum 14 characters in length |
probrania7 | 0:7a82ad48df33 | 334 | // or create formatted strings - ensure they aren't more than 14 characters long |
probrania7 | 0:7a82ad48df33 | 335 | int temperature = 27; |
probrania7 | 0:7a82ad48df33 | 336 | int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer |
probrania7 | 0:7a82ad48df33 | 337 | // it is important the format specifier ensures the length will fit in the buffer |
probrania7 | 0:7a82ad48df33 | 338 | if (length <= 14) // if string will fit on display |
probrania7 | 0:7a82ad48df33 | 339 | lcd.printString(buffer,0,1); // display on screen |
probrania7 | 0:7a82ad48df33 | 340 | |
probrania7 | 0:7a82ad48df33 | 341 | float pressure = 1012.3; // same idea with floats |
probrania7 | 0:7a82ad48df33 | 342 | length = sprintf(buffer,"P = %.2f mb",pressure); |
probrania7 | 0:7a82ad48df33 | 343 | if (length <= 14) |
probrania7 | 0:7a82ad48df33 | 344 | lcd.printString(buffer,0,2); |
probrania7 | 0:7a82ad48df33 | 345 | |
probrania7 | 0:7a82ad48df33 | 346 | // can also print individual characters at specified place |
probrania7 | 0:7a82ad48df33 | 347 | lcd.printChar('X',5,3); |
probrania7 | 0:7a82ad48df33 | 348 | |
probrania7 | 0:7a82ad48df33 | 349 | // draw a line across the display at y = 40 pixels (origin top-left) |
probrania7 | 0:7a82ad48df33 | 350 | for (int i = 0; i < WIDTH; i++) |
probrania7 | 0:7a82ad48df33 | 351 | { |
probrania7 | 0:7a82ad48df33 | 352 | lcd.setPixel(i,40); |
probrania7 | 0:7a82ad48df33 | 353 | } |
probrania7 | 0:7a82ad48df33 | 354 | // need to refresh display after setting pixels |
probrania7 | 0:7a82ad48df33 | 355 | lcd.refresh(); |
probrania7 | 0:7a82ad48df33 | 356 | |
probrania7 | 0:7a82ad48df33 | 357 | // can also check status of pixels using getPixel(x,y) |
probrania7 | 0:7a82ad48df33 | 358 | |
probrania7 | 0:7a82ad48df33 | 359 | wait(5.0); |
probrania7 | 0:7a82ad48df33 | 360 | lcd.clear(); // clear display |
probrania7 | 0:7a82ad48df33 | 361 | lcd.inverseMode(); // invert colours |
probrania7 | 0:7a82ad48df33 | 362 | lcd.setBrightness(1.0); // put LED backlight on full |
probrania7 | 0:7a82ad48df33 | 363 | |
probrania7 | 0:7a82ad48df33 | 364 | float array[84]; |
probrania7 | 0:7a82ad48df33 | 365 | |
probrania7 | 0:7a82ad48df33 | 366 | for (int i = 0; i < 84; i++) |
probrania7 | 0:7a82ad48df33 | 367 | { |
probrania7 | 0:7a82ad48df33 | 368 | array[i] = 0.5 + 0.5*sin(i*2*3.14/84); |
probrania7 | 0:7a82ad48df33 | 369 | } |
probrania7 | 0:7a82ad48df33 | 370 | |
probrania7 | 0:7a82ad48df33 | 371 | // can also plot graphs - 84 elements only |
probrania7 | 0:7a82ad48df33 | 372 | // values must be in range 0.0 - 1.0 |
probrania7 | 0:7a82ad48df33 | 373 | lcd.plotArray(array); |
probrania7 | 0:7a82ad48df33 | 374 | wait(5.0); |
probrania7 | 0:7a82ad48df33 | 375 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 376 | lcd.normalMode(); // normal colour mode back |
probrania7 | 0:7a82ad48df33 | 377 | lcd.setBrightness(0.5); // put LED backlight on 50% |
probrania7 | 0:7a82ad48df33 | 378 | |
probrania7 | 0:7a82ad48df33 | 379 | // example of drawing lines |
probrania7 | 0:7a82ad48df33 | 380 | for (int x = 0; x < WIDTH ; x+=10) |
probrania7 | 0:7a82ad48df33 | 381 | { |
probrania7 | 0:7a82ad48df33 | 382 | // x0,y0,x1,y1,type 0-white,1-black,2-dotted |
probrania7 | 0:7a82ad48df33 | 383 | lcd.drawLine(0,0,x,HEIGHT,2); |
probrania7 | 0:7a82ad48df33 | 384 | } |
probrania7 | 0:7a82ad48df33 | 385 | |
probrania7 | 0:7a82ad48df33 | 386 | wait(5.0); |
probrania7 | 0:7a82ad48df33 | 387 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 388 | |
probrania7 | 0:7a82ad48df33 | 389 | // example of how to draw circles |
probrania7 | 0:7a82ad48df33 | 390 | lcd.drawCircle(WIDTH/2,HEIGHT/2,20,1); // x,y,radius,black fill |
probrania7 | 0:7a82ad48df33 | 391 | lcd.drawCircle(WIDTH/2,HEIGHT/2,10,2); // x,y,radius,white fill |
probrania7 | 0:7a82ad48df33 | 392 | lcd.drawCircle(WIDTH/2,HEIGHT/2,30,0); // x,y,radius,transparent with outline |
probrania7 | 0:7a82ad48df33 | 393 | |
probrania7 | 0:7a82ad48df33 | 394 | wait(5.0); |
probrania7 | 0:7a82ad48df33 | 395 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 396 | |
probrania7 | 0:7a82ad48df33 | 397 | // example of how to draw rectangles |
probrania7 | 0:7a82ad48df33 | 398 | // origin x,y,width,height,type |
probrania7 | 0:7a82ad48df33 | 399 | lcd.drawRect(10,10,50,30,1); // filled black rectangle |
probrania7 | 0:7a82ad48df33 | 400 | lcd.drawRect(15,15,20,10,2); // filled white rectange (no outline) |
probrania7 | 0:7a82ad48df33 | 401 | lcd.drawRect(2,2,70,40,0); // transparent, just outline |
probrania7 | 0:7a82ad48df33 | 402 | |
probrania7 | 0:7a82ad48df33 | 403 | wait(5.0); |
probrania7 | 0:7a82ad48df33 | 404 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 405 | } |
probrania7 | 0:7a82ad48df33 | 406 | |
probrania7 | 0:7a82ad48df33 | 407 | //Interrupt service routines |
probrania7 | 0:7a82ad48df33 | 408 | //=========================== |
probrania7 | 0:7a82ad48df33 | 409 | void button1Pressed() |
probrania7 | 0:7a82ad48df33 | 410 | { |
probrania7 | 0:7a82ad48df33 | 411 | button1Flag = 1; |
probrania7 | 0:7a82ad48df33 | 412 | } |
probrania7 | 0:7a82ad48df33 | 413 | void button2Pressed() |
probrania7 | 0:7a82ad48df33 | 414 | { |
probrania7 | 0:7a82ad48df33 | 415 | button2Flag = 1; |
probrania7 | 0:7a82ad48df33 | 416 | } |
probrania7 | 0:7a82ad48df33 | 417 | void button3Pressed() |
probrania7 | 0:7a82ad48df33 | 418 | { |
probrania7 | 0:7a82ad48df33 | 419 | button3Flag = 1; |
probrania7 | 0:7a82ad48df33 | 420 | } |
probrania7 | 0:7a82ad48df33 | 421 | |
probrania7 | 0:7a82ad48df33 | 422 | //beep sound based on beeper frequency and beeper time |
probrania7 | 0:7a82ad48df33 | 423 | //entered respectively |
probrania7 | 0:7a82ad48df33 | 424 | void beeper(float beeperFreq, float beeperTime) |
probrania7 | 0:7a82ad48df33 | 425 | { |
probrania7 | 0:7a82ad48df33 | 426 | beep1.beep(beeperFreq,beeperTime); |
probrania7 | 0:7a82ad48df33 | 427 | } |
probrania7 | 0:7a82ad48df33 | 428 | |
probrania7 | 0:7a82ad48df33 | 429 | void menuLevel(float lvl) |
probrania7 | 0:7a82ad48df33 | 430 | { |
probrania7 | 0:7a82ad48df33 | 431 | if ((lvl >= 0) && (lvl < 0.25)) |
probrania7 | 0:7a82ad48df33 | 432 | { |
probrania7 | 0:7a82ad48df33 | 433 | cursorSelect(1, 6, 9, 14); |
probrania7 | 0:7a82ad48df33 | 434 | if (button2) |
probrania7 | 0:7a82ad48df33 | 435 | { |
probrania7 | 0:7a82ad48df33 | 436 | distFlag = 1; |
probrania7 | 0:7a82ad48df33 | 437 | brightFlag = 0; |
probrania7 | 0:7a82ad48df33 | 438 | clockFlag = 0; |
probrania7 | 0:7a82ad48df33 | 439 | } |
probrania7 | 0:7a82ad48df33 | 440 | |
probrania7 | 0:7a82ad48df33 | 441 | } |
probrania7 | 0:7a82ad48df33 | 442 | if ((lvl >= 0.25) && (lvl < 0.5)) |
probrania7 | 0:7a82ad48df33 | 443 | { |
probrania7 | 0:7a82ad48df33 | 444 | cursorSelect(1, 6, 17, 22); |
probrania7 | 0:7a82ad48df33 | 445 | } |
probrania7 | 0:7a82ad48df33 | 446 | if ((lvl >= 0.5) && (lvl < 0.75)) |
probrania7 | 0:7a82ad48df33 | 447 | { |
probrania7 | 0:7a82ad48df33 | 448 | cursorSelect(1, 6, 25, 30); |
probrania7 | 0:7a82ad48df33 | 449 | if (button2) |
probrania7 | 0:7a82ad48df33 | 450 | { |
probrania7 | 0:7a82ad48df33 | 451 | brightFlag = 1; |
probrania7 | 0:7a82ad48df33 | 452 | distFlag =0; |
probrania7 | 0:7a82ad48df33 | 453 | clockFlag = 0; |
probrania7 | 0:7a82ad48df33 | 454 | } |
probrania7 | 0:7a82ad48df33 | 455 | |
probrania7 | 0:7a82ad48df33 | 456 | } |
probrania7 | 0:7a82ad48df33 | 457 | if ((lvl >= 0.75) && (lvl <= 1)) |
probrania7 | 0:7a82ad48df33 | 458 | { |
probrania7 | 0:7a82ad48df33 | 459 | cursorSelect(1, 6, 33, 38); |
probrania7 | 0:7a82ad48df33 | 460 | if (button2) |
probrania7 | 0:7a82ad48df33 | 461 | { |
probrania7 | 0:7a82ad48df33 | 462 | clockFlag = 1; |
probrania7 | 0:7a82ad48df33 | 463 | distFlag = 0; |
probrania7 | 0:7a82ad48df33 | 464 | brightFlag = 0; |
probrania7 | 0:7a82ad48df33 | 465 | } |
probrania7 | 0:7a82ad48df33 | 466 | } |
probrania7 | 1:920749af1300 | 467 | |
probrania7 | 0:7a82ad48df33 | 468 | |
probrania7 | 0:7a82ad48df33 | 469 | } |
probrania7 | 0:7a82ad48df33 | 470 | void clockA() |
probrania7 | 0:7a82ad48df33 | 471 | { |
probrania7 | 0:7a82ad48df33 | 472 | lcd.printString("Set clock",35,0); |
probrania7 | 0:7a82ad48df33 | 473 | } |
probrania7 | 0:7a82ad48df33 | 474 | void distMeasure() |
probrania7 | 0:7a82ad48df33 | 475 | { |
probrania7 | 0:7a82ad48df33 | 476 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 477 | lcd.printString("Measure",15,0); |
probrania7 | 0:7a82ad48df33 | 478 | |
probrania7 | 0:7a82ad48df33 | 479 | // read sensor distance in cm and print over serial port |
probrania7 | 0:7a82ad48df33 | 480 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
probrania7 | 0:7a82ad48df33 | 481 | // so can display a string of a maximum 14 characters in length |
probrania7 | 0:7a82ad48df33 | 482 | // or create formatted strings - ensure they aren't more than 14 characters long |
probrania7 | 0:7a82ad48df33 | 483 | int distance = sensor.getDistanceCm(); |
probrania7 | 0:7a82ad48df33 | 484 | int length = sprintf(buffer,"Dist = %2d cm",distance); // print formatted data to buffer |
probrania7 | 0:7a82ad48df33 | 485 | // it is important the format specifier ensures the length will fit in the buffer |
probrania7 | 0:7a82ad48df33 | 486 | if (length <= 14) // if string will fit on display |
probrania7 | 0:7a82ad48df33 | 487 | { |
probrania7 | 0:7a82ad48df33 | 488 | lcd.printString(buffer,0,1); // display on screen |
probrania7 | 0:7a82ad48df33 | 489 | } |
probrania7 | 0:7a82ad48df33 | 490 | |
probrania7 | 0:7a82ad48df33 | 491 | // serial.printf("Distance = %d cm\n",distance); |
probrania7 | 1:920749af1300 | 492 | serial.printf("\n\n\r %s, %0.2f \n",buffer, length); |
probrania7 | 0:7a82ad48df33 | 493 | |
probrania7 | 0:7a82ad48df33 | 494 | } |
probrania7 | 0:7a82ad48df33 | 495 | |
probrania7 | 0:7a82ad48df33 | 496 | void saveData() |
probrania7 | 0:7a82ad48df33 | 497 | { |
probrania7 | 0:7a82ad48df33 | 498 | lcd.printString("Save data",36,0); |
probrania7 | 0:7a82ad48df33 | 499 | } |
probrania7 | 0:7a82ad48df33 | 500 | void systemTest() |
probrania7 | 0:7a82ad48df33 | 501 | { |
probrania7 | 0:7a82ad48df33 | 502 | lcd.printString("System test",30,0); |
probrania7 | 0:7a82ad48df33 | 503 | } |
probrania7 | 0:7a82ad48df33 | 504 | void startMessage() |
probrania7 | 0:7a82ad48df33 | 505 | { |
probrania7 | 0:7a82ad48df33 | 506 | lcd.printString("Press",25,2); |
probrania7 | 0:7a82ad48df33 | 507 | lcd.printString("Start Button",8,3); |
probrania7 | 0:7a82ad48df33 | 508 | //displayClock(); |
probrania7 | 0:7a82ad48df33 | 509 | |
probrania7 | 0:7a82ad48df33 | 510 | } |
probrania7 | 0:7a82ad48df33 | 511 | void displayClock() |
probrania7 | 0:7a82ad48df33 | 512 | { |
probrania7 | 1:920749af1300 | 513 | //timer2.attach(&timer2Expired,1.0); |
probrania7 | 1:920749af1300 | 514 | //serial.attach(&serialISR); // attach serial ISR |
probrania7 | 0:7a82ad48df33 | 515 | lcd.clear(); |
probrania7 | 0:7a82ad48df33 | 516 | |
probrania7 | 0:7a82ad48df33 | 517 | lcd.printString("-SYS CLOCK-",9,0); |
probrania7 | 0:7a82ad48df33 | 518 | |
probrania7 | 0:7a82ad48df33 | 519 | if (timer2Flag) |
probrania7 | 0:7a82ad48df33 | 520 | { |
probrania7 | 0:7a82ad48df33 | 521 | getCurrentTime(); |
probrania7 | 0:7a82ad48df33 | 522 | } |
probrania7 | 0:7a82ad48df33 | 523 | |
probrania7 | 0:7a82ad48df33 | 524 | if (setTimeFlag) |
probrania7 | 0:7a82ad48df33 | 525 | { |
probrania7 | 0:7a82ad48df33 | 526 | chkUpdateTime(); |
probrania7 | 0:7a82ad48df33 | 527 | } |
probrania7 | 0:7a82ad48df33 | 528 | |
probrania7 | 0:7a82ad48df33 | 529 | lcd.printString(buffer2,4,2); // display on screen |
probrania7 | 0:7a82ad48df33 | 530 | } |