Hi there please could some one help, I have a robot that follows me around but i added a RF remote and receiver as well but now the follow me turning is not working .
Many Thanks.
#include <Servo.h>
//Cm is the left sensor from the back
//Cm1 is the right sensors from the back
Servo Rightservo; // create servo object named myservo to control a servo/ Right servo
Servo Leftservo; //create servo object named myservo1 to control a servo/ Left servo
const int trigPin = 7;//Sender sensor1
const int echoPin = 8;//Receiving sensor 1 //Left sensor from front
const int echoPin1 = 9;//Receiving sensor 2 //Right sensor from front
const int straight = 2;
const int turnL = 3;
const int turnR = 4;
void setup()
{
pinMode(straight,INPUT);
pinMode(turnL,INPUT);
pinMode(turnR,INPUT);
Rightservo.attach(11); // Right servo from the back
Leftservo.attach(10); //Left servo from the back
}
void loop() {
long duration, cm;
long duration1, cm1;
//Sender sensor 1
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
//Receiving sensor 1
pinMode(echoPin, OUTPUT);
digitalWrite(echoPin, HIGH);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);
delay(250);
//Sender sensor 1
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
//Receiving sensor 2
pinMode(echoPin1, OUTPUT);
digitalWrite(echoPin1, HIGH);
pinMode(echoPin1, INPUT);
duration1 = pulseIn(echoPin1, HIGH);
cm1 = microsecondsToCentimeters(duration1);
delay(250);
if(digitalRead(straight) ==HIGH)//if RF forward button is pressed go straight
{
Rightservo.write(180);
Leftservo.write(0);
}
else if(digitalRead(turnL) ==HIGH)//if RF turn left button is pressed go left
{
Rightservo.write(180);
Leftservo.write(103);
}
else if(digitalRead(turnR) ==HIGH)//if RF turn right button is pressed go right
{
Rightservo.write(101);
Leftservo.write(0);
}
else if(cm > 100 && cm < 200 && cm1 > 100 && cm1 < 200) //Go forward if distance is greater than 100cm.
{
Rightservo.write(150); //Go forward = 180
Leftservo.write(30); //Go forward = 0
}
else if(cm > 10 && cm < 100 ) //Stop both servos if to close.
{
Rightservo.write(101); //Stop
Leftservo.write(103); //Stop
}
else if (cm1 > 10 && cm1 < 100)
{
Rightservo.write(101); //Stop
Leftservo.write(103); //Stop
}
else if(cm - cm1 >= 5) //Turn right if sensor1 value minus sensor2 value is more than 10cm.
{
Rightservo.write(130);
Leftservo.write(0);
}
else if(cm1 - cm >= 5) //Turn left if sensor2 value minus sensor1 value is more than 10cm.
{
Rightservo.write(180);
Leftservo.write(74);
}
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 14.5 / 2;
}
#include <Servo.h>
//Cm is the left sensor from the back
//Cm1 is the right sensors from the back
Servo Rightservo; // create servo object named myservo to control a servo/ Right servo
Servo Leftservo; //create servo object named myservo1 to control a servo/ Left servo
const int trigPin = 7;//Sender sensor1
const int echoPin = 8;//Receiving sensor 1 //Left sensor from front
const int echoPin1 = 4;//Receiving sensor 2 //Right sensor from front
void setup()
{
Rightservo.attach(11); // Right servo from the back
Leftservo.attach(10); //Left servo from the back
}
void loop() {
long duration, cm;
long duration1, cm1;
//Sender sensor 1
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
//Receiving sensor 1
pinMode(echoPin, OUTPUT);
digitalWrite(echoPin, HIGH);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);
delay(250);
//Sender sensor 1
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
//Receiving sensor 2
pinMode(echoPin1, OUTPUT);
digitalWrite(echoPin1, HIGH);
pinMode(echoPin1, INPUT);
duration1 = pulseIn(echoPin1, HIGH);
cm1 = microsecondsToCentimeters(duration1);
delay(250);
if(cm > 100 && cm < 200 && cm1 > 100 && cm1 < 200) //Go forward if distance is greater than 100cm.
{
Rightservo.write(150); //Go forward = 180
Leftservo.write(30); //Go forward = 0
}
else if(cm > 10 && cm < 100 ) //Stop both servos if to close.
{
Rightservo.write(101); //Stop
Leftservo.write(103); //Stop
}
else if (cm1 > 10 && cm1 < 100)
{
Rightservo.write(101); //Stop
Leftservo.write(103); //Stop
}
else if(cm - cm1 >= 10) //Turn right if sensor1 value minus sensor2 value is more than 10cm.
{
Rightservo.write(120);
Leftservo.write(0);
}
else if(cm1 - cm >= 10) //Turn left if sensor2 value minus sensor1 value is more than 10cm.
{
Rightservo.write(180);
Leftservo.write(84);
}
}
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 14.5 / 2;
}
Nothing obvious comes to mind, but Which receiver do you have?- the M, T or L? That could will have a bearing on things.
I'd put a voltmeter on the receiver outputs, or an LED with resistor, and check that they actually go low. Even if you have the M model, it might need a pull down resistor?
Also, the original "if" needn't be an "else if" of the new "if" I think, so take that "else" off and make it standalone, that might help.
edit... according to that page on Micro's site, you may well have the latching model. Looks to me like after you press any button the first time, one or other is always selected.... (But the page says toggle at the top, latching at the bottom.)
and even more edit.... You only use 3 of the 4 buttons ne? So if you do have the latching model, use the 4th as a dummy, push it, make the others got low.....
Jimbo how would you turn the momentary one into a toggle one via code?
Im in Lydenburg if you know where that is haha, was working in Port Elizabeth but moved back to my home town recently.
Ye most of the work is in that area i guess.
The secret to making a momentary switch into a toggle switch, is to do with not looking to see if is pressed, but rather that it became pressed. That is, you need to keep track of its state and see if that changed.
If it's pressed now, but wasn't before, it just got pressed. But if it's pressed now and was before, then it's still pressed from last time.