1

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
}
7
  • Programming this is not hard but the requirements are unclear so I would start with that, organizing resources and actions and describing the flow of the program. You should draw a flowchart, it should help you explain the requirements and will make programming a lot easier. Commented Apr 10, 2021 at 12:35
  • Few questions you should answer: 1) Is button3 is what you mentioned as “start button”? 2) Does “all this commands” means turning all leds and relays? If yes, for how long and what happen after that timeout (wait)? 3) What happen after the wait delay? Do you want to turn the leds and relays off? 4) what happen if buttons are pressed again (more than twice)? Commented Apr 10, 2021 at 12:36
  • Here’s what I was able to grasp from your question: 1) button1 pressed: turn led1 on, wait 10 sec. 2) button1 pressed again: turn led2 on, wait 10 sec. 3) button2 pressed: turn relay1 on, wait 10 sec. 4) button2 pressed again: turn relay2 on, wait 10 sec. 5) button3 pressed: do all above (where you wrote “all this commands”) and turn led3 on. Commented Apr 10, 2021 at 12:37
  • Thisi is like a timer. You set the secon and minute and at the end run start the timer. In my project you set how long this circuit will run. At the beginning you set Led1 or Led 2,When you choose led 1 then circuit runs 10 sec,if you choose led 2 then circuit runs 20 sec,second you set the relay which relay will work? firs second or both? at the end when you push start button all this chooses will run. 10 sec or 20 sec Commented Apr 10, 2021 at 13:20
  • int my code I dont know how to code push button for a once twice or triple pushing,when push once led1 turns on when push twice led 2 turns on an this method would work for relays too. And I need your help. What wrong in my code for this project. Help pls Commented Apr 10, 2021 at 13:30

1 Answer 1

0

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?

  1. you need to find out that the button is pressed (you ask yourself did I press the button?)

  2. you need to know how often you have pressed the button (you count 1, 2, 3, 1, 2, 3)

  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 ...

  4. 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you help me to write all code? cause I'm not a professional programmer, Pls Help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.