First, we made IR Obstacle Detection Sensor For Dimming LED (Stop Event Facing Obstacle), then we made 2 Push Button One LED to Switch On/Off project. Here is Circuit Diagram and Code To Make LED Switch On By Push Button Switch Off By IR Obstacle Sensor and Arduino Board. In next guide, we will improve our Arduino Door Bell – door bell will start with glowing LEDs upon button press and stop when house owner go closer to door to open it.
LED Switch On By Push Button Switch Off By IR Obstacle Sensor
There is actually not huge difference between push button and IR Obstacle Detection Sensor or any sensor which has one value very high and the other value lower, closer to zero mimicking 1 and 0 or yes and no or ON and off. This logic will not work for things like room temperature sensor without modification as we do not experience huge difference of temperature within so much short time.
First, modify the previous 2 Push Button One LED project to replace the button pin to switch off the LED with sensor pin :
---
As for the coding part, when you are replacing the button pin to switch off the LED with sensor pin on breadboard, if use the old code that will work in primitive way without any need of change :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | const int buttonPin1 = 2; // first push button const int ledPin1 = 13; // LED const int buttonPin2 = 4; // second push button int lastPin1State,lastPin2State; void setup() { pinMode(buttonPin1, INPUT); pinMode(ledPin1, OUTPUT); pinMode(buttonPin2,INPUT); } void loop() { // read the pushbutton input pin: int pin1State = digitalRead(buttonPin1); int pin2State = digitalRead(buttonPin2); if (pin1State == HIGH && lastPin1State == LOW && pin2State == LOW ) { digitalWrite(ledPin1,HIGH); } if (pin2State == HIGH && lastPin2State == LOW && pin1State == LOW) { digitalWrite(ledPin1,LOW); } lastPin1State = pin1State; lastPin2State = pin2State; delay(10); } |
I informed you – there is not huge difference between push button and any sensor which has one value very high and the other value lower, closer to zero.
If you modify the code to this, then you’ll have meaningful serial print (which may be useful to debug or plan to print on a LED or LCD display) :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | const int buttonPin1 = 2; // first push button const int ledPin1 = 13; // LED const int buttonPin2 = 4; // second push button int lastPin1State,lastPin2State; void setup() { pinMode(buttonPin1, INPUT); pinMode(ledPin1, OUTPUT); pinMode(buttonPin2,INPUT); Serial.begin(9600); } void loop() { // read the pushbutton input pin: int pin1State = digitalRead(buttonPin1); int pin2State = digitalRead(buttonPin2); if (pin1State == HIGH && lastPin1State == LOW && pin2State == LOW ) { Serial.println("Button pressed"); digitalWrite(ledPin1,HIGH); } if (pin2State == HIGH && lastPin2State == LOW && pin1State == LOW) { Serial.println("Something closer to IR"); digitalWrite(ledPin1,LOW); } lastPin1State = pin1State; lastPin2State = pin2State; delay(10); } |