Expected primary Expression before "or"

Can anyone fix this code please? (The error message is below).

const int buttonPin1 = 2; 
const int buttonPin2 = 3; 
const int ledPin =  9;  
int buttonState = 0;  

void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
}

void loop() 
{
  buttonState = digitalRead(buttonPin2);
  or
  buttonState = digitalRead(buttonPin1);
  if (buttonState == HIGH) 
  {
    digitalWrite(ledPin, HIGH);
  } 
  else 
  {
    digitalWrite(ledPin, LOW);
  }
}

Arduino: 1.8.19 (Windows 10), Board: "Arduino Mega or Mega 2560, ATmega2560 (Mega 2560)"
6:3: error: expected primary-expression before 'or' token

or

^~

exit status 1

expected primary-expression before 'or' token

I am trying to light one LED with either one of two buttons.

Many thanks
astat101

  buttonState = digitalRead(buttonPin2) or digitalRead(buttonPin1);

Then follow it with

if (buttonState > 0) 
  {
    digitalWrite(ledPin, HIGH);
  } 
  else 
  {
    digitalWrite(ledPin, LOW);
  }

Assuming that you have wired it so that an un-pushed button returns a zero.

That brings to mind:

buttonState = digitalRead(buttonPin2) + digitalRead(buttonPin1);

:slight_smile: then you can later tell how many buttons were pushed if necessary...

Before multiple people jump on this, the usual way would be:

if (digitalRead(buttonPin1) == HIGH or digitalRead(buttonPin2) == HIGH) {...

which has the advantage of self explaining....

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