Close

Testing parts

A project log for The Vision Project

A project to provide a form of "vision" to the blind and partially sighted.

haydn joneshaydn jones 04/17/2015 at 16:280 Comments
/*
HC-SR04 


trig pin 8
echo pin 9
5v
servo pin 2

*/
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo 
int ang;    // angleofservo

const int TriggerPin = 8; //Trig pin
const int EchoPin = 9; //Echo pin
long Duration = 0;

void setup(){
myservo.attach(2);  // attaches the servo on pin 2 to the servo object 
pinMode(TriggerPin,OUTPUT); // Trigger is an output pin
pinMode(EchoPin,INPUT); // Echo is an input pin
Serial.begin(9600); // Serial Output
}

void loop(){
digitalWrite(TriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin, HIGH); // Trigger pin to HIGH
delayMicroseconds(10); // 10us high
digitalWrite(TriggerPin, LOW); // Trigger pin to HIGH

Duration = pulseIn(EchoPin,HIGH); // Waits for the echo pin to get high
// returns the Duration in microseconds
long Distance_cm = Distance(Duration); // Use function to calculate the distance

Serial.print("Distance = "); // Output to serial
Serial.print(Distance_cm);
Serial.println(" cm");
ang = map(Distance_cm, 0, 1023, 0, 179);
myservo.write(ang);

delay(100); // 10 readings per second
}

long Distance(long time)
{
// Calculates the Distance in cm
// ((time)*(Speed of sound))/ toward and backward of object)

long DistanceCalc; // Calculation variable
DistanceCalc = ((time /29) / 2); // Actual calculation in cm
//DistanceCalc = time / 74 / 2; // Actual calculation in inches
return DistanceCalc; // return calculated value
}
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