Expected '(' before 'digitalRead'

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);
}

This has two problems:

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

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

Oops -- three problems.

2 Likes

Try:

 if (digitalRead (5) == HIGH)
 {
    button = random(10, 5000);
 }
1 Like

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