Mazebot-Servo Control Issue

Hi, so i started a topic early today about my robot having control issues. I almost have the problems resolved.

Heres the code, after it is my issue:

#include <Servo.h>

Servo servo;
Servo servo2;

const int mid = 70;
const int mid2 = mid-3;

const int st = 110;
const int st2 = -st+3;

const int bk = -110;
const int bk2 = st-3;

const int btnPinA = 3;     // the number of the pushbutton pin
const int btnPinB =  4;      // the number of the LED pin

// variables will change:
int btnStateA = 0;
int btnStateB = 0;

void setup() {
  servo.attach(5);//right servo viewing from back
  servo2.attach(6);//left servo viewing from back
  
  pinMode(btnPinA, INPUT);
  pinMode(btnPinB, INPUT);  
}

void loop(){
  // read the state of the pushbuttons values
  btnStateA = digitalRead(btnPinA);
  btnStateB = digitalRead(btnPinB);

  if (btnStateA == HIGH && btnStateB == HIGH) {     
     servo.write(bk);
     servo2.write(bk2);
     delay(500);
  } 
  else
  if (btnStateB == LOW && btnStateB == HIGH) {
     servo.write(st);
     servo2.write(bk2);
     delay(500);
  }
  else
  if (btnStateB == HIGH && btnStateA == LOW) {
     servo.write(bk);
     servo2.write(st2);
     delay(500);
  }
  else
  if (btnStateB == LOW && btnStateA == LOW) {
     servo.write(st);
     servo2.write(st2);
     delay(500);
  }
}

What should happen
If the left button is pressed it should turn right.
If the right button is pressed it should turn left.
If both buttons are pressed it should go backward.
If no buttons are pushed it should go straight.

Heres what happens that shouldnt
when the left button is pressed both servos go backward.
when the right button is pressed both servos go backward.

Heres what happens that should
When no buttons are pushed both servos go forward.
When both buttons are push both servos go backward.

You've declared st bk wrong. Or more specifically, the integers with 2 postfix.

servo.write(st); //110
servo2.write(bk2); //107

trust me ints arent the problem, the 2 servos are 3 units off, so this is right

servo.write(st); //110
servo2.write(bk2); //107

the problem is something to do with the booleans and/or if then else statements.

That code is copy-pasted from a block where you want the wheel to spin in opposite directions.
Is 110 and 107 the correct values for making the go opposite directions?

If that's the case, then I was wrong.

The servos are on the opposite sides of the robot, so if they are going the same direction they are moving opp. dir. when on opp sides of a robot. Do you see any issue in the loop, disregard the ints, i have them right because one servo has to have negated directions as the other for the robot to go straight. The negated servo is also about 3 units off from the other. thats why they are 110 and 107.

Any problems in anything else, because logic says this should work.

I need some sleep now, but try to make every if like this:

if ( (btnStateA == HIGH) && (btnStateB == HIGH) ) {/*...*/}

You code looks good, the again it's 01:41 I might overlook something...

Good night, and good luck!

IM in america, its only 7:44 PM, good night and ill try what you said

I did what you said about the if statements and it didn't work. Also i found one typo and fixed it. Anyway because of this i figured it must be a hardware problem so i re-wired the two buttons and realized that i was using one resistor to ground for both of the buttons grounds, a novice mistake. I fixed that and the robot works perfectly as intended, yey!

Debugging working code is always hard :wink: Congratulations on getting it to work!