Hello, so basically im completely new to arduino and programming overall. I tried to make simple traffic light using arduino. I ran in to issue with function 'if' and 'digitalwrite'. The code is not complete yet this is all i have. Thanks in advance
int button = 0;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, INPUT);
}
void loop() {
if digitalRead (5, HIGH); {
int button = random(10, 5000);
}
delay(button);
digitalWrite(4, HIGH);
delay(50);
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
delay(50);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
delay(1000);
}
DaveX
April 19, 2022, 5:28pm
2
This has two problems:
The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords.
You need parentheses around the if clause, and you need to eliminate the null statement following the if by eliminating the semicolon:
if (digitalRead (5, HIGH)) {
1 Like
horace
April 19, 2022, 5:29pm
3
if requires () around the test expression
if (digitalRead (5, HIGH)) {
button = random(10, 5000);
}
also removed the ; which would terminate the if
also you redefine button inside the {} which will create a local variable
2 Likes
Try:
if (digitalRead (5) == HIGH)
{
button = random(10, 5000);
}
1 Like
system
Closed
October 16, 2022, 6:14pm
6
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.