Summary - I have a sketch that I've used successfully for weeks that contains some 'if' , 'else if' code. When I modified it by adding two 'for' statements following the 'if' and the 'else if' things went sideways on me. That modified code does not work as I expected. I'm hoping someone on the forum can educate me about what I did wrong and what I can do to make it work as I intended.
Wiring detail - I have a spdt toggle switch connected to digital inputs on an UNO. Common on the toggle switch is connected to ground on the UNO; toggle pin that I call Tog1UpPin is connected to pin 0 on the UNO; toggle pin that I call Tog1DnPin is connected to pin 1 on the UNO. The UNO is connected to a PCA9685 servo driver. I have a servo that I call ServoA connected to pin 0 on the PCA9685.
Sequence of operation - With Tog1UpPin thrown, ServoA rapidly moves to a fixed point of rotation (280) AND HOLDS THAT POSITION. With Tog1DnPin thrown, ServoA rapidly moves to a fixed point of rotation (300) AND HOLDS THAT POSITION. As I throw the toggle between the Up and Dn positions, the servo responds and holds it appropriate position until toggle thrown in the opposite direction.
The Fast sketch - The sketch described above is shown below. It's the first of two sketches. I'll name it "Fast sketch".
I decided to try to modify my Fast sketch to slow down the speed of the servo as it moves from 300 to 280 and back. This is where things fell apart for me.
Modified sketch which I call "Slow sketch" sequence of operation - Same as sequence of operation of Slow sketch except servo speed reduced to 1 step every 30 ms. To accomplish this I added the statement for(pos=300;pos>=280;pos-=1) after the 'if' statement and for(pos=280;<=300; pos+1) after the 'else if' statement.
The modified sketch - This sketch (Slow sketch) is the second sketch shown below.
How the Slow sketch functions - With the Tog1UpPin thrown, the servo slowly moves to position 280, then rapidly returns to position 300, then slowly moves to position 280, then rapidly returns to position 300. The servo will continue this 'cycling' pattern until Tog1DnPin is thrown. When Tog1DnPin is thrown, the servo stops and no longer responds.
FINALLY, MY QUESTION - Thank you for having the patience to continue. Why doesn't the servo in Fast sketch respond exactly as it does in Slow sketch, with the exception being the speed of response?
Thank you for your patience in reading my long post and for any suggestions you can offer.
Regards
Rick
FAST SKETCH
[code]
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm=Adafruit_PWMServoDriver();
int Tog1UpPin=0;
int Tog1DnPin=1;
int ServoA=0;
bool Tog1UpPinstatus;
bool Tog1DnPinstatus;
void setup() {
pwm.begin();
pwm.setPWMFreq(60);
pinMode (Tog1UpPin,INPUT_PULLUP);
pinMode (Tog1DnPin,INPUT_PULLUP);
Tog1UpPinstatus=digitalRead(Tog1UpPin);
Tog1DnPinstatus=digitalRead(Tog1DnPin);
}
void loop() {
Tog1UpPinstatus=digitalRead(Tog1UpPin); // Toggle switch in the UP position
Tog1DnPinstatus=digitalRead(Tog1DnPin); // Toggle switch in the DOWN position
if (Tog1UpPinstatus==LOW)
{
pwm.setPWM(ServoA,0,280);
}
else if (Tog1DnPinstatus==LOW)
{
pwm.setPWM(ServoA,0,300);
}
}
[/code]
``
``
SLOW SKETCH
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm=Adafruit_PWMServoDriver();
int Tog1UpPin=0;
int Tog1DnPin=1;
int ServoA=0;
int pos=0;
bool Tog1UpPinstatus;
bool Tog1DnPinstatus;
void setup() {
pwm.begin();
pwm.setPWMFreq(60);
pinMode (Tog1UpPin,INPUT_PULLUP);
pinMode (Tog1DnPin,INPUT_PULLUP);
Tog1UpPinstatus=digitalRead(Tog1UpPin);
Tog1DnPinstatus=digitalRead(Tog1DnPin);
}
void loop() {
Tog1UpPinstatus=digitalRead(Tog1UpPin); // Toggle switch in the UP position
Tog1DnPinstatus=digitalRead(Tog1DnPin); // Toggle switch in the DOWN position
if(Tog1UpPinstatus==LOW)
{
for(pos=300;pos>=200;pos-=1)
{
pwm.setPWM(ServoA,0,pos);
delay (30);
}
}
else if(Tog1DnPinstatus==LOW)
{
for(pos=200;pos>=300;pos+=1)
{
pwm.setPWM(ServoA,0,pos);
delay (30);
}
}
}