2 buttons pressed

Im working on a project that i want 2 buttons to be pressed and then filck a realy on so if wont turn on realy till it has 2 buttons pressed i was play with a if statment but i cant get it to work`

int button1 = 7;
int button2 = 8;


void setup() {
 
 pinMode(LED_BUILTIN, OUTPUT);
pinMode (button1, INPUT_PULLUP);
pinMode (button2, INPUT_PULLUP);
}

void loop() {
  
  digitalWrite(LED_BUILTIN, LOW);
  {
    if (digitalRead)(button1 == LOW && button2 == LOW);
 
    digitalWrite(LED_BUILTIN, HIGH);
    }
    

    }

   

Hello briangaskell21
Post your sketch, well formated, with well-tempered comments and in so called code tags "</>" and schematic to see how we can help.

Hi, @briangaskell21
Welcome to the forum.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

if (digitalRead (button1) == LOW && digitalRead (button2) == LOW)  // must read both of them
  {
  // do something
  }

Put some delay() after you turn on the LED, otherwise it isn't on long enough to see.
Leo..

Edit: semicolon blunder corrected.

delete the semicolon

Hi, @briangaskell21

What model Arduino are you using?

Look at this bit of code;

  {
    if (digitalRead)(button1 == LOW && button2 == LOW);
    digitalWrite(LED_BUILTIN, HIGH);
  }

How do you structure an if statement?

Also how will you turn the LED off, you need extra code to do that.

Where is your digitalRead statement to read each of the button inputs?

Tip. Press [ CTRL ] [ T ] in the IDE and you code will be formatted with the corrected indentation.

Please post your edited code in a new post, do not go back and edit the original.

Thanks.. Tom.. :smiley: :smiley: :+1: :coffee: :coffee: :australia:

Im using a uno am only using the led as a trial i will be adding a realy to turn on for 10 sec then off will try tonight as im at work at moment thanks

hi thanks that helped. it working now ill work on adding relay now thans agan

int button1 = 7;
int button2 = 8;


void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  pinMode (button1, INPUT_PULLUP);
  pinMode (button2, INPUT_PULLUP);
}

void loop() {

  digitalWrite(LED_BUILTIN, LOW);


  if (digitalRead (button1) == LOW && digitalRead (button2) == LOW) {

    digitalWrite(LED_BUILTIN, HIGH);

    delay (9000);
  }
}


This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.