Question: Use the Ping4Pin program provided in the zip file to modify the Knob program to control the servo by using an advanced sensor (ultrasonic distance
Use the Ping4Pin program provided in the zip file to modify the Knob program to control the servo by using an advanced sensor (ultrasonic distance sensor) instead of the potentiometer.
Determine the map( ) function input and output range values, in order to control the position servo motor through a range of motion of 10 to 170 degrees, by using a distance sensor range of 2 to 22. Hint: Use the constrain( ) function to limit the sensor distance measurement between 2 and 22, before the map( ) function.
Ping_4Pin Program
// Arduino pin numbers for the sensor output and input pins
// create servo object to control a servo #include
void setup() { // initialize serial communication Serial.begin(9600); // initialize servo motor connection myservo.attach(9); // attaches the servo on pin 9 to the servo object // initialize sensor pins pinMode(pingTrig, OUTPUT); pinMode(pingEcho, INPUT); }
void loop() { // establish variables for duration of the ping, // and the distance result in inches and centimeters: long duration, inches, cm;
// The HC-SR04 is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse digitalWrite(pingTrig, LOW); delayMicroseconds( 5); digitalWrite(pingTrig, HIGH); delayMicroseconds(10); digitalWrite(pingTrig, LOW);
// The Echo pin is used to read the signal from HC-SR04; a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. duration = pulseIn(pingEcho, HIGH);
// convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); if (cm > 40) cm = 40; // scale it to use it with the servo (value between 0 and 180) long val = map(cm, 0, 40, 0, 179); // sets the servo position according to the scaled value myservo.write(val);
Serial.print(cm); Serial.print(","); Serial.print(val); Serial.println();
delay(100); }
long microsecondsToInches(long microseconds) { // According to Parallax's datasheet for the Ping sensor, there are // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per // second). This gives the distance travelled by the ping, outbound // and return, so we divide by 2 to get the distance of the obstacle. // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf return microseconds / 74 / 2; }
long microsecondsToCentimeters(long microseconds) { // The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. return microseconds / 29 / 2; }
Knob Program
#include
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin
void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object }
void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180) myservo.write(val); // sets the servo position according to the scaled value delay(15); // waits for the servo to get there }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
