Giving conflicting commands

Here I have a problem because I'm giving double command to arduino.

  1. Turn LED on when brightness is under 30
  2. Turn LED off when brightness is over 30
  3. When button is pressed, turn off LED
    #1 and #3 is in conflict with one another in my current code.

Is there something like "OR"
I tried with else and if, and not too successful.

const int buttonPin = 8;
const int ledPin = 10;

int buttonState = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  }

void loop() {
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  
  buttonState = digitalRead(buttonPin);
  
  if(buttonState == LOW || sensorValue >= 30)
  {
    digitalWrite(ledPin, LOW);
  }else if(sensorValue < 30)
  {
    digitalWrite(ledPin, HIGH);
  }
  delay(200);
}

I'm not sure I follow your issue.

 if(buttonState == LOW || sensorValue >= 30) {
    digitalWrite(ledPin, HIGH);
  } else if(sensorValue < 30)

This code turns the LED on when SensorValue is 30 or greater. Your description of #1 and #2 seems to imply you want the opposite behavior. Which is it?

My apologies, was working with so many experiments and I copy and pasted the wrong code. Fixed now.

blanclait:
My apologies, was working with so many experiments and I copy and pasted the wrong code. Fixed now.

Okay, well now the code matches, I still don't see a problem.

The code says if the button is low (pressed?) OR sensorValue is over 30, the LED gets turned OFF. Otherwise, it gets turned ON. Isn't that what you describe?

Edit: Added a missing "don't"

That's correct James.

So, then what's your question?

I thought the command I have written for Arduino was in conflict as it failed to function.
That is the button did not do anything.

You didn't enable the internal pull up, are you using an external pull up resistor?

The LED will only be off for as long as the pin is low (e.g. Pressed)

yes, I do realize that LED will be OFF only when button is being pressed.
(planning on doing 1 button press without holding next)

Yeap, I am using external pull up resistor.

blanclait:
#1 and #3 is in conflict with one another in my current code.

You need to decide which of those rules you want to take precedence, and then write the code so that it does.

I think this will work, assuming the button input goes low when it's pressed:

boolean state = ((sensorValue < 30) && !buttonState);
digitalWrite(ledPin, state);

No, I'm wrong, argh.

if(buttonState = false)
{
state = (sensorState < 30);
digitalWrite(ledPin, state);
}
else digitalWrite(ledPin, false);

...That should work, I think.

Sev:
if(buttonState = false)

if(buttonState == FALSE)

Dar god, I hate it when I mess up in that particular way. That's part of why I use a compiler that warns me about those. :wink: