Generation

generate functionSun, 12 Feb 2023

write a code for ardino uno whenever someone is 10 meters away from the ultrasonic sensor then reduce the speed of the moter and if it is not 10 meters away then let it run at normal speed

int motor = 5; int motor_speed = 0; int trig = 3; int echo = 2; void setup() { pinMode(trig, OUTPUT); pinMode(echo, INPUT); pinMode(motor, OUTPUT); } void loop() { long duration, distance; digitalWrite(trig, LOW); delayMicroseconds(2); digitalWrite(trig, HIGH); delayMicroseconds(10); digitalWrite(trig, LOW); duration = pulseIn(echo, HIGH); distance = (duration/2) / 29.1; if (distance < 10) { motor_speed = 100; } else { motor_speed = 255; } analogWrite(motor, motor_speed); }

Questions about programming?Chat with your personal AI assistant