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
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.
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?
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.