Reputation: 11
I'm not proffessional in arduino coding,and i have an question about sketch. I need code to control relay with push buttons. I have 3 push buttons 3 leds and 2 relays. When 1 button push then select first led if twice push then select second led. When push second button once then select first relay,if twice push then select second relay,and in the end start button to start all this commands an then lights third led. Pls Help! this is my code:
int button1=2;
int button2=3;
int button3=4;
int relay1=8;
int relay2=9;
int led=5;
int led2=6;
int led3=7;
int button1State=0;
int button2State=0;
int button3State=0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(button1, INPUT_PULLUP);
pinMode(button2,INPUT_PULLUP);
pinMode(button3,INPUT_PULLUP):
pinMode(relay1, OUTPUT);
digitalWrite(relay1, HIGH);
pinMode(relay2,OUTPUT);
digitalWrite(relay2, HIGH);
pinMode(led,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
button1State = digitalRead(button1);//when once click turn led
if(button1State==HIGH){
digitalWrite(led,HIGH);
delay(wait);
if(button1State==HIGH){ //when clicked twice then turn on led 2, but i dont know how to do it
digitalWrite(led2,HIGH);
delay(wait)
}
}
if (button2State== HIGH){
digitalWrite(relay1,HIGH);
delay(wait);
if(button2State==HIGH){ //when clicked twice turn on second relay but i dont know how to do this
digitalWrite(relay2,HIGH);
delay(wait);
}
if(button2State==HIGH){
digitalWrite(relay1&&relay2,HIGH);
delay(wait);
}
}
//and click start i dont kknow how to do this :((
//when select led then circuits run for 10 sec,if led 2 select then circuit runs for 20 sec
}
Upvotes: 1
Views: 825
Reputation: 28974
Draw a flow chart of your code and you'll quickly find out that it does not what you want to do.
Let's go over a few of your lines:
Here you read the state of button 1 at that very moment
button1State = digitalRead(button1);//when once click turn led
If that state is HIGH you turn on the led and wait. (wait is undefined here btw, this will cause an error)
if(button1State==HIGH){
digitalWrite(led,HIGH);
delay(wait);
Then if the button state is HIGH you turn on the second led and wait. same problem as above.
if(button1State==HIGH){ //when clicked twice then turn on led 2, but i dont know how to do it
digitalWrite(led2,HIGH);
delay(wait)
}
...
So your first problem is that you do not update button1State. It is still the value you read at the beginning.
How would you approach if you just talk to yourself? What do you need to do if you want to do three different things each time you press a button?
you need to find out that the button is pressed (you ask yourself did I press the button?)
you need to know how often you have pressed the button (you count 1, 2, 3, 1, 2, 3)
you need to do different things for each button press (if 1 then do a, if 2 then do b, if 3 then do c ...
you also need to know if the button is still pressed
So you need a variable that counts the button presses.
The following code is just meant to show you how you could approach things. I have not tested if it compiles nor did I implement everything you need. Just giving you a starting point.
Also at some point you have to reset your counter. You can use the modulo operator for this. Now go and learn!
int button1Count = 0;
int lastButton1State = LOW;
// your other variables
void setup()
{
pinMode(button1, INPUT_PULLUP);
}
void loop()
{
int button1State = digitalRead(button1);
// button 1 has just been pressed
if (button1State && !lastButton1State)
{
button1Count++;
}
lastButton1State = button1State;
switch(button1State) {
case 1:
digitalWrite(led, HIGH);
break;
case 2:
digitalWrite(led2, HIGH);
break;
}
}
When you successfully implemented this you should find out how to write non-blocking arduino code.
Upvotes: 0