i have made a script for a arduino that has a switch (pin 8) connected and 2 relays(pin 2 and 3)
i want that when i press the button relay1 will go on.
i want that when i press the button again relay1 goes off and relay2 will go on.
i want that when i press the button again they both need to go on.
and the last time i press it i want every thing to go off.
i added delays of 0,5 sec in case in hold the button i doesn’t go crazy.
my code works but it directly starts, and noting happens when i press the button.
what did i done wrong?
Timon
const int relay1 = 2;
const int relay2 = 3;
const int bell = 8;
int bellState = 0;
int relay1State = 0;
int relay2State = 0;
void setup() {
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(bell, INPUT);
}
void loop(){
// read the state of the pushbutton value:
bellState = digitalRead(bell);
relay1State = digitalRead(relay1);
relay2State = digitalRead(relay2);
if (bellState == HIGH);
(relay1State == LOW);
(relay2State == LOW); {
digitalWrite(relay1, HIGH);
delay(0500);
if (bellState == HIGH);
(relay1State == HIGH);
(relay2State == LOW); {
digitalWrite(relay1, LOW);
digitalWrite(relay2, HIGH);
delay(0500)
;if (bellState == HIGH);
(relay1State == LOW);
(relay2State == HIGH); {
digitalWrite(relay1, HIGH);
delay(0500)
;if (bellState == HIGH);
(relay1State == HIGH);
(relay2State == HIGH); {
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
delay(0500)
; }
{
}
}}}}
I am a native speaker and i am a starter in Python. I don't know what base 8 is and if there is something better. And what do I need to do with the semicolon? Can you be more clear in what I need to change?
#define but 2 // Pin 2 on arduino board
#define Relay1 3 // Pin 3 on arduino board
#define Relay2 4 // Pin 4 on arduino board
void setup()
{
pinMode(but,INPUT_PULLUP);
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,LOW);
}
void loop()
{
// First hit relay1 on relay 2 off
if (digitalRead(but) == LOW && digitalRead(Relay1) == LOW && digitalRead(Relay2) == LOW){
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,LOW);
while(digitalRead(but) == LOW){delay(50);} // wait here until release the button
// or
//delay(500);
}
// Second hit relay 1 off relay 2 on
if (digitalRead(but) == LOW && digitalRead(Relay1) == HIGH && digitalRead(Relay2) == LOW){
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,HIGH);
while(digitalRead(but) == LOW){delay(50);} // wait here until release the button
// or
//delay(500);
}
// Third hit relay 1 on relay 2 on
if (digitalRead(but) == LOW && digitalRead(Relay1) == LOW && digitalRead(Relay2) == HIGH){
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,HIGH);
while(digitalRead(but) == LOW){delay(50);} // wait here until release the button
// or
//delay(500);
}
// forth hit relay 1 off relay 2 off
if (digitalRead(but) == LOW && digitalRead(Relay1) == HIGH && digitalRead(Relay2) == HIGH){
digitalWrite(Relay1,LOW);
digitalWrite(Relay2,LOW);
while(digitalRead(but) == LOW){delay(50);} // wait here until release the button
// or
//delay(500);
}
}
thanks, it worked. I try to learn from my mistakes but i don't see the cause why mine script just ''ignored'' the button.
if you could tell me that, that would be awesome.
timon989:
thanks, it worked. I try to learn from my mistakes but i don't see the cause why mine script just ''ignored'' the button.
if you could tell me that, that would be awesome.