LEDS with distance sensor and for loop

Hi everyone,

I want to design a code that uses a distance sensor to code four LED's. If distance is greater than x cm then led1 turns on but if distance is less than x cm, I want led 2 to turn on, delay a few seconds then turn led 3 on, delay a few seconds and then led 4 on if that makes sense. Here is the code I have written so far, the operation variable is getting stuck in the loop and it's not progressing past operation 1. Any help will be so appreicated. Thanks.

int operation;
const int trig = A5; // TRIG pin
const int echo = A3; // ECHO pin
const int led1 = 8;// LED Pins
const int led2 = 7;
const int led3 = 6;
const int led4 = 4;
const float c = 343; // Speed of sound [m/s]
float t; // Time Variable
float d; // Distance Variable


void setup() {
  // put your setup code here, to run once:
Serial.begin (9600); // Begin the Serial Read
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);


}
//===============================================================
// Continuous Loop


void loop() {
//  // put your main code here, to run repeatedly:
digitalWrite(trig, HIGH); // Start ultrasound
delayMicroseconds(10000); // 10 microsecond delay
digitalWrite(trig, LOW); // End ultrasound
t = pulseIn(echo, HIGH); // Measure time taken [microseconds]
t = t*(0.000001); // [microseconds] -> [seconds]
d = (t/2)*c; // distance [meters]
d = d*100; // [meters] -> [centimeters]
Serial.print("distance: "); // Print "Distance"
Serial.print(d); // Print Measured Distance
Serial.println(" cm"); // Print Distance Units
delay(50);
if (d > 10) {
  operation=0;
}
else{
operation = 1;
}
if (operation==0){
  digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
}
else if (operation==1){
  digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
Serial.println(operation);
operation=operation+1;
delay(1000);

}
else if(operation==2){
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,HIGH);
digitalWrite(led4,LOW);
//delay(3000);

operation=operation+1;
}
else if(operation==3){
  digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,HIGH);
delay(3000);
operation=1;
}
Serial.println(operation);
//digitalWrite(led4,HIGH);
}

Each time thru loop( ) you revert operation to either 0 or 1 so it will never go past 1.

Try:


int operation;
const int trig = A5; // TRIG pin
const int echo = A3; // ECHO pin
const int led1 = 8;// LED Pins
const int led2 = 7;
const int led3 = 6;
const int led4 = 4;
const float c = 343; // Speed of sound [m/s]
float t; // Time Variable
float d; // Distance Variable


void setup() {
  // put your setup code here, to run once:
Serial.begin (9600); // Begin the Serial Read
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);


}
//===============================================================
// Continuous Loop


void loop() {
//  // put your main code here, to run repeatedly:
digitalWrite(trig, HIGH); // Start ultrasound
delayMicroseconds(10000); // 10 microsecond delay
digitalWrite(trig, LOW); // End ultrasound
t = pulseIn(echo, HIGH); // Measure time taken [microseconds]
t = t*(0.000001); // [microseconds] -> [seconds]
d = (t/2)*c; // distance [meters]
d = d*100; // [meters] -> [centimeters]
Serial.print("distance: "); // Print "Distance"
Serial.print(d); // Print Measured Distance
Serial.println(" cm"); // Print Distance Units
delay(50);
if (d > 10) {
  operation=0;
}
else{
operation = 1;
}
if (operation==0){
  digitalWrite(led1,HIGH);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
}
if (operation==1){
  digitalWrite(led1,LOW);
digitalWrite(led2,HIGH);
digitalWrite(led3,LOW);
digitalWrite(led4,LOW);
Serial.println(operation);
operation=operation+1;
delay(1000);

}
if(operation==2){
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,HIGH);
digitalWrite(led4,LOW);
operation=operation+1;
delay(3000);
}
if(operation==3){
  digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
digitalWrite(led3,LOW);
digitalWrite(led4,HIGH);
delay(3000);
operation=1;
}
Serial.println(operation);
//digitalWrite(led4,HIGH);
}

Thanks so much for the help! That seems to solve the issue, do you know how to stop the code when it reaches the end? As in don't loop back to "operation 1"?

Unplug the Arduino :wink: :rofl:

Actually, @LarryD would a for loop be useful here? Or could this line just be removed?

Define "the end"? If the end is the completion of operation 3 then replace:

operation=1;

with:

while (true);

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.