Hello, trying to control two servo motors with two rocker switches and a toggle switch. When the toggle switch is high it controls each motor individually, when the toggle is low it controls them together but in opposite motions. I'm getting a problem where the motors are vibrating and one turns consistently, the rocker switches work but then when pressed they almost latch on and take a long time before returning to a low value.
All criticism welcome, I'm trying my best to learn as I go so will write down anything good for future me. If you could explain why you changed parts as well I'd really appreciate it.
//define pins
#define dir1Pin 2
#define step1Pin 3
#define dir2Pin 4
#define step2Pin 5
#define tog 8
#define rock1Up 10
#define rock1Down 9
#define rock2Up 11
#define rock2Down 12
#define stepsPerRevolution 1600
int switchState=0;
int rock1UpState=0;
int rock1DownState=0;
int rock2UpState=0;
int rock2DownState=0;
void setup() {
// declare inputs and outputs
pinMode(dir1Pin, OUTPUT);
pinMode(step1Pin, OUTPUT);
pinMode(dir2Pin, OUTPUT);
pinMode(step2Pin, OUTPUT);
pinMode(tog, INPUT);
pinMode(rock1Up, INPUT);
pinMode(rock1Down, INPUT);
pinMode(rock2Up, INPUT);
pinMode(rock2Down, INPUT);
}
void loop() {
switchState=digitalRead(tog); //read if running individual or together
//If selected run individually
if(switchState==HIGH){
rock1UpState=digitalRead(rock1Up);
rock1DownState=digitalRead(rock1Down);
rock2UpState=digitalRead(rock2Up);
rock2DownState=digitalRead(rock2Down);
//operator presses rocker switch to try to move right side up
if(rock1UpState==HIGH){
//set direction to CCW
digitalWrite(dir1Pin, LOW);
digitalWrite(step1Pin, HIGH);
//set speed
delayMicroseconds(500);
digitalWrite(step1Pin, LOW);
delayMicroseconds(500);
}
//operator presses rocker switch to try to move right side down
if(rock1DownState==HIGH){
//set direction to CW
digitalWrite(dir1Pin, HIGH);
digitalWrite(step1Pin, HIGH);
//set speed
delayMicroseconds(500);
digitalWrite(step1Pin, LOW);
delayMicroseconds(500);
}
//operator presses rocker switch to try to move left side up
if(rock2UpState==HIGH){
//set direction to CCW
digitalWrite(dir2Pin, LOW);
digitalWrite(step2Pin, HIGH);
delayMicroseconds(500);
digitalWrite(step2Pin, LOW);
delayMicroseconds(500);
}
//operator presses rocker switch to try to move left side down
if(rock2DownState==HIGH){
//set direction to CW)
digitalWrite(dir2Pin, HIGH);
digitalWrite(step2Pin,HIGH);
delayMicroseconds(500);
digitalWrite(step2Pin, LOW);
delayMicroseconds(500);
}
}
//if selected to run together
if(switchState==LOW){
rock1UpState=digitalRead(rock1Up);
rock1DownState=digitalRead(rock1Down);
rock2UpState=digitalRead(rock2Up);
rock2DownState=digitalRead(rock2Down);
if(rock1UpState==HIGH or rock2DownState==HIGH){
digitalWrite(dir1Pin, LOW);
digitalWrite(step1Pin, HIGH);
delayMicroseconds(500);
digitalWrite(step1Pin, LOW);
delayMicroseconds(500);
digitalWrite(dir2Pin, HIGH);
digitalWrite(step2Pin,HIGH);
delayMicroseconds(500);
digitalWrite(step2Pin, LOW);
delayMicroseconds(500);
}
if(rock2UpState==HIGH or rock1DownState==HIGH){
digitalWrite(dir1Pin, HIGH);
digitalWrite(step1Pin, HIGH);
delayMicroseconds(500);
digitalWrite(step1Pin, LOW);
delayMicroseconds(500);
digitalWrite(dir2Pin, LOW);
digitalWrite(step2Pin, HIGH);
delayMicroseconds(500);
digitalWrite(step1Pin, LOW);
delayMicroseconds(500);
}
}
}