Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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.

Write the following code in Arduino if your distance sensor is 3-pin. For the 4-pin s For the IR sensor the code and connections are different, and simpler. Google that too. int distance sensor_pin 2 distance sensor, google the code! It will be very similarl I/ define the variable for the pin where the distance sensor is connected to the arduino. II This pin will be used both as an input and output. Winitialize long integer variables to store the sensor data. long int pulse_duration, distance_in_inches W all the code in the setup function is run once // start serial communication. void setupO ( Serial.begin(9600) void loop0 I/ all the code in the loop function is run again and again I/ make the sensor pin output, we'll be sending a signal out. // make pin low I/ keep it low for 2 microseconds. / making the pin high will ask the sensor to send an ultrasonic pulse. Il keep sensing sound for 5 microseconds. II stop the sound pulse I/ make the sensor pin input, we'll be receiving an echo signal out. pinModel distance sensor_pin, OUTPUT) digitalWrite( distance_sensor pin, Low) delayMicroseconds(2); digitalWrite( distance sensor pin, HIGH); digitalWritel distance_sensor_pin, LOW); pinMode( distance_sensor pulse_duration pulseln( distance_sensor_pin, HIGH) distance_in_inches pulse duration /7412; pin, INPUT) this function will tell how long it took for you to hear back the echo. // the time to echo/72-total distance traveled by the sound pulse to and back from the obstacle, I/ and a further division by 2 will give you the distance to the object. Serialprintln(distance_in_inches) delay(200); Display distance in serial monitor IIl Short delay before you repeat the code and send another sound pulse out. IWgo back to the first line in the loop0function and repeat the code If everything is set up correctly, when you run the program, the output displayed on the Serial Monitor will be the distance measured by the sensor. You can adjust the obstacle distance by your hand in front of the sensor, or by clicking on the sensor in Tinkercad and adjusting the obstacle circle. Also note what happens when the dot is NOT in the sensor's 'field of view The Serial Monitor is accessed by going through the following menus: "Tools> Serial Monitor" in the Arduino software, or at the bottom right in Tinkercad. Please test and make sure everything works. Deliverable for this activity: (7 points) In the above setup, attach an LED (in series with a enough resistors) to Arduino pin of your choice Write an Arduino code to make the LED 1) turn on if the distance is smaller than some value x of your choice, larger than some value y of your choice. Submit the snapshot or picture of your circuit and code. 2) blink if the distance is between the values y and x, and 3) turn off if the distance is

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.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote