Hi,
I'm new trying the Arduino features and I tried a code to blink a LED depending on the input from two different IR sensors. I hoped three different situations depending whether one, two or none of the IR sensors is triggered. The situation when none of the sensors is triggered works fine. When only one of the sensors is triggered is also fine. But the very first "if" statement where the two sensors are supposed to be triggered at the same time gives a flickering LED instead of steady light.
Do you have an advice on this?
below the code:
The else parts turn the LED off. The LED can be permanently on only if all subsequent conditions are true. Use else if, instead of only if, to skip subsequent checks after a true condition was found.
It's possible your inputs are floating. Many types of IR sensor, like PIR movement sensors, can only pull the pin low, they can't pull it high, so it is left floating. Try using INPUT_PULLUP or attaching an external 10K pull-up.
I would separate the checking of the sensors and the flashing of the led, to keep things simple:
byte count = 0;
if (digitalRead(9) == HIGH) count++;
if (digitalRead(10) == HIGH) count++;
// Now, count will be the number of sensors triggered at the moment: 0, 1 or 2
Thanks for the suggestion.
If I understand correctly, this code would not differenciate which one of the two sensor is triggered?
This is the reason why I tried the code updated above : so that the arduino can tell which one sends input.
The idea behind is to replace the LED sequence with servo rotation and adapt the turning direction depending on which side has an obstacle
Ok, I get it. That wasn't very clear from your previous posts.
byte sensors = 0;
if (digitalRead(9) == HIGH) sensors += 1;
if (digitalRead(10) == HIGH) sensors += 2;
// Now, sensors will indicate which sensors triggered at the moment: 0, 1, 2 or 3