I am writing a simple arduino code. I have two leds and corresponding two switches. When one switch is pressed one led is on and other is off. Then there is a Serial.read function, which reads reset from the computer. Then both switch are off. After that other switch is pressed and other led is on. My problem is when one switch is on other shouldn't be working till the Serial.read happens.But in my case, when led1 is on if I press switch2 led2 is on and led1 is off. But that is not my desired operation. I want to make the logic that when led1 is on if I press switch2 led2 shouldn't be on and wait for the Serial.read to happen. Here is my code. I need to know what should be the correction in the logic:
Serial Monitor (user input) Ask Question Asked 2 years. Arduino Serial Monitor. Serial monitor disconnecting itself. Arduino Vending Machine to monitor coin slot input while waiting for user input. Why a servo doesn`t move to angles properly. Hot Network Questions. Check For Data Input with Serial.available This next sketch uses Serial.available to check for input from your serial monitor. Fundamentally, we will stay in setup until we receive an input from the Arduino serial monitor. Go ahead and upload this sketch. Arduino: wait for serial input. Ask Question Asked 2 years, 11. I want to make the logic that when led1 is on if I press switch2 led2 shouldn't be on and wait for the Serial.read to happen. Here is my code. I need to know what should be the correction in the logic. Browse other questions tagged arduino serial-port or ask your own.
2 Answers
You seem to be using some asynchronous code structure. This is a good thing, but I am not really sure if this is what you intended to do.
In its current state, the code will continue looping over and over, checking if one of the buttons is pressed.
Now, there is two ways of achieving this:
- Either make a proper state machine design, which would be the preferred way
- Or just wait for the serial to be available at some point.
For the second solution, you could replace
by
essentially doing nothing while there is nothing on the serial port. Of course, this will completely freeze the rest of the program, and isn't very flexible.
For the first way, there is once again multiple ways of doing it. Since you seem not to be familiar with C programming (no offense), one of the easiest ways of doing it would be to change your if (d11)
and else if (d21)
statements by if (d11 && serial_read false)
and else if (d21 && serial_read true)
. Then, at the top of the program, add:
Arduino Wait For Serial Input Number
This is the basic idea. I will let you sort out the various bugs and improvements (such as setting serial_read
to false
again) as an exercise.
Arduino Serial Input String
I also strongly encourage you to read a bit about programming in general, and C programming in particular. I also advise you to try to stick to some convention for indenting your code. There is nothing worse than code with mixed indentation.