Pages: [1]   Go Down
Author Topic: simple home control system  (Read 991 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As a newbee, I started my first project with a arduino.
The project is a small home control system. (push buttoms and relays to control the lights).
With the push buttoms I can switch the light on and off. (working)
With one pushbutton I switch all the lights off (working), except one. This one has to go one for X minutes and then go off.
This last function will not work. The light relay 14 stays on.
When I use the delay fucntion it works, but with the millis() function I struggle.
I tested the function in a small sketch and it worked perfect. In the general scetchit refuse to work.


  • // 16 ingangen
    // TABEL 1
    int sw1 = 22;   // Arduino Digital I/O pin number     
    int sw2 = 23;       
    int sw3 = 24;       
    int sw4 = 25;       
    int sw5 = 26;       
    int sw6 = 27;       
    int sw7 = 28;       
    int sw8 = 29;       
    int sw9 = 30;
    int sw10 = 31;
    int sw11 = 32;
    int sw12 = 33;
    int sw13 = 34;
    int sw14 = 35;    //inkom
    int sw15 = 36;    // alle verlichting uit + inkom 5 minuten aan.(test 5 seconds)
    int sw16 = 37;


    #define RELAY_ON 0
    #define RELAY_OFF 1
    /*-----( Declare objects )-----*/
    /*-----( Declare Variables )-----*/

    //tabel 2
    // 16 uitgangen (voor test poort 38 vervangen door 13
    #define Relay_1  38 
    #define Relay_2  39
    #define Relay_3  40
    #define Relay_4  41
    #define Relay_5  42
    #define Relay_6  43
    #define Relay_7  44
    #define Relay_8  45
    #define Relay_9  46
    #define Relay_10  47
    #define Relay_11  48
    #define Relay_12  49
    #define Relay_13  50
    #define Relay_14  51
    #define Relay_15  52
    #define Relay_16  53


    int state1 = HIGH;      // status van de relais
    int state2 = HIGH;
    int state3 = HIGH;
    int state4 = HIGH;
    int state5 = HIGH;     
    int state6 = HIGH;
    int state7 = HIGH;
    int state8 = HIGH;
    int state9 = HIGH;     
    int state10 = HIGH;
    int state11 = HIGH;
    int state12 = HIGH;
    int state13 = HIGH;
    int state14 = HIGH;     
    int state15 = HIGH;
    int state16 = HIGH;

    int reading;           // lezen status drukknop


    int previous1 = HIGH;    // vorige status relais
    int previous2 = HIGH;
    int previous3 = HIGH;
    int previous4 = HIGH;
    int previous5 = HIGH;
    int previous6 = HIGH;
    int previous7 = HIGH;
    int previous8 = HIGH;
    int previous9 = HIGH;
    int previous10 = HIGH;
    int previous11 = HIGH;
    int previous12 = HIGH;
    int previous13 = HIGH;
    int previous14 = HIGH;
    int previous15 = HIGH;
    int previous16 = HIGH;


    long time = 2;         // laatste tijd dat de drukknop was ingedrukt
    long debounce = 300;   // the debounce time, verhoog als de uitgang flikkert
    long starttijd;

    void setup()
    {
     
      pinMode(sw1, INPUT);
      pinMode(sw2, INPUT);
      pinMode(sw3, INPUT);
      pinMode(sw4, INPUT);
      pinMode(sw5, INPUT);
      pinMode(sw6, INPUT);
      pinMode(sw7, INPUT);
      pinMode(sw8, INPUT);
      pinMode(sw9, INPUT);
      pinMode(sw10, INPUT);
      pinMode(sw11, INPUT);
      pinMode(sw12, INPUT);
      pinMode(sw13, INPUT);
      pinMode(sw14, INPUT);
      pinMode(sw15, INPUT);
      pinMode(sw16, INPUT);
     
    //-------( Initialize Pins so relays are inactive at reset)----
      digitalWrite(Relay_1, RELAY_OFF);
      digitalWrite(Relay_2, RELAY_OFF);
      digitalWrite(Relay_3, RELAY_OFF);
      digitalWrite(Relay_4, RELAY_OFF);
      digitalWrite(Relay_5, RELAY_OFF);
      digitalWrite(Relay_6, RELAY_OFF);
      digitalWrite(Relay_7, RELAY_OFF);
      digitalWrite(Relay_8, RELAY_OFF);
      digitalWrite(Relay_9, RELAY_OFF);
      digitalWrite(Relay_10, RELAY_OFF);
      digitalWrite(Relay_11, RELAY_OFF);
      digitalWrite(Relay_12, RELAY_OFF);
      digitalWrite(Relay_13, RELAY_OFF);
      digitalWrite(Relay_14, RELAY_OFF);
      digitalWrite(Relay_15, RELAY_OFF);
      digitalWrite(Relay_16, RELAY_OFF);   

     
      pinMode(Relay_1, OUTPUT);   
      pinMode(Relay_2, OUTPUT); 
      pinMode(Relay_3, OUTPUT); 
      pinMode(Relay_4, OUTPUT);
      pinMode(Relay_5, OUTPUT);   
      pinMode(Relay_6, OUTPUT); 
      pinMode(Relay_7, OUTPUT); 
      pinMode(Relay_8, OUTPUT); 
      pinMode(Relay_9, OUTPUT);   
      pinMode(Relay_10, OUTPUT); 
      pinMode(Relay_11, OUTPUT); 
      pinMode(Relay_12, OUTPUT); 
      pinMode(Relay_13, OUTPUT);   
      pinMode(Relay_14, OUTPUT); 
      pinMode(Relay_15, OUTPUT); 
      pinMode(Relay_16, OUTPUT); 
     
     
     
    }
      void loop()

      //input 1
      {
        reading = digitalRead(sw1);

      // if the input just went from LOW and HIGH and we've waited long enough
      // to ignore any noise on the circuit, toggle the output pin and remember
      // the time
      if (reading == LOW && previous1 == HIGH && millis() - time > debounce)
      {
           if (state1 == HIGH) state1 = LOW;
           else state1 = HIGH;
           time = millis();
      }
      digitalWrite(Relay_1, state1);
     previous1 = reading;
     
     
      //input 2
       
       reading = digitalRead(sw2);
      if (reading == LOW && previous2 == HIGH && millis() - time > debounce)
      {
           if (state2 == HIGH) state2 = LOW;
           else state2 = HIGH; 
           time = millis();
      }
      digitalWrite(Relay_2, state2);
       previous2 = reading;
       
     ..........
      //input 13
        reading = digitalRead(sw13);
      if (reading == LOW && previous13 == HIGH && millis() - time > debounce)
      {
          if (state13 == HIGH) state13 = LOW;
          else  state13 = HIGH;

        time = millis();   
      }
      digitalWrite(Relay_13, state13);
      previous13 = reading;
     
    input 14
      reading = digitalRead(sw14);
      if (reading == LOW && previous14 == HIGH && millis() - time > debounce)
      {
        if (state14 == HIGH){state14 = LOW;}
        else  {state14 = HIGH;}
        time = millis();   
      }
      digitalWrite(Relay_14, state14);
      previous14 = reading;
     
     //input 15
    // all lights off, except
    // relay_14 ON for 5 minutes (for test 5 seconds)

     //reading = digitalRead(sw15);
    if (digitalRead(sw15) == LOW )
    {
     starttijd = millis();
     afsluiten();// ga naar subroutine voor doven lichten
     }
      }
     
     void afsluiten()
     {
      digitalWrite(Relay_1,RELAY_OFF); state1=HIGH;
      digitalWrite(Relay_2,RELAY_OFF); state2=HIGH;
      digitalWrite(Relay_3,RELAY_OFF); state3=HIGH;
      digitalWrite(Relay_4,RELAY_OFF); state4=HIGH;
      digitalWrite(Relay_5,RELAY_OFF); state5=HIGH;
      digitalWrite(Relay_6,RELAY_OFF); state6=HIGH;
      digitalWrite(Relay_7,RELAY_OFF); state7=HIGH;
      digitalWrite(Relay_8,RELAY_OFF); state8=HIGH;
      digitalWrite(Relay_9,RELAY_OFF); state9=HIGH;
      digitalWrite(Relay_10,RELAY_OFF); state10=HIGH;
      digitalWrite(Relay_11,RELAY_OFF); state11=HIGH;
      digitalWrite(Relay_12,RELAY_OFF); state12=HIGH;
      digitalWrite(Relay_13,RELAY_OFF); state13=HIGH;
      digitalWrite(Relay_14,RELAY_ON); //state15=LOW;
      if ((millis()- starttijd) > 5000){digitalWrite(Relay_14,RELAY_OFF); } // relay 14 5 seconds on for test
     
     }

Logged

New River, Arizona
Offline Offline
God Member
*****
Karma: 16
Posts: 906
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Take a look at the Time library and the associated timeAlarms library that are in the arduino playground.  You not only get access to the time so you can turn things on and off based on time of day, but you also get timers that can turn things on and off for certain periods.

Logged

Trying to keep my house under control http://www.desert-home.com/

Holland
Offline Offline
Newbie
*
Karma: 1
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what happens: If switch 15 == LOW, you jump to afsluiten.
millis - starttijd is not greater then 5000, so nothing happens and void loop starts again.

Then again, starttijd = millis, we jump to afsluiten and again (and never will...) millis - starttijd is not greater then 5000.

So make sure you only update starttijd once.
« Last Edit: January 22, 2013, 11:28:59 am by Kong » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

it's working.
replaced the 'if' function by 'while'
Quote




 //input 15
// alle verlichting worden uitgezet, uitgezonderd deze van de inkom 
// Verlichting inkom gaat aan voor 5 minuten

 reading = digitalRead(sw15);
if (reading == LOW && previous15 == HIGH && millis() - time > debounce)
       { 
        
         
          time = millis();
          afsluiten();// ga naar subroutine voor doven lichten
      
     tijdrelais(); // ga naar subroutine voor aansturing verlichting doorgang
      }
  }
     
 void afsluiten()
 { 
  digitalWrite(Relay_1,RELAY_OFF); state1=HIGH;
  digitalWrite(Relay_2,RELAY_OFF); state2=HIGH;
  digitalWrite(Relay_3,RELAY_OFF); state3=HIGH;
  digitalWrite(Relay_4,RELAY_OFF); state4=HIGH;
  digitalWrite(Relay_5,RELAY_OFF); state5=HIGH;
  digitalWrite(Relay_6,RELAY_OFF); state6=HIGH;
  digitalWrite(Relay_7,RELAY_OFF); state7=HIGH;
  digitalWrite(Relay_8,RELAY_OFF); state8=HIGH;
  digitalWrite(Relay_9,RELAY_OFF); state9=HIGH;
  digitalWrite(Relay_10,RELAY_OFF); state10=HIGH;
  digitalWrite(Relay_11,RELAY_OFF); state11=HIGH;
  digitalWrite(Relay_12,RELAY_OFF); state12=HIGH;
  digitalWrite(Relay_13,RELAY_OFF); state13=HIGH;
  digitalWrite(Relay_14,RELAY_OFF); state14=HIGH;
  
 
 }

void tijdrelais()
// tijdsschakelaar voor verlichting doorgang 5 minuten aan te doen.
{
  starttijd=millis();
   while(millis() < starttijd+2000) {
      digitalWrite(Relay_14,RELAY_ON); state15=LOW;
   }    
     digitalWrite(Relay_14,RELAY_OFF);state15=HIGH;  
    
    }
  //previous15= reading;
   //} // relay 15 5 seconds on for test


//}


Logged

Pages: [1]   Go Up
Jump to: