#include <Servo.h>
Servo myservo;
int ang;
const int TriggerPin = 8;
const int EchoPin = 9;
long Duration = 0;
void setup(){
myservo.attach(2);
pinMode(TriggerPin,OUTPUT);
pinMode(EchoPin,INPUT);
Serial.begin(9600);
}
void loop(){
digitalWrite(TriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin, LOW);
Duration = pulseIn(EchoPin,HIGH);
long Distance_cm = Distance(Duration);
Serial.print("Distance = ");
Serial.print(Distance_cm);
Serial.println(" cm");
ang = map(Distance_cm, 0, 1023, 0, 179);
myservo.write(ang);
delay(100);
}
long Distance(long time)
{
long DistanceCalc;
DistanceCalc = ((time /29) / 2);
return DistanceCalc;
}
Just tested the servos with sensors as a proof of concept. Will need to work on smoothing and fine tuning the angles,possibly with gearing/levers.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.