Hey, I am trying to get a continious servo to stop when the ultrasonic sensors detects a object closer than 10 cm..and to start again when a new command tells it to rotate the other way. I am using 4 pieces of HC-SR04 sensors fitted on different sides of the project.
I was hoping someone could help me to get this working, i deleted the codes regarding the ultrasonic because all i could get was fault messages >:(
This is my current code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("received: ");
Serial.print (incomingByte);
if(incomingByte == 108){
Serial.println(" sent 0 Rotaing CW ");
myservo.write(0);
}else if(incomingByte == 114){
Serial.println(" sent 180 Rotaing CCW ");
myservo.write(180);
}else if(incomingByte == 60){
Serial.println(" sent Stopped ");
myservo.write(60);
}else{
Serial.println(" moving Random");
myservo.write(incomingByte);
}
}
}