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;
}
@ 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.
//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
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...