Im Projekt wurden einige interessante Hard- und Software Möglichkeiten des Arduino umgesetzt.
1. Batteriebetrieb (siehe Schaltung)
2. Ansteuerung des Display TM1638
3. Einlesen eines Drehencoder
4. Soundausgabe auf Piezo Lautsprecher
5. Datenspeicherung im EEPROM des Arduino
Schaltung
Programm Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
/* * Für Adruino Pro or Pro Mini (ATMega 328 5V 16Mhz) * Interne Uhr wird nur als Taktgeber genutzt * * timebeforepoweroff 15 */ #include <EEPROM.h> #include <TimeLib.h> #define PROG_NAME "EIERUHR" // Programm Name #define PROG_VERSION "1.0.0" // Programm Version #define PROG_DATE "17.11.2016" // Programm Datum #define PROG_AUTOR "banto@gmx.net" // Programm Autor const int PinEncoderA = 2; const int PinEncoderB = 3; const int PinEncoderT = 4; const int PinSelfHolder = 5; const int PinPiezo = 9; const int PinTM1637Clk = 11; const int PinTM1637Dio = 10; #include <TM1637Display.h> TM1637Display display(PinTM1637Clk, PinTM1637Dio); /* ---0--- Display Segmente | | 5 1 | | ---6--- | | 4 2 | | ---3--- */ // Segemente einzeln ansprechen uint8_t data[8][4] = { { 0x40, 0x40, 0x40, 0x40 }, { 0x04, 0x04, 0x04, 0x04 }, { 0x08, 0x08, 0x08, 0x08 }, { 0x10, 0x10, 0x10, 0x10 }, { 0x20, 0x20, 0x20, 0x20 }, { 0x01, 0x01, 0x01, 0x01 }, { 0x02, 0x02, 0x02, 0x02 }, { 0x40, 0x40, 0x40, 0x40 }, }; uint8_t data_off[] = { 0x00, 0x00, 0x00, 0x00 }; uint8_t data_on[] = { 0xff, 0xff, 0xff, 0xff }; uint8_t data_Text_EndE[]= { B01111001, B01010100, B01011110, B01111001 }; uint8_t data_Text_AUS[] = { B01110111, B00111110, B01101101, B00000000 }; uint8_t data_Text_on[] = { B01011100, B01010100, B00000000, B00000000 }; uint8_t data_Text_off[] = { B01011100, B01111000, B01111000, B00000000 }; struct PROG { int Minuten; int Sekunden; int secBeforePowerOff; int powerOffTime; int piezoFrequenz; }; PROG eu; #define DEFAULT_POWEROFFTIME 300 #define DEFAULT_SECBEFOREPOWEROFF 15 // 15 Sekunden beepen bis zur Abschaltung #define DEFAULT_PIEZOFREQUENZ 2214 // Maximaler Schalldruck bei Frequenz boolean doppelpunkt = true; char buf[80]; int oldSecond; int counter; int powerOffCounter; ///////////////////////////////////////////////////// void setup() { pinMode(PinEncoderA, INPUT_PULLUP); pinMode(PinEncoderB, INPUT_PULLUP); pinMode(PinEncoderT, INPUT_PULLUP); pinMode(PinPiezo, OUTPUT); pinMode(PinSelfHolder, OUTPUT); digitalWrite(PinSelfHolder, HIGH); // Selbsthaltung EEPROM.get(0,eu); if(eu.powerOffTime <= 0) eu.powerOffTime = DEFAULT_POWEROFFTIME; if(eu.secBeforePowerOff <= 0) eu.secBeforePowerOff = DEFAULT_SECBEFOREPOWEROFF; if(eu.piezoFrequenz <= 0) eu.piezoFrequenz = DEFAULT_PIEZOFREQUENZ; piezo(eu.piezoFrequenz,500); Serial.begin(115200); Serial.println("////////////////////////////////"); Serial.print (" Program..: "); Serial.println(PROG_NAME); Serial.print (" Version..: "); Serial.println(PROG_VERSION); Serial.print (" Autor....: "); Serial.println(PROG_AUTOR); Serial.print (" Erstellt.: "); Serial.println(PROG_DATE); Serial.println("////////////////////////////////"); display.setBrightness(0x0A); // 0x00-0x0F initDisplay(); showParameter(); counter = eu.Minuten*60+eu.Sekunden+eu.secBeforePowerOff; oldSecond=second(); Serial.println("Setup ready"); delay(1000); } ///////////////////////////////////////////////////// void loop() { readSerialInterface(); encoderRead(); //**** COUNTDOWWN **** if (second()!=oldSecond && (eu.Minuten >0 || eu.Sekunden >0) ) { oldSecond=second(); // Zeit anzeigen sprintf(buf,"%02d:%02d counter: %d COUNTER",eu.Minuten,eu.Sekunden,counter); Serial.println(buf); display.showNumberDec(eu.Minuten*100+eu.Sekunden,true); display.setColon(doppelpunkt); doppelpunkt = !doppelpunkt; // Zeit herunter zählen eu.Sekunden--; if (eu.Sekunden < 0) { eu.Sekunden=59; eu.Minuten--; } counter--; piezo(3300,10); } //**** BEEP AUSGEBEN **** if (second() != oldSecond && eu.Minuten == 0 && eu.Sekunden == 0 && counter <= eu.secBeforePowerOff && counter > 0) { sprintf(buf,"%02d:%02d counter: %d BEEP",eu.Minuten,eu.Sekunden,counter); Serial.println(buf); oldSecond=second(); if(counter%2==0) display.setSegments(data_off); else display.setSegments(data_Text_EndE); counter--; piezo(eu.piezoFrequenz,800); } //**** AUSSCHALTEN **** if (second() != oldSecond && eu.Minuten == 0 && eu.Sekunden == 0 && counter == 0) { sprintf(buf,"%02d:%02d counter: %d AUSSCHALTEN",eu.Minuten,eu.Sekunden,counter); Serial.println(buf); display.setSegments(data_off); ausschalten(); } } ////////////////////////////////////////////////////// void encoderRead() { static int val, Val_A, Val_B, Old_Val_A; Old_Val_A = digitalRead(PinEncoderA); while(digitalRead(PinEncoderT)==LOW) { val = eu.Minuten*60+eu.Sekunden; // Wert einlesen Val_A = digitalRead(PinEncoderA); Val_B = digitalRead(PinEncoderB); if (Val_A != Old_Val_A) { if (Val_A == Val_B) { val -= 10; if (val < 0) val = 0; } else { val += 10; if (val > 3600) val = 3600; } Old_Val_A = Val_A; eu.Sekunden = val%60; eu.Minuten = val/60; counter = eu.Minuten*60+eu.Sekunden+eu.secBeforePowerOff; EEPROM.put(0,eu); display.showNumberDec(eu.Minuten*100+eu.Sekunden,true); display.setColon(true); powerOffCounter=0; } else { if(++powerOffCounter > eu.powerOffTime) { display.setSegments(data_Text_AUS); while(digitalRead(PinEncoderT)==LOW){delay(10);} Serial.println(powerOffCounter); powerOffCounter=0; ausschalten(); } delay(10); } } } ////////////////////////////////////////////////////// void piezoTest() { static int val, Val_A, Val_B, Old_Val_A; Old_Val_A = digitalRead(PinEncoderA); val = eu.piezoFrequenz; // Wert einlesen display.setColon(false); while(true) { Val_A = digitalRead(PinEncoderA); Val_B = digitalRead(PinEncoderB); if (Val_A != Old_Val_A) { if (Val_A == Val_B) { val -= 1; } else { val += 1; } Old_Val_A = Val_A; } long t2=1000000/(2*val); digitalWrite(PinPiezo,HIGH); delayMicroseconds(t2); digitalWrite(PinPiezo,LOW); delayMicroseconds(t2); if (digitalRead(PinEncoderT)==LOW) { display.showNumberDec(val); Serial.println(); EEPROM.put(0,eu); delay(1000); } } } ////////////////////////////////////////////////////// void ausschalten() { pinMode(PinEncoderA, INPUT); pinMode(PinEncoderB, INPUT); pinMode(PinEncoderT, INPUT); pinMode(PinPiezo, INPUT); pinMode(13, INPUT); Serial.println("Ausschalten"); digitalWrite(PinSelfHolder, LOW); // AUS } ////////////////////////////////////////////////////// void initDisplay() { // Anzeige-Segmente einzeln ansprechen for (int i=0; i<8; i++) { display.setSegments(data[i]); delay(100); } delay(500); display.setSegments(data_off); } ////////////////////////////////////////////////////// void piezo(int frequenz, int dauer) { // frequenz in Hertz // dauer in ms long t2=1000000/(2*frequenz); int loops = (int)frequenz/(1000.00)*dauer; for (int i=0; i < loops; i++) { digitalWrite(PinPiezo,HIGH); delayMicroseconds(t2); digitalWrite(PinPiezo,LOW); delayMicroseconds(t2); } } ////////////////////////////////////////////////////// void showParameter() { Serial.print("eu.minuten..............: "); Serial.println(eu.Minuten); Serial.print("eu.sekunden.............: "); Serial.println(eu.Sekunden); Serial.print("eu.powerOffTime.........: "); Serial.println(eu.powerOffTime); Serial.print("eu.secBeforePowerOff...: "); Serial.println(eu.secBeforePowerOff); Serial.print("eu.piezoFrequenz........: "); Serial.println(eu.piezoFrequenz); } ////////////////////////////////////////////////////// void readSerialInterface() { String cmd1; String cmd2; if (Serial.available() > 0) { delay (25); // Warten bis alle Zeichen im Puffer String cmd=""; while (Serial.available()> 0) // Alle Zeichen einzeln auslesen { char c=Serial.read(); if (c>=32) cmd=cmd+c; // Nur Zeichen mit ASCII-Code >=32 } boolean found = false; cmd = cmd + " "; String cmd1 = split(cmd,' ',0); String cmd2 = split(cmd,' ',1); if (cmd1=="piezotest") { Serial.print ("PIEZOTEST"); piezoTest(); } if (cmd1=="secbeforepoweroff") { eu.secBeforePowerOff = cmd2.toInt(); EEPROM.put(0,eu); } if (cmd1=="powerofftime") { eu.powerOffTime = cmd2.toInt(); EEPROM.put(0,eu); } showParameter(); } } ////////////////////////////////////////////////////// String split(String data, char separator, int index) { int found = 0; int strIndex[] = {0, -1}; int maxIndex = data.length()-1; for(int i=0; i<=maxIndex && found<=index; i++){ if(data.charAt(i)==separator || i==maxIndex){ found++; strIndex[0] = strIndex[1]+1; strIndex[1] = (i == maxIndex) ? i+1 : i; } } return found>index ? data.substring(strIndex[0], strIndex[1]) : ""; } ////////////////////////////////////////////////////// int readKey(int pin) { static int oldval; static int taster_val = digitalRead(pin); if (taster_val != oldval) { oldval = taster_val; delay(10); } return taster_val; } |