Robot not turning anymore

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;
}

It would be easier if you posted the old code too, so we can see the exact changes.

Also a circuit: I can't visualise the RF remote/receiver thing.... especially the button pressing.

I dont have a circuit diagram but here are the links to the RF receiver and RF remote.

https://www.robotics.org.za/index.php?route=product/product&path=17&product_id=128 // Remote

https://www.robotics.org.za/index.php?route=product/product&path=17&product_id=127 //Receiver

Also old code before changes.

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 = 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.....

Capturexx.PNG

Well I ordered the T4 but i think they might of sent me the (L4) wrong one,.

Thanks Jimbo Going to try to use the 4th button as an (go low) button.

I think that should work.

Have a good day.

Did you check the output with a meter?

Can you see adafruit's part number on there?- according to their site, M=1096, T=1097 and 1098=L.

Nice looking product: I might pop up to Centurion and get one just for mos.

No my multi meter is not here but i put a led on the pins and is does pull down to low.

On the plastic its got (M4 1096) so they did send me the wrong one but its not the latching one its the momentary one.

I tried giving it, its own power supply that seems to be working now.
I think maybe my battery pack didnt have enough power for everything.

Yes they can come in handy and they arent very expensive.

Which one do you think will work the best, I want

Button A to stop my robot from moving
Button B to make it go forward
Button c to turn left
Button D to turn right

T4 toggle type ?

Yeah I'd guess the toggle one.

OTOH, if you get the momentary one you can make it a toggle one in code. You could make it a latched one in code too, for that matter.

But if you get a toggle one, which presumably keeps the pins high or low in hardware, you won't be able to turn it into a momentary one with code.

So toggle is simpler, momentary more flexible I'd say.

Where in SA are you btw? I'm in Jo'burg (Kensington, eastern side). Worked in Pretoria the last year, just picked up a new contract in Rivonia.

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.

Have a look at this example.

I know of Lydenburg- out east in the Lowveld somewhere- never been out that way tough.

This code does a toggle from a momentary switch.

The switch I have is really bouncy: invariable gives 2-3 clicks for the price of 1 so I debounced it in software.

Each click toggles the LED on=>off or off=>on.

// simple toggle
// switch to ground from pin, internal pullup
// switch is debounced in sw with bounce2 library
// lines marked //** debounce are to do with debouncing
// comment out or delete those lines if not using debouncing
// and uncomment the line marked //** uncomment me.

#include <Bounce2.h> //** debounce

byte buttonPin=2;
byte ledPin=13;

byte ledState = 0;
byte buttonState = 0;
byte lastButtonState = 0;

Bounce dbbuttonPin = Bounce(); //** debounce

void setup()
{
  pinMode(buttonPin, INPUT_PULLUP);
  
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  
  dbbuttonPin.attach(buttonPin); //** debounce
  dbbuttonPin.interval(5); //** debounce

  
}

void loop()
{
  dbbuttonPin.update(); //** debounce
  buttonState=dbbuttonPin.read(); //** debounce
  //buttonState = digitalRead(buttonPin); //uncomment me if not using s/w debounce.
  
  if (buttonState != lastButtonState) //switch changed
  {
    if (buttonState == 0) //0= pressed becuse of pullups ("active low")
    {
      ledState = !ledState; //changed and pressed => toggle led
    }
  }
  
  digitalWrite(ledPin, ledState);
  lastButtonState = buttonState;
  
}