Hi Im new to programming ini general, Ive been through the sample codes in "getting started with Arduino" and now Im trying out some sensors I have here. The first on eis the HC-SR04 ultrasonic I have it working using this code,
/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
More info at: http://goo.gl/kJ8Gl
*/
#define trigPin 12
#define echoPin 13
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
int duration, distance;
digitalWrite(trigPin, HIGH);
delayMicroseconds(1000);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if (distance >= 200 || distance <= 0){
Serial.println("Out of range");
}
else {
Serial.print(distance);
Serial.println(" cm");
}
delay(500);
}
My question is how would I go about getting a servo to rotate with this sensor mounted on top of it and compare distances on both sides and make a decision ie. Go forward or Stop or whatever?.Ive been searching for a few days now and could only find something similar using a PING sensor not the HC-SR04 but I have not been able to modify it due to lack of knowledge.
Any sketches or pointers would be great.