In my code, I am controling a servo with a rotary encoder and 4 buttons. The encoder is acting like a pot. I could not get smooth servo movement with the pot so am trying this. It works really well. Only issue at the moment is that when the buttons take over control of the servo, the pot is allowed to spring back to 0 degree and therefore when the button control is released back to the Pot, the servo knows its position.
With the encoder however, when it springs back to its position and the button releases control, the servo goes back to its last known position before the puttons took over.
I have tried adding another loop to write to the servo a specific angle. With a delay it will do this but then goes right back to the last known angle. I
I tried to redirect to the encoder loop but same thing.
at this point in the code is where the buttons are released
else if (bouncer3.read() == HIGH)
{
btnControlEnabled = 0; //Set loop exit condition
throttleServo.write(180);
}
else if (bouncer4.read() == HIGH)
{
throttleServo.write(180);
btnControlEnabled = 0; //Set loop exit condition
}
//If nothing pressed...
else
{
//...do nothing at all, go back to start of loop
#include <Bounce.h>
#include <Servo.h>
void setup(){
buttonSetup();
throttleSetup();
}
void loop(){
throttleLoop();
buttonLoop();
}
int servoPosition = 20; //Old val1, used to store servo position. Allows use by whole program
Servo throttleServo; // Define throttle Servo
int servoPos = 0; // Initial power up “servo position”
int moveServoAmount = 7; // move the servo 10 degrees per 1/24 pulse
const int pin_A = A0; // pin 15
const int pin_B = A1; // pin 14
unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev=0;
// set pin numbers:
const int buttonPin = 10; // the number of the pushbutton pin
const int buttonPin2 = 8;
const int buttonPin3 = 4;
const int buttonPin4 = 2;
long debounceDelay = 50; // the debounce time; increase if the output flickers
//Debounce objects
// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer1 = Bounce(buttonPin, debounceDelay);
Bounce bouncer2 = Bounce(buttonPin2, debounceDelay);
Bounce bouncer3 = Bounce(buttonPin3, debounceDelay);
Bounce bouncer4 = Bounce(buttonPin4, debounceDelay);
void throttleSetup() {
throttleServo.attach(12); // servo on digital pin 12
pinMode(pin_A, INPUT);
digitalWrite(pin_A, HIGH);// turn on pullup resistor
pinMode(pin_B, INPUT);
digitalWrite(pin_B, HIGH);// turn on pullup resistor
}
void throttleLoop() {
encoder_A = digitalRead(pin_A); // Read encoder pins
encoder_B = digitalRead(pin_B);
if((!encoder_A) && (encoder_A_prev)){
// A has gone from high to low
if(encoder_B) {
// B is high so clockwise
// increase the throttle, dont go over 255
if(servoPos + moveServoAmount <= 220) servoPos += moveServoAmount;
}
else {
// B is low so counter-clockwise
// decrease the throttle, dont go below 20
if(servoPos - moveServoAmount >= 20) servoPos -= moveServoAmount;
}
}
encoder_A_prev = encoder_A; // Store value of A for next time
// set the new location of servo:
throttleServo.write(servoPos);
}
void buttonSetup(){
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
}
void buttonLoop(){
//Update debounce tool
bouncer1.update();
bouncer2.update();
//Do not need to update these here are they are not used
//bouncer3.update();
//bouncer4.update();
if (bouncer1.read() == HIGH)
{
serloop1(); //Enters button control loop
}
if (bouncer2.read() == HIGH)
{
serloop1(); //Enters button control loop
}
}
/**
* If button control is enabled, loop and handle control buttons
* If exit buttons (To return to pot control) are pressed, exit loop and return
* to pot control
*/
void serloop1(){
servoPosition = throttleServo.read(); //reads current servo location
int btnControlEnabled = 1; //If button control is enabled this equals 1, else it equals 0
while(btnControlEnabled == 1)
{
//Update all debounce tools
bouncer1.update();
bouncer2.update();
bouncer3.update();
bouncer4.update();
if (bouncer1.read() == HIGH)
{
throttleServo.write(servoPosition + 2); //SUBTRACT 2 degrees to servo position for increased motor rpm
servoPosition = throttleServo.read(); //Read new servo position
delay(40); //allows time for switch ro reset
}
//If first button not pressed, check the next...
else if (bouncer2.read() == HIGH)
{
throttleServo.write(servoPosition - 2); //ADDS 2 degrees to servo position for decreased motor rpm
servoPosition = throttleServo.read(); //Read new servo position
delay(40); //allows time for switch ro reset
}
else if (bouncer3.read() == HIGH)
{
btnControlEnabled = 0; //Set loop exit condition
throttleServo.write(180);
}
else if (bouncer4.read() == HIGH)
{
throttleServo.write(180);
btnControlEnabled = 0; //Set loop exit condition
}
//If nothing pressed...
else
{
//...do nothing at all, go back to start of loop
}
}
}