Newbie timer question



  • I'm new at arduino programming and I'm sure this will be obvious to most of you. I am trying to create a sketch that will control 2 relays based on the pin state of pin 2 for example.

    So when pin 2 is HIGH relay 1 should turn on x seconds later relay 2 should turn on.

    When pin 2 goes LOW I want relay 1 to turn off and x seconds later I want relay 2 to turn off.

    So far I have no problem getting relay 1 to turn on and after x seconds relay 2 to turn on.

    My confusion begins here. Right now both relays turn off when pin 2 goes low because both were turned on by an if then else under the condition pin 2 is HIGH.

    Can you give me ideas conceptually how to approach this? I'm not asking you to code it just to give me some ideas on how to approach the problem.

    Thanks!

    
    const int ZwavePin = 2; // controlled by zwave relay to turn fireplace on or off
    const int FirePin = 7; // turns on gas
    const int FanPin = 8; // turns on fan
    float val;
    //float voltage;
    long previousMillis = 0;
    long currentMillis;
    
    void setup(){
     Serial.begin(9600);
    
    
        //initialize the fan and fire pins as an output:
      pinMode(FirePin, OUTPUT);
      pinMode(FanPin, OUTPUT);  
      pinMode(ZwavePin, INPUT);
      digitalWrite(FirePin, LOW);
      digitalWrite(FanPin, LOW);
     // digitalWrite(ZwavePin, LOW);
    }
    
    void loop(){
      
      
    
     
      
      val=digitalRead(ZwavePin); // read the input pin    
      if (val == HIGH){
        digitalWrite(FirePin, LOW);    // sets the firepin to low to turn on gas
        unsigned long currentMillis = millis();
        Serial.print("Fire has been on for ");
        Serial.println(currentMillis);
        
        if (currentMillis > 5000)
          digitalWrite(FanPin, LOW);    // sets the fanpin to low to turn on fan
      }
      else
      {  
        digitalWrite(FirePin, HIGH);
        digitalWrite(FanPin, HIGH);
      } 
     
    

  • Mod

    I think it may be easier for you to look at timer library in arduino ide, also it could be better to use the interrupt to detect change state on the input and make it easier to calculate intervals


  • Mod

    @scottdube If your code is allowed to block during the wait, the implementation can be very straightforward (untested):

    // ...setup etc...
    
    void loop()
    {
        static int prevState = LOW;
        int state = digitalRead(ZwavePin);
        if (state != prevState)
        {
            // ZWavePin state has changed
            // Determine target state of relay
            const int pinState = (state == HIGH) ? LOW : HIGH;
            // Set Fire relay
            digitalWrite(FirePin, pinState);
            // Wait some ms
            delay(5000);
            // Set Fan relay
            digitalWrite(FanPin, pinState);
            // Remember current  ZWavePin state to be able to detect changes
            prevState = state;
        }
    }
    

    As your description doesn't mention the source of the ZWavePin, it might require debouncing if it is a mechanical switch.


Log in to reply
 

Suggested Topics

  • 1
  • 10
  • 5
  • 2
  • 198
  • 3

22
Online

11.2k
Users

11.1k
Topics

112.5k
Posts