Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned int buzzer = 6;
- int relayPin = 5;
- unsigned long int runTime;
- unsigned long int restTime;
- unsigned int val = 0;
- unsigned long previousMillisRun = 0;
- unsigned long previousMillisRest = 0;
- boolean pwrOn = true;
- unsigned long myMillis;
- void setup() {
- pinMode(relayPin, OUTPUT);
- digitalWrite(relayPin, HIGH);
- }
- void loop() {
- runTime = 1000;
- restTime = 1000;
- // you forgot this.
- unsigned long myMillis = millis();
- // this code runs when millis reaches 1000 (or more)
- // because previousMillisRun is set to 0, and runTime is 1000.
- if (myMillis - previousMillisRun >= runTime && pwrOn == true)
- {
- // you store 1000
- previousMillisRun = myMillis;
- // turn the relay off
- digitalWrite(relayPin, LOW);
- // now you set your state variable to the opposite.
- pwrOn = false;
- }
- // guess what, this code executes IMMEDIATELY after the previous if-statement, BECAUSE
- // pwrOn is now false. AND previousMillisRest is STILL 0!
- if (myMillis - previousMillisRest >= restTime && pwrOn == false)
- {
- // so you store the same 1000 value
- previousMillisRest = myMillis;
- // turn the relay back on
- digitalWrite(relayPin, HIGH);
- // and re-enable the first if-statement.
- pwrOn = true;
- }
- // one second later, it happens again.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement