Hi there. First time posting and looking for some help with my digital IR Sensor. Just to clarify, for this project I am using an Arduino Uno board, LED pin, I2C LCD Screen, servo motors, and digital IR sensor.
The issue that I am having appears to be that the IR sensor is not having it's reading (HIGH or LOW) properly read by the code. I have done simple codes such as this first one below to confirm that the sensor works and can be read in this specific IF statement.
// code for object passing ir sensor and activating led
int IRSensor = 2; // Connect the IR Sensor output to pin 2
int LED = 13; // Connect LED input to pin 13
void setup()
{
pinMode (IRSensor, INPUT); // Define the IR sensor as an input to detect passing objects
pinMode (LED, OUTPUT); // Once IR sensor is read translate this value to LED
}
void loop()
{
int statusSensor = digitalRead (IRSensor); //Make IR Sensor always active to read
if (statusSensor == 1)
{
digitalWrite(LED,LOW); //if there is no reading make LED low (off)
}
else
{
digitalWrite(LED,HIGH); //If there is a reading made make the LED high (on)
}
} // End of passing object code
However, in the code below the IR Sesnor appears to no longer be read by the IF statement. In the stripped down version of what I ultimately want to do I am looking to achieve the same goal as the code above, to light the LED when it detects an object. However in this case, the LED goes on once the device is powered and never goes off, even if the IR sensor doesn't detect any objects. Does the presence of servo motors and other tech components in the system disrupt the sequence of coding? If not then what is the best way to code this script in order to have the IR sensor properly read and then do the corresponding actions that I want when it is detecting an object (Move servo motors, display a message on an I2C LCD screen, etc.)? Would using a switch case help? Please help me and thanks in advance.
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
Servo gripperservo;
Servo slotservo;
int IRSensor = 2;
int gripperservoPin = 7;
int slotservoPin = 9;
int LED = 13; // Connect LED input to pin 13
void setup()
{
lcd.init();
lcd.backlight();
pinMode (IRSensor, INPUT);
gripperservo.attach(gripperservoPin);
slotservo.attach(slotservoPin);
pinMode (LED, OUTPUT); // Once IR sensor is read translate this value to LED
}
void loop()
{
int statusSensor = digitalRead(IRSensor);
if (statusSensor == 0)
{
digitalWrite(LED,HIGH); //If there is a reading made make the LED high (on)
}
}