title: Arduino Switch to Turn a Relay timer
body: <p>Briefly: I would like to turn on a relay for 30 seconds, after I toggle a switch on.</p>
  
  <p>I'm trying to do a blinds automation at home.</p>
  
  <p>I have a simple ON-OFF-ON switch, attached to an Arduino connected to Relays. </p>
  
  <p>I want to turn on Relay#1 for a maximum of 30 seconds if I toggle the switch down from center. In other words, relay turns on when I switch, and when timer reaches 30 seconds relay turns off.</p>
  
  <p>similarly I want to turn on Relay#2 for exactly 30 seconds if I toggle the switch up from center</p>
  
  <p>And when I switch back to center, I would like the timer to reset.</p>
  
  <p>I could not figure out how. Could anyone help?</p>
  
  <p>I have been trying to use elapsedMillis library for this, which is a nice library that helps me avoid using Delays:
  <a href="http://playground.arduino.cc/Code/ElapsedMillis" rel="nofollow">http://playground.arduino.cc/Code/ElapsedMillis</a></p>
  
  <p>However even though I could work the relays without the 30 second limitation, I couldn't figure out the code to end working of the relays. Here is my current code:</p>
  
  <pre><code>      #include &lt;elapsedMillis.h&gt;
      #define RELAY_ON 0
      #define RELAY_OFF 1
  
      #define RELAY1_TURNS_ON_BLINDS  5
      #define RELAY2_SHUTS_DOWN_BLINDS 6
  
      #define shutswitch A0
      #define openswitch A1
  
      bool LocalCommandToOpen;
      bool LocalCommandToShut;
  
      void setup() ////////SETUP////////
      {
      digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
       digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
  
       pinMode(RELAY1_TURNS_ON_BLINDS, OUTPUT);
       pinMode(RELAY2_SHUTS_DOWN_BLINDS, OUTPUT);
  
       pinMode(shutswitch, INPUT);
       pinMode(openswitch, INPUT);
          } ////SETUP
  
    void loop() { ///////LOOP
           if (digitalRead(shutswitch) == 1)
       {
         LocalCommandToOpen = 1;
       }
       else
       {
         LocalCommandToOpen = 0;
       }
  
        if ( digitalRead(openswitch) == 1)
       {
         LocalCommandToShut = 1;
       }
       else
       {
         LocalCommandToShut = 0;
       }
  
      unsigned int CloseInterval = 14000;
       elapsedMillis timeElapsedSinceCloseButtonPush = 0;
       unsigned int OpenInterval = 14000;
       elapsedMillis timeElapsedSinceOpenButtonPush = 0;
  
  
  
       //MANUAL SWITCH OPERATION
  
       if ( LocalCommandToShut == 1 )
       {
         digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
         digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_ON);
       }
       else
       {
         digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
       }
  
      //MANUEL DUGME ILE ACMA
       if ( LocalCommandToOpen == 1)
       {
         digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
         digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_ON);
       }
       else
       {
         digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
       }
  
       delay(500);
  
      } /////////////////LOOP////////////////////////////////////
  </code></pre>