Good day.
I have to codes.
1st i needed 2 servos working togeher running in diffrant directions. Got that to work running in loop. The basic to what i want it to do. The code is as follow.
#include <Servo.h>
Servo servoLeft; // Define left servo
Servo servoRight; // Define right servo
int posLeft; //angle of left servo
int posRight; //angle of right servo
//opposite side servos rotate together
void setup() {
servoLeft.attach(8); // Set left servo to digital pin 8
servoRight.attach(9); // Set right servo to digital pin 9
}
void loop() { // Loop throught motion test
foward(); // Example: move foward
delay(2000); // Wait 1000 milliseconds (1 seconds)
reverse();
delay(2000);
}
//Motion routines for foward and reverse
void foward() {
servoLeft.write(10);
servoRight.write(170);
}
void reverse() {
servoLeft.write(170);
servoRight.write(10);
}
This works great.
Next i wanted to control with i switch. Got code from one of the topics and that works great.
#include <Servo.h>
const byte switchPinA = 2; //active low
const byte switchPinB = 3; //active low
const byte servoPin = 9;
Servo servo;
byte switchAstate = HIGH;
byte switchBstate = LOW;
byte servoPos = 20;
void setup() {
pinMode(switchPinA, INPUT_PULLUP);
pinMode(switchPinB, INPUT_PULLUP);
servo.attach(servoPin);
}
void loop () {
readButtons();
moveServo();
}
void readButtons() {
switchAstate = digitalRead(switchPinA);
switchBstate = digitalRead(switchPinB);
}
void moveServo() {
if (switchAstate == LOW) {
servoPos = 20;
}
if (switchBstate == LOW) {
servoPos = 110;
}
servo.write(servoPos);
delay(100);
}
Now i want to combine the and have the first code work with the switch idea in second code but with the 2 servos. In pos 1 move only foward and second position move reverse.
i am new to this been trying lost of thing using relay and got to this site bought my kit and love it. I can make my idea work just need help.