[ nikola1902 @ 29.07.2017. 02:05 ] @
Pozdrav drugari! Pre svega, pocetnik sam u ovome, snalazim se jeeee-dva, pa eto, cisto da to imate na umu :D Zamolio bih vas za pomoc/ Poceo sam da radim jednostavan tajmer, koji ne bi imao nikakav ekrancic, imao bi 4 diodice i jedno dugme za izbor jednog od ta 4 moda (15,30,45,60 minuta). Snasao sam se za case switch koji sam nasao negde na internetu i malo ga modifikovao da pase mojim potrebama, sada mi treba konkretno da ugradim tajmer u sve to, ali bez modula za sat, znaci, treba sve da obavlja samo Arduino, ali bez delay funkcije koja blokira ceo kod. Dakle, problematika je ovakva: kada izaberem jedan od 4 tajmera, on automatski krece da odbrojava vreme na koje je podesen. Kada vreme istekne, pali alarm. Ako nekim slucajem izaberem drugi mod od pocetnog, automatski resetuje prethodni tajmer dok ga ponovo ne izaberem. Jako je jednostavno, i svestan sam da mi je resenje verovatno ispred nosa, pa bih vas molio ako mozete da mi pomognete, bio bih jako zahvalan. Evo dosadasnjeg koda: Code: const int led1 = 2; // LED1 ANODE const int led2 = 3; const int led3 = 4; // LED2 ANODE const int led4 = 5; // LED3 ANODE const int alarm = 6; const int sw1 = 7; // Active HIGH, held low by 4.7K int mode = 0; // Selector State (Initial state = ALL OFF) int val = 0; // Pin 13 HIGH/LOW Status int butState = 0; // Last Button State int modeState = 0; // Last Mode State boolean debug = 1; // 1 = Print Serial Enabled / 0 = disabled //=============================================================== // SETUP //=============================================================== void setup () { pinMode(led1, OUTPUT); pinMode(led2, OUTPUT); pinMode(led3, OUTPUT); pinMode(led4, OUTPUT); pinMode(alarm, OUTPUT); pinMode(sw1, INPUT); if (debug){ Serial.begin(9600); Serial.print("Initial Mode: "); Serial.println(mode); Serial.print("Setup Complete\n"); } } //=============================================================== // Main Loop //=============================================================== void loop() { val = digitalRead(sw1); // If we see a change in button state, increment mode value if (val != butState && val == HIGH){ mode++; } butState = val; // Keep track of most recent button state // No need to keep setting pins *every* loop if (modeState != mode){ // If no keys have been pressed yet don't execute // the switch code below // if (mode != 0) { switch ( mode ) { //case 1 is actually handled below as default case 2: //second led, it should sound alarm after 10 seconds unless user interrupt digitalWrite(led1, LOW); digitalWrite(led2, HIGH); //hold second led high digitalWrite(led3, LOW); digitalWrite(led4, LOW); showState(); break; case 3: digitalWrite(led1, LOW); digitalWrite(led2, LOW); digitalWrite(led3, HIGH); digitalWrite(led4, LOW); showState(); break; case 4: digitalWrite(led1, LOW); digitalWrite(led2, LOW); digitalWrite(led3, LOW); digitalWrite(led4, HIGH); showState(); break; default: mode = 1; // loop back to 1 by default, seems redundant but // it also handles the "mode is > 3" problem digitalWrite(led1, HIGH); digitalWrite(led2, LOW); digitalWrite(led3, LOW); digitalWrite(led4, LOW); showState(); break; } // end switch // } // end of "if mode = 0" check } // end of ModeState check modeState = mode; // Keep track of mode recent mode value delay(20); // slow the loop just a bit for debounce } //=============================================================== // Subroutine //=============================================================== void showState() { if (debug){ Serial.print("Mode: "); Serial.println(mode); } } |