Please help me an Arduino code to make the LED 1) turn on if the distance is sma
ID: 2293566 • Letter: P
Question
Please help me an Arduino code to make the LED 1) turn on if the distance is smaller than some value x of your choice, 2) blink if the distance is between the values y and x, and 3) turn off if the distance is larger than some value y of your choice. ?
int distance_sensor_pin = 2; // define the variable for the pin where the distance sensor is connected to the arduino.
// This pin will be used both as an input and output.
long int pulse_duration, distance_in_inches; //initialize long integer variables to store the sensor data.
void setup() { // all the code in the setup function is run once
Serial.begin(9600); // start serial communication.
}
void loop() { // all the code in the loop function is run again and again
pinMode( distance_sensor_pin, OUTPUT) ; // make the sensor pin output, we'll be sending a signal out.
digitalWrite( distance_sensor_pin , LOW); // make pin low
delayMicroseconds(2); // keep it low for 2 microseconds.
digitalWrite( distance_sensor_pin , HIGH); // making the pin high will ask the sensor to send an ultrasonic pulse.
delayMicroseconds(5); // keep sensing sound for 5 microseconds.
digitalWrite( distance_sensor_pin , LOW); // stop the sound pulse
pinMode( distance_sensor_pin , INPUT); // make the sensor pin input, we'll be receiving an echo signal out.
pulse_duration = pulseIn( distance_sensor_pin , HIGH); // this function will tell how long it took for you to hear back the echo.
distance_in_inches = pulse_duration / 74 / 2; // the time to echo/72 = total distance traveled by the sound pulse to and back from the obstacle,
// and a further division by 2 will give you the distance to the object.
Serial.println(distance_in_inches); // Display distance in serial monitor.
delay(200); // Short delay before you repeat the code and send another sound pulse out.
} // go back to the first line in the loop() function and repeat the code.
Explanation / Answer
int distance_sensor_pin = 2; // define the variable for the pin where the distance sensor is connected to the Arduino.
// This pin will be used both as an input and output.
long int pulse_duration, distance_in_inches; //initialize long integer variables to store the sensor data.
int x=20,y=30; // define distance x and y
int ledpin=13; // define ledpin
void setup() { // all the code in the setup function is run once
Serial.begin(9600); // start serial communication.
}
void loop() { // all the code in the loop function is run again and again
pinMode( distance_sensor_pin, OUTPUT) ; // make the sensor pin output, we'll be sending a signal out.
digitalWrite( distance_sensor_pin , LOW); // make pin low
delayMicroseconds(2); // keep it low for 2 microseconds.
digitalWrite( distance_sensor_pin , HIGH); // making the pin high will ask the sensor to send an ultrasonic pulse.
delayMicroseconds(5); // keep sensing sound for 5 microseconds.
digitalWrite( distance_sensor_pin , LOW); // stop the sound pulse
pinMode( distance_sensor_pin , INPUT); // make the sensor pin input, we'll be receiving an echo signal out.
pulse_duration = pulseIn( distance_sensor_pin , HIGH); // this function will tell how long it took for you to hear back the echo.
distance_in_inches = pulse_duration / 74 / 2; // the time to echo/72 = total distance traveled by the sound pulse to and back from the obstacle,
// and a further division by 2 will give you the distance to the object.
if(distance_in_inches<=x)
digitalWrite(ledpin,HIGH); //led on
else if(distance_in_inches>x&&distance_in_inches<=y)
{digitalWrite(ledpin,HIGH); //blink
delay(100);
digitalWrite(ledpin,LOW);
delay(100);
}
else if(distance_in_inches>y)
digitalWrite(ledpin,LOW); //led off
Serial.println(distance_in_inches); // Display distance in serial monitor.
delay(200); // Short delay before you repeat the code and send another sound pulse out.
} // go back to the first line in the loop() function and repeat the code.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.