Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Newbie timer question

Newbie timer question

Scheduled Pinned Locked Moved Development
3 Posts 3 Posters 595 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scottdube
    wrote on last edited by
    #1

    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);
      } 
     
    
    YveauxY 1 Reply Last reply
    0
    • gohanG Offline
      gohanG Offline
      gohan
      Mod
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • S scottdube

        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);
          } 
         
        
        YveauxY Offline
        YveauxY Offline
        Yveaux
        Mod
        wrote on last edited by Yveaux
        #3

        @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.

        http://yveaux.blogspot.nl

        1 Reply Last reply
        1
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        13

        Online

        11.7k

        Users

        11.2k

        Topics

        113.1k

        Posts


        Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • MySensors
        • OpenHardware.io
        • Categories
        • Recent
        • Tags
        • Popular