Arduino Code does not work together

It only works individually, either the button or the serial monitor input of 0 and 1, but when put together only the button works. I want both to work simultaneously.

int led = 9;
int button = 7;

void setup() {
pinMode(led, OUTPUT);
pinMode(button, INPUT);
Serial.begin(9600);
}
void loop(){

char ledState = Serial.read();
if(ledState == '1'){
digitalWrite(led, HIGH);
}
if(ledState == '0'){
digitalWrite(led, LOW);
}

int buttonState = digitalRead(button);
if ( buttonState == LOW){
Serial.println("Button is pressed");
digitalWrite(led, HIGH);
}
else{ digitalWrite(led, LOW);
delay(500);
}
}

Your topic was MOVED to its current forum category which is more appropriate than the original as it has nothing to do with Installation and Troubleshooting of the IDE

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Please use code tags.

What are you trying to do?
Please describe when the LED should turn on and when it should turn off.

Sorry, first time using a forum, I am trying to turn on the led using both the button and typing 1 to turn on a LED and 0 to turn off an LED. The LED should turn on when the button is pressed and off when unpressed.

The code that reads and acts on the button being pressed will always be executed no matter what you enter in the Serial monitor

You should only execute the button reading portion of the code if nothing has been entered

Yea, but I also want to execute the code where I can turn the LED on and off with 0 and 1 without the use of the button, so I would have either the option to turn the LED on with the button or the serial monitor. The issue is when I have the part of the code that allows the button to turn on the LED, the serial monitor code does not work. Is there a fix for this?

I already suggested it

If there is Serial data available then read it and act on it and ignore the button, else read the button and act on it

I think you need to detect the change of the button.

  • not pressed -> button pressed => turn LED on
  • button pressed -> not pressed => turn LED off

This could look like this:

current_state = read_button()
if (last_state != current_state) {
  update_led()
  last_state = current_state
}

What would that code look like?

the button works in the code, its the serial data that is not registering.

The serial works but is immediately overwritten by your button code.

What code do I need to change that?

if (Serial.available())
{
  code here to read Serial input and act on it if valid
}
else
{
  code here to read the button input and act on it
}

Start with an empty sketch and only print to the serial when the button changes.

The pseudo code in #8 will help you.

Oh... and show your code please.

It didnt work, I just updated the post to include the code.

I just updated the post to show the code.

Whatever code you updated you should not have because it makes a nonsense of any comments to that post and it is regarded as bad form

In any case, I can't see where you are testing whether Serial data is available

Please post your revised code in a new post in this topic

if (Serial.available())

Yes, but it is not in your code

@red_11

Your other topic on the same subject deleted. Why did you open it ?

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.