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);
}
}
briangaskell21:
i cant get it to work
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.
Wawa
March 11, 2023, 8:48am
4
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.
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?
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
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?
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
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..
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);
}
}
system
Closed
September 8, 2023, 5:34am
9
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.