Write Arduino code using an HC SR04 distance sensor so that as it perceives an object…
- User
Write Arduino code using an HC SR04 distance sensor so that as it perceives an object moving closer it blinks an LED faster. As an object moves away, the LED blinks slower.
- Assistantscss
const int trigPin = 9; const int echoPin = 10; const int ledPin = 13; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(ledPin, OUTPUT); } void loop() { long duration, distance; // Trigger the sensor to emit a pulse digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the duration of the pulse duration = pulseIn(echoPin, HIGH); // Calculate the distance based on the duration distance = (duration / 2) / 29.1; // Determine the LED blink rate based on the distance if (distance >= 50) { digitalWrite(ledPin, HIGH); delay(200); digitalWrite(ledPin, LOW); delay(200); } else if (distance >= 20 && distance < 50) { digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); delay(100); } else { digitalWrite(ledPin, HIGH); delay(50); digitalWrite(ledPin, LOW); delay(50); } }This code uses an HC SR04 ultrasonic distance sensor to measure the distance of an object from the sensor. The
pulseInfunction is used to measure the duration of the pulse emitted by the sensor and received back after hitting an object. Thedistancevariable is then calculated based on the duration.Finally, an LED connected to pin 13 is used to indicate the distance of the object. The LED will blink faster as the object moves closer and slower as it moves away. The blink rate is determined by the distance value, with distances less than 20 cm causing the LED to blink the fastest and distances greater than 50 cm causing it to blink the slowest.
- User
Change so that change in blink rate is continually gradual
- Assistantscss
const int trigPin = 9; const int echoPin = 10; const int ledPin = 13; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(ledPin, OUTPUT); } void loop() { long duration, distance; // Trigger the sensor to emit a pulse digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the duration of the pulse duration = pulseIn(echoPin, HIGH); // Calculate the distance based on the duration distance = (duration / 2) / 29.1; // Determine the LED blink rate based on the distance int delayTime = map(distance, 0, 50, 50, 200); digitalWrite(ledPin, HIGH); delay(delayTime); digitalWrite(ledPin, LOW); delay(delayTime); }This code uses the function to smoothly change the LED blink rate based on the distance of the object. The function maps the distance value, which ranges from 0 to 50 cm, to a delay time that ranges from 50 to 200 milliseconds. The LED will blink faster as the object moves closer and slower as it moves away, creating a gradual change in the blink rate.
- User
Add so that when an object is more than 50cm away the led stays on
- Assistantscss
const int trigPin = 9; const int echoPin = 10; const int ledPin = 13; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(ledPin, OUTPUT); } void loop() { long duration, distance; // Trigger the sensor to emit a pulse digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure the duration of the pulse duration = pulseIn(echoPin, HIGH); // Calculate the distance based on the duration distance = (duration / 2) / 29.1; // Determine the LED state based on the distance if (distance >= 50) { digitalWrite(ledPin, HIGH); } else { int delayTime = map(distance, 0, 50, 50, 200); digitalWrite(ledPin, HIGH); delay(delayTime); digitalWrite(ledPin, LOW); delay(delayTime); } }