OK, I got both servos moving the amount I want them to move, but I need to reverse the right servo. I've tried for a few hours now and can't figure it out. Can someone help me to reverse it?
#include <Servo.h>
// --------CONSTANTS (won't change)---------------
const int onBoardLedPin ; // the pin numbers for the LEDs
const int led_A_Pin = 12;
const int led_B_Pin = 11;
const int buttonLed_Pin = 13;
const int buttonPin = 2; // the pin number for the button
const int onBoardLedInterval = 500; // number of millisecs between blinks
const int led_A_Interval = 2500;
const int led_B_Interval = 4500;
const int blinkDuration = 500; // number of millisecs that Led's are on - all three leds use this
const int buttonInterval = 300; // number of millisecs between button readings
const int leftServoPin = 9; // the pin number for the servo signal
const int rightServoPin = 10; // the pin number for the servo signal
const int leftServoMinDegrees = 90; // the limits to servo movement
const int leftServoMaxDegrees = 120;
const int rightServoMinDegrees = 90; // the limits to servo movement
const int rightServoMaxDegrees = 120;
Servo leftServo;
Servo rightServo;
//------------ VARIABLES (will change)---------------------
byte onBoardLedState = LOW; // used to record whether the LEDs are on or off
byte led_A_State = LOW; // LOW = off
byte led_B_State = LOW;
byte buttonLed_State = LOW;
int leftServoPosition = 90; // the current angle of the servo - starting at 90.
int leftServoSlowInterval = 50; // millisecs between servo moves
int leftServoFastInterval = 0;
int leftServoInterval = leftServoSlowInterval; // initial millisecs between servo moves
int leftServoDegrees = 1; // amount servo moves at each step
// will be changed to negative value for movement in the other direction
int rightServoPosition = 90; // the current angle of the servo - starting at 90.
int rightServoSlowInterval = 50; // millisecs between servo moves
int rightServoFastInterval = 50;
int rightServoInterval = rightServoSlowInterval; // initial millisecs between servo moves
int rightServoDegrees = 1; // amount servo moves at each step
// will be changed to negative value for movement in the other direction
unsigned long currentMillis = 0; // stores the value of millis() in each
unsigned long leftPreviousServoMillis = 0; // the time when the servo was last moved
unsigned long rightPreviousServoMillis = 0;
unsigned long previousOnBoardLedMillis = 0; // will store last time the LED was updated
unsigned long previousLed_A_Millis = 0;
unsigned long previousLed_B_Millis = 0;
unsigned long previousButtonMillis = 0; // time when button press last checked
void setup() {
pinMode(onBoardLedPin, OUTPUT);
pinMode(led_A_Pin, OUTPUT);
pinMode(led_B_Pin, OUTPUT);
pinMode(buttonLed_Pin, OUTPUT);
leftServo.write(leftServoPosition); // sets the initial position
rightServo.write(rightServoPosition); // sets the initial position
leftServo.attach(leftServoPin);
rightServo.attach(rightServoPin);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
// use the same time for all LED flashes to keep them synchronized
readButton(); // call the functions that do the work
updateOnBoardLedState();
updateLed_A_State();
updateLed_B_State();
switchLeds();
leftServoSweep();
rightServoSweep();
}
void leftServoSweep() { /////////////////////////////////////////////////////
// nothing happens unless the interval has expired
// the value of currentMillis was set in loop()
if (currentMillis - leftPreviousServoMillis >= leftServoInterval) {
// its time for another move
leftPreviousServoMillis = leftPreviousServoMillis + leftServoInterval; //increments leftServoInterval
leftServoPosition = leftServoPosition + leftServoDegrees; // leftServoDegrees might be negative
if ((leftServoPosition >= leftServoMaxDegrees) || (leftServoPosition <= leftServoMinDegrees)) {
// if the servo is at either extreme change the sign of the degrees to make it move the other way
leftServoDegrees = - leftServoDegrees; // reverse direction
// and update the position to ensure it is within range
leftServoPosition = leftServoPosition + leftServoDegrees;
}
// make the servo move to the next position
leftServo.write(leftServoPosition);
// and record the time when the move happened
}
}
void rightServoSweep() { //////////////////////////////////////
// this is similar to the servo sweep example except that it uses millis() rather than delay()
// nothing happens unless the interval has expired
// the value of currentMillis was set in loop()
if (currentMillis - rightPreviousServoMillis >= rightServoInterval) {
// its time for another move
rightPreviousServoMillis += rightServoInterval;
rightServoPosition = rightServoPosition + rightServoDegrees; // servoDegrees might be negative
if (rightServoPosition <= rightServoMinDegrees) {
// when the servo gets to its minimum position change the interval to change the speed
if (rightServoInterval == rightServoSlowInterval) {
rightServoInterval = rightServoFastInterval;
}
else {
rightServoInterval = rightServoSlowInterval;
}
}
if ((rightServoPosition >= rightServoMaxDegrees) || (rightServoPosition <= rightServoMinDegrees)) {
// if the servo is at either extreme change the sign of the degrees to make it move the other way
rightServoDegrees = - rightServoDegrees; // reverse direction
// and update the position to ensure it is within range
rightServoPosition = rightServoPosition + rightServoDegrees;
}
// make the servo move to the next position
rightServo.write(rightServoPosition);
// and record the time when the move happened
}
}
void updateOnBoardLedState() {
if (onBoardLedState == LOW) {
// if the Led is off, we must wait for the interval to expire before turning it on
if (currentMillis - previousOnBoardLedMillis >= onBoardLedInterval) {
// time is up, so change the state to HIGH
onBoardLedState = HIGH;
// and save the time when we made the change
previousOnBoardLedMillis += onBoardLedInterval;
// NOTE: The previous line could alternatively be
// previousOnBoardLedMillis = currentMillis
// which is the style used in the BlinkWithoutDelay example sketch
// Adding on the interval is a better way to ensure that succesive periods are identical
}
}
else { // i.e. if onBoardLedState is HIGH
// if the Led is on, we must wait for the duration to expire before turning it off
if (currentMillis - previousOnBoardLedMillis >= blinkDuration) {
// time is up, so change the state to LOW
onBoardLedState = LOW;
// and save the time when we made the change
previousOnBoardLedMillis += blinkDuration;
}
}
}
//========================================
void updateLed_A_State() {
if (led_A_State == LOW) {
if (currentMillis - previousLed_A_Millis >= led_A_Interval) {
led_A_State = HIGH;
previousLed_A_Millis += led_A_Interval;
}
}
else {
if (currentMillis - previousLed_A_Millis >= blinkDuration) {
led_A_State = LOW;
previousLed_A_Millis += blinkDuration;
}
}
}
//========================================
void updateLed_B_State() {
if (led_B_State == LOW) {
if (currentMillis - previousLed_B_Millis >= led_B_Interval) {
led_B_State = HIGH;
previousLed_B_Millis += led_B_Interval;
}
}
else {
if (currentMillis - previousLed_B_Millis >= blinkDuration) {
led_B_State = LOW;
previousLed_B_Millis += blinkDuration;
}
}
}
//========================================
void switchLeds() {
// this is the code that actually switches the LEDs on and off
digitalWrite(onBoardLedPin, onBoardLedState);
digitalWrite(led_A_Pin, led_A_State);
digitalWrite(led_B_Pin, led_B_State);
digitalWrite(buttonLed_Pin, buttonLed_State);
}
//========================================
void readButton() {
if (millis() - previousButtonMillis >= buttonInterval) {
if (digitalRead(buttonPin) == LOW) {
buttonLed_State = ! buttonLed_State; // this changes it to LOW if it was HIGH
// and to HIGH if it was LOW
previousButtonMillis += buttonInterval;
}
}
}