Hi all,
I'm still new to all this. But, I was trying to make 2 servo motors turn whenever the device
comes to close to an object via a distance-sensor.
My motors don't seem to react whenever the device comes close to an object though and I wonder what I could be missing.
Thanks in advance!
#include <Servo.h> // include a special file that is needed for servo motors, do this only once
Servo motor1; // create a servo object for each motor
Servo motor2;
void setup() {
// initialize the servo on pin 6
motor1.attach(10);
motor2.attach(11);
Serial.begin(9600);
// set input and output pins
pinMode(7, INPUT);
pinMode(8, OUTPUT);
}
void loop() {
// the trigger pin sends a pulse
digitalWrite(8, LOW);
delayMicroseconds(2);
digitalWrite(8, HIGH);
delayMicroseconds(10);
digitalWrite(8, LOW);
// the echo pin measures how long it took for that pulse to come back
Serial.println(pulseIn(7, HIGH));
if (pulseIn(7, HIGH) < 612)
motor1.write(0);
else {
motor1.write(90);
// delay in between reads for stability
delay(50);
}
}