relay timer

Hi to you all,

I want to make arduino to control two relays that should go on for certain amount of time and off for some time depending on which of two buttons is pressed. Each button control one relay.

Problems are:

  • when arduino is powered on both relays are on (i want it off)
  • when no button is pressed arduino sonetimes turns relays on and off by himself...

here is the code:

int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = LOW;
long time = 0;
long debounce = 500;
int stayON = 5000; //stay on for 5000 ms

int pinButton1 = 9;
int Relay1 = 3;
int stateRelay1 = LOW;
int stateButton1;
int previous1 = LOW;
long time1 = 0;
long debounce1 = 500;
int stayON1 = 10000; //stay on for 5000 ms
 
void setup() {
  pinMode(pinButton, INPUT);
  pinMode(Relay, OUTPUT);

    pinMode(pinButton1, INPUT);
  pinMode(Relay1, OUTPUT);
}
 
void loop() {
  stateButton = digitalRead(pinButton);  
  if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {
    if(stateRelay == HIGH){
      digitalWrite(Relay, HIGH);
    } else {
       digitalWrite(Relay, LOW);
       delay(stayON);
       digitalWrite(Relay, HIGH);
              delay(stayON);
    }
    time = millis();
  }
  previous == stateButton;

  stateButton1 = digitalRead(pinButton1);  
  if(stateButton1 == HIGH && previous == LOW && millis() - time > debounce1) {
    if(stateRelay1 == HIGH){
      digitalWrite(Relay1, HIGH);
    } else {
       digitalWrite(Relay1, LOW);
       delay(stayON1);
       digitalWrite(Relay1, HIGH);
              delay(stayON);
    }
    time = millis();
  }
  previous == stateButton1;
}

Please help,

sinecerely,

Denis

The relays are probably active LOW, ie turned on by a LOW signal. Change the program logic to fix this

How many previous variables do you have ? You need one for each button.

  previous == stateButton1;

Wrong !

If you start numbering variables at least number all... Or better, arrays :wink:

And if they are active LOW set the output HIGH (to turn it off) before making it an output. Otherwise it will be LOW (thus on) for a short period.

Thanks for quick reply.

I think I could make it work.

Best regards,

Denis

So....another problem...

When I increase int stayON = 5000; //stay on for 5000 ms to 100000 ms it doesnt stop after 100 sec...with 5 sec it works ok..

here is updated code...

int led = 13;

int pinButton = 8;
int Relay = 2;
int stateRelay = LOW;
int stateButton;
int previous = HIGH;
long time = 0;
long debounce = 500;
int stayON = 100000; //stay on for 100000 ms
int stayOFF = 3500000; //stay off for 3500000 ms

int pinButton1 = 9;
int Relay1 = 3;
int stateRelay1 = LOW;
int stateButton1;
int previous1 = HIGH;
long time1 = 0;
long debounce1 = 500;
int stayON1 = 1200000; //stay on for 1200000 ms
int stayOFF1 = 42000000; //stay off for 42000000 ms
 
void setup() {
  pinMode(led, OUTPUT);
  pinMode(pinButton, INPUT);
  pinMode(Relay, OUTPUT);
  pinMode(pinButton1, INPUT);
  pinMode(Relay1, OUTPUT);
         digitalWrite(led, HIGH);
}
 
void loop() {
  stateButton = digitalRead(pinButton); 
  stateButton1 = digitalRead(pinButton1); 
  if(stateButton == LOW && stateButton1 == LOW)
  {
      digitalWrite(Relay, LOW);
    }
  if(stateButton == LOW && previous == HIGH && millis() - time > debounce) {
    if(stateRelay == HIGH){
      digitalWrite(Relay, LOW);
    } else 
  {
       digitalWrite(Relay, HIGH);
 
       delay(stayON);
       digitalWrite(Relay, LOW);
              delay(stayOFF);
    }
    time = millis();
  }
  previous == stateButton;
  previous1 == stateButton1;

  stateButton = digitalRead(pinButton);
  stateButton1 = digitalRead(pinButton1);
   if(stateButton1 == LOW && stateButton == LOW)
  {
      digitalWrite(Relay1, LOW);
    }  
  if(stateButton1 == LOW && previous == HIGH && millis() - time > debounce1) {
    if(stateRelay1 == HIGH){
      digitalWrite(Relay1, LOW);
    } else 
  {
       digitalWrite(Relay1, HIGH);

       delay(stayON1);
       digitalWrite(Relay1, LOW);
              delay(stayOFF1);
    }
    time = millis();
  }
  previous == stateButton;
  previous1 == stateButton1;
}

Thanks for help.

Denis

@ddigula
What is the range of numbers that a int can represent?

:wink:
previous == stateButton;
previous1 == stateButton1;
.

Arrays arrays arrays! Or like you would put it array array2 array3....

Your code would be halve....

@ LarryD
int goes from -32,768 to 32,767, so I see the problem...would long solve problem?

I know that this whit statebutton is wrong...I tried to switch off motor while buttons are low...but..

Thanks!

@ septillion

since I'm beginner in Arduino I'm not sure that I know what to do with arrays, so thats the reason I didn't to that first time you suggested it...but I'll look for solutinos in that way.

Thanks!

People tend to overthink arrays...

Simply

//Instead of
int myVar;
int myVar2;
int myVar3;
int myVar4;

//you do
int myVars[4]; //plural to make it clear!

//and instead of using it like
digitalWrite(myVar2, HIGH);

//you do
//digitalWrite(myVars[1], HIGH);

//aka simply place the number in the bracket. Only note, the first entry is 0, NOT 1. So the last is 3, NOT 4

Yes, a long would solve it. But I would make it a unsigned long because a negative interval would not make sense here (or even break stuff) and you get more range for free :wink:

How do you have the relays wired up and powered? Are you giving them a proper power source?

I want to make arduino to control two relays that should go on for certain amount of time and off for some time depending on which of two buttons is pressed. Each button control one relay.

If you want the two buttons to work independently, then it is cruicial that you not use delay() to do your timings. Go study the "blink without delay" sketch and try to understand how to go about this.

So, i didn't have time to test it, but if I understod everything right this should do what I want...

int led = 13;
int pinButton1 = 8;
int relayPin1 =  2;      // the number of the relay pin
int stateButton1 = LOW;
int relayState1 = LOW;             // relayState used to set the relay
unsigned long previousMillis1 = 0;        // will store last time relay was updated
int pinButton2 = 9;
int relayPin2 =  3;      // the number of the relay pin
int stateButton2 = LOW;
int relayState2 = LOW;             // relayState used to set the relay
unsigned long previousMillis2 = 0;        // will store last time relay was updated
unsigned long time[4] = {100000, 3500000, 1200000, 42000000};	// milliseconds of on-time1, milliseconds of off-time1, milliseconds of on-time2, milliseconds of off-time2

void setup() 
{
  // set the digital pin as output:
  pinMode(relayPin1, OUTPUT);      
  pinMode(relayPin2, OUTPUT);     
  pinMode(pinButton1, INPUT);      
  pinMode(pinButton2, INPUT); 
  pinMode(led, OUTPUT);      
  digitalWrite (led, HIGH);
}

void loop()
{
  // check to see if it's time to change the state of the relay

  unsigned long currentMillis = millis();
  stateButton1 = digitalRead(pinButton1);
  stateButton2 = digitalRead(pinButton2);

  if(stateButton1 == LOW && stateButton2 == LOW)
  {
  digitalWrite(relayPin1, LOW)
  digitalWrite(relayPin2, LOW)
  }


  if(stateButton1 == HIGH && stateButton2 == LOW)
  {
 
  if((relayState1 == HIGH) && (currentMillis - previousMillis1 >= time[0]))
  {
    relayState1 = LOW;  // Turn it off
    previousMillis1 = currentMillis;  // Remember the time
    digitalWrite(relayPin1, relayState1);  // Update the actual relay
  }
  else if ((relayState1 == LOW) && (currentMillis - previousMillis1 >= time[1]))
  {
    relayState1 = HIGH;  // turn it on
    previousMillis1 = currentMillis;   // Remember the time
    digitalWrite(relayPin1, relayState1);	  // Update the actual relay
  }
  }



  if(stateButton1 == LOW && stateButton2 == HIGH)
  {
  if((relayState2 == HIGH) && (currentMillis - previousMillis2 >= time[2]))
  {
    relayState2 = LOW;  // Turn it off
    previousMillis2 = currentMillis;  // Remember the time
    digitalWrite(relayPin2, relayState2);  // Update the actual relay
  }
  else if ((relayState2 == LOW) && (currentMillis - previousMillis2 >= time[3]))
  {
    relayState2 = HIGH;  // turn it on
    previousMillis2 = currentMillis;   // Remember the time
    digitalWrite(relayPin2, relayState2);	  // Update the actual relay
  }
  }
}

I changed part of code whit buttons because i'll use two state switch and it will be always ON or OFF...hope that this work that way...