if / else as a function of analog input

Hello,

I have a IR sensor which supplies some voltage when no object is detected, and 0V if something is at 15cm or closer. My aim is to have a LED light up when something is detected.

I believe I can achieve this by:

  1. reading the input from the analog pin to which sensor is connected
  2. if sensor value less than "benchmark" - light the LED
    2bis. if sensor value is more than "benchmark" - turn the LED off
  3. display value on serial monitor (when no object detected)
    3bis. display "sensor triggered" when value under benchmark and LED on

Now, bear with me, because I am no code wizard and this is where I need your help:

int sensor=0; // assign analog pin zero to sensor 
int sensorValue=0; // 
int ledPin=2; // assign digital pin two to LED

void setup()

{

serial.begin(9600);
pinMode(ledPin, OUTPUT); // are these really necessary?
pinMode(sensor, INPUT);

}

void loop()

{

sensor=analogRead(sensorValue); // to display on serial monitor values from sensor
serial.println(sensor);
delay(100);

}

I would like you to enlighten me on the use of if/else. I have tried this:

if (sensorVALUE > 100)

{

digitalWrite(ledPin, LOW);

}

else

{

digitalWrite(ledPin, HIGH);

}

But I don't know where and how to insert in the above code. Please help?

Thank you

  1. if sensor value less than "benchmark" - light the LED
    2bis. if sensor value is more than "benchmark" - turn the LED off

The term is threshold.

   sensorValue = analogRead(sensorPin); // to display on serial monitor values from sensor
   Serial.println(sensorValue);

   if(sensorValue > 150)
   {
      // There is something there
   }

Your design specification can be turned into code like this:

const int SENSOR_PIN = A0;
const int LED_PIN = 2;
const int BENCHMARK = 100;

void setup() {
  Serial.begin(9600);
  pinMode(LED_PIN, OUTPUT);
  // No pinMode() needed for analogRead()
}

void loop() {
  // 1. reading the input from the analog pin to which sensor is connected
  int input = analogRead(SENSOR_PIN);
  
  // 2. if sensor value less than "benchmark" - light the LED
  if (input < BENCHMARK)
    digitalWrite(LED_PIN, HIGH);
    
  //2bis. if sensor value is more than "benchmark" - turn the LED off
  if (input > BENCHMARK)
    digitalWrite(LED_PIN, LOW);
    
  // 3. display value on serial monitor (when no object detected)
  if (digitalRead(LED_PIN) == LOW)
    Serial.println(input);
    
  // 3bis. display "sensor triggered" when value under benchmark and LED on
  if (digitalRead(LED_PIN) == HIGH)
    Serial.println("sensor triggered");
}

You will find that your serial output is flooded. You will probably want to add code to limit the output, perhaps only displaying text when the light changes or the sensor output changes.

Just a small nit but you can’t easily use if/else with your specification because you specified “< benchmark” and “> benchmark” but not “== benchmark”. To write that correctly with if/else you would have to say:

  if (input < BENCHMARK)
    digitalWrite(LED_PIN, HIGH);
 else if (input > BENCHMARK)
    digitalWrite(LED_PIN, LOW);

If you had said:

  1. if sensor value less than “benchmark” - light the LED otherwise turn the LED off

then the code could be:

  if (input < BENCHMARK)
    digitalWrite(LED_PIN, HIGH);
 else
    digitalWrite(LED_PIN, LOW);

or more simply:

    digitalWrite(LED_PIN, input < BENCHMARK);

(Because HIGH==1==true and LOW==0==false)

You are very kind - thank you. Let me digest what you told me and cover some blanks I have. I shall return with more questions probably.

thanks for taking the time.