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.
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?
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 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!