I would like to move the servo by a fractional amount if possible so I can fine tune the throttle movement. 1 is too little and 2 is too much.
I changed the move amount to a float and tried different amounts but it does not work.
float moveServoAmount = 1; // move the servo 1 degrees per 1/125 pulse
here is where the movement takes place.
if (encoder_ThrottleUp == encoder_ThrottleDn) {
if( + moveServoAmount <= 255) servoPos += moveServoAmount; // rotated forward,
}
else {
if(servoPos - moveServoAmount >= 20) servoPos -= moveServoAmount; // B is low so counter-clockwise
}
#include <Bounce.h>
#include <Servo.h>
void setup(){
buttonSetup();
throttleSetup();
}
void loop(){
throttleLoop();
cruiseButtonLoop();
}
unsigned long startPress=0;
Servo throttleServo; // Define throttle Servo
int servoPos = 30; // Initial power up “servo position”
float moveServoAmount = 1; // move the servo 1 degrees per 1/125 pulse
const int pin_ThrottleUp = 4;
const int pin_ThrottleDn = A2; // pin 14
unsigned char encoder_ThrottleUp;
unsigned char encoder_ThrottleDn;
unsigned char encoder_ThrottleUp_prev=0;
// set pin numbers:
const int buttonPin = 11; // the number of the pushbutton pin
const int buttonPin2 = 10;
const int buttonPin3 = 8;
const int buttonPin4 = 9;
long debounceDelay = 200; // the debounce time; increase if the output flickers
//Debounce objects
// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce cruiseUp = Bounce(buttonPin, debounceDelay);
Bounce cruiseDn = Bounce(buttonPin2, debounceDelay);
Bounce brakeReleaseToThrottle = Bounce(buttonPin4, debounceDelay);
Bounce clutchReleaseToThrottle = Bounce(buttonPin3, debounceDelay);
void throttleSetup() {
throttleServo.attach(12); // servo on digital pin 12
throttleServo.write(30);
pinMode(pin_ThrottleUp, INPUT);
digitalWrite(pin_ThrottleUp, HIGH);// turn on pullup resistor
pinMode(pin_ThrottleDn, INPUT);
digitalWrite(pin_ThrottleDn, HIGH);// turn on pullup resistor
}
void throttleLoop() {
encoder_ThrottleUp = digitalRead(pin_ThrottleUp); // Read encoder pins
encoder_ThrottleDn = digitalRead(pin_ThrottleDn); // Read encoder pins
if(encoder_ThrottleUp != encoder_ThrottleUp_prev) {
if (encoder_ThrottleUp == encoder_ThrottleDn) {
if( + moveServoAmount <= 255) servoPos += moveServoAmount; // rotated forward,
}
else {
if(servoPos - moveServoAmount >= 20) servoPos -= moveServoAmount; // B is low so counter-clockwise
}
}
encoder_ThrottleUp_prev = encoder_ThrottleUp; // 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 cruiseButtonLoop(){
//Update debounce tool
cruiseUp.update();
cruiseDn.update();
brakeReleaseToThrottle.update();
clutchReleaseToThrottle.update();
//Do not need to update these here are they are not used
//bouncer3.update();
//bouncer4.update();
if (cruiseUp.read() == HIGH)
{
cruiseLoop(); //Enters button control loop
}
if (cruiseDn.read() == HIGH)
{
cruiseLoop(); //Enters button control loop
}
else if (brakeReleaseToThrottle.read() == HIGH)
{
servoPos = 30;
}
}
/**
* 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 cruiseLoop(){
servoPos = 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
cruiseUp.update();
cruiseDn.update();
brakeReleaseToThrottle.update();
clutchReleaseToThrottle.update();
if (cruiseUp.read() == HIGH)
{
throttleServo.write(servoPos + 1); //Add 2 degrees to servo position for increased motor rpm
servoPos = throttleServo.read();
delay(200); //allows time for switch ro reset
}
//If first button not pressed, check the next...
else if (cruiseDn.read() == HIGH)
{
throttleServo.write(servoPos - 1); //Subtract 2 degrees to servo position for decreased motor rpm
servoPos = throttleServo.read(); //Read new servo position
delay(200); //allows time for switch ro reset
}
else if (clutchReleaseToThrottle.read() == LOW & (brakeReleaseToThrottle.read() == LOW))
{
startPress = 0;
}
else if (startPress == 0){
startPress = millis();
}
else if (millis() - startPress > 500)
{
servoPos = 30;
btnControlEnabled = 0; //Set loop exit condition
throttleLoop();
}
}
}