Help! Cant Figure This Code out!

Hey Everybody, Thanks in Advance for taking the time to help me out!
I’m very new to arduino and any help is appreciated

I’m using an Arduino Uno, along with (3) LV Maxsonar EZ0 Sensors(ultrasonic), and 3 LEDS. I am Reading this via the Analogue pin on the sensor and to the A0, A1, A2 of my arduino.
I want one of the LEDS to turn on when it detects an object within 2 feet (24inches), then turn off when the object is no longer within 2 feet of the sensor.
For some reason, the code seems to turn the LED off and on when those parameters are not met.

Here’s my code so far:

const int anPin1 = A0;
const int anPin2 = A1;
const int anPin3 = A2;
const int LED1 = 10;
const int LED2 = 11;
const int LED3 = 9;
int triggerPin = 13; // starts the sensor
long distance1, distance2, distance3;

void setup() {
  Serial.begin(9600);  // sets the serial port to 9600
  pinMode(triggerPin, OUTPUT);
  pinMode(anPin1, INPUT);
  pinMode(anPin2, INPUT);
  pinMode(anPin3, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
}

void start_sensor(){
  digitalWrite(triggerPin,HIGH);
  delay(1);
  digitalWrite(triggerPin,LOW);
}

void read_sensors(){
  
  /* 
  Scale factor is (Vcc/512) per inch. A 5V supply yields 9.8mV/in
  Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
  */
  distance1 = analogRead(anPin1)/2;
  distance2 = analogRead(anPin2)/2;
  distance3 = analogRead(anPin3)/2;
}

void print_all(){
  Serial.print("Sensor1:");
  Serial.print(" ");
  Serial.print(distance1);
  Serial.print("inches");
  Serial.print(" ");
  Serial.println(" ");
  Serial.print("Sensor2:");
  Serial.print(" ");
  Serial.print(distance2);
  Serial.print("inches");
  Serial.print(" ");
  Serial.println(" ");
  Serial.print("Sensor3");
  Serial.print(" ");
  Serial.print(distance3);
  Serial.print("inches");
  Serial.print(" ");
  Serial.println(" ");
  delay(20);
}

void Detection_Mode () {

  if (distance1 < 20) {
    digitalWrite( LED1, HIGH);
  } else {
    digitalWrite(LED1, LOW);
  }


  if (distance2 < 20) {
    digitalWrite(LED2, HIGH);
  } else {
    digitalWrite(LED2, LOW);
  }
  
  if (distance3 < 20) {
    digitalWrite(LED3, HIGH);

  } else {
    digitalWrite(LED3, LOW);
  }
}
  
void loop() {
  start_sensor();
  read_sensors();
  print_all();
  Detection_Mode();
  delay(150); 
}

Thanks for helping!

Can you please provide a link to the sensors that you are using ?

It could be that your ultrasonic sensor does not go that far. It might be getting confused because it is detecting bits and pieces, but can't measure the distance because the sound waves are to weak.

I don’t know about the Uno, but on some Arduinos, an analog read must be thrown away when changing channels. Try this:

void read_sensors(){
  
  /* 
  Scale factor is (Vcc/512) per inch. A 5V supply yields 9.8mV/in
  Arduino analog pin goes from 0 to 1024, so the value has to be divided by 2 to get the actual inches
  */


  distance1 = analogRead(anPin1);
  distance1 = analogRead(anPin1)/2;
  distance2 = analogRead(anPin2);
  distance2 = analogRead(anPin2)/2;
  distance3 = analogRead(anPin3);
  distance3 = analogRead(anPin3)/2;
}

By the way, an analog pin goes from 0 to 1023.

When you are tempted to suffix variable names with numbers, it is time to learn about arrays.

Thanks, I seemed to have figured out the issue…
I changed a bit of code and lowered a delay at the end of printing the results of the sensor into
the serial communication port, which seemed to have resolve my issues.

Quick Question though:

If i want to repeat a “Void Function” within the “Void Loop”(Main Loop), do i have to use a infinite ‘for loop’, or is there another way to stay inside that function when its called.
The reason i’m asking, is i want my (3) range finding sensors to continually be ‘Void Detection_Mode’ section of my code, instead of repeating the start up and reading of the sensors.
hope that made sense!
Thanks for the responses

Just do

void loop(){
Detection_Mode()
}

loop() loops infinitely