I am trying to write a program for my arduino to control the servo setting the throttle on my kicker motor. I have the following conditions that I am trying to set up. The pot controls the servo position. Fairly simple and is working fine. The next two conditions are to do with the two momentary buttons that I am trying to set up. Button 1 when pushed sets the servo position to idle speed and holds it there until asked to return to the preset pot speed. Button 2 (Resume) when pushed sets the servo position to the preset pot detemined speed thus resuming the trolling speed that the kicker was set at before I pushed Button 1 (Idle). Right now When I push button 2 I have to keep holding it to get the pot speed.
This is what my code looks like so far. Any help would be greatly appreciated as I am a complete newbie to arduinos. Thanks in advance.
/*
Controlling a servo position using a potentiometer (variable resistor)
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer for "Throttle Servo"
int val; // variable to read the value from the analog pin
int buttonPin1 = 6; // choose the input pin for a pushbutton to trigger "Throttle Idle"
int buttonPin2 = 7; // choose the input pin for a pushbutton to trigger "Throttle Resume"
int buttonVal1 = 0; // variable for reading the button1 status
int buttonVal2 = 0; // variable for reading the button2 status
int bounceCheck1 = 0; // variable for debouncing
int bounceCheck2 = 0; // variable for debouncing
int buttonState1; // variable for reading the pushbutton status
int buttonState2; // variable for reading the pushbutton status
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(20); // return servo to throttle idle position
pinMode(buttonPin1, INPUT); // declare pushbutton1 as input
pinMode(buttonPin2, INPUT); // declare pushbutton2 as input
Serial.begin(9600); // begin serial communication at 9600 baud
}
void loop()
{
buttonState1 = digitalRead(buttonPin1); // read the state of the pushbutton1 value:
if(buttonPin1 == HIGH); // if it is, the Idle buttonState1 is HIGH:
buttonVal1 = digitalRead(buttonPin1); // read input value from button1
delay(10); // wait 10ms
bounceCheck1 = digitalRead(buttonPin1); // check again
if((buttonVal1 == bounceCheck1)){ // if val is the same then not a bounce
if(buttonVal1 == HIGH && buttonState1 == 1) { // check if the input is HIGH
myservo.write(20); // return servo to throttle idle position
delay(20); // waits 20ms for the servo to get there
Serial.println("IDLE");
buttonState1 = 0;
}
{
buttonState2 = digitalRead(buttonPin2); // read the state of the pushbutton2 value:
if(buttonPin2 == HIGH); // if it is, the Idle buttonState1 is HIGH:
buttonVal2 = digitalRead(buttonPin2); // read input value from button1
delay(10); // wait 10ms
bounceCheck2 = digitalRead(buttonPin2); // check again
if(buttonVal2 == bounceCheck2) { // if val is the same then not a bounce
if(buttonVal2 == HIGH && buttonState2 == 1) { // check if the input is HIGH
myservo.write(val); // sets the servo position according to the scaled value
delay(20); // waits 20ms for the servo to get there
Serial.println("RUN");
buttonState1 = 0;
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 20, 50); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(20); // waits 20ms for the servo to get there
Serial.println(val); // send potVal 20-50 via serial
/*
Controlling a servo position using a potentiometer (variable resistor)
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer for "Throttle Servo"
int val; // variable to read the value from the analog pin
int buttonPin1 = 6; // choose the input pin for a pushbutton to trigger "Throttle Idle"
int buttonPin2 = 7; // choose the input pin for a pushbutton to trigger "Throttle Resume"
int buttonVal1 = 0; // variable for reading the button1 status
int buttonVal2 = 0; // variable for reading the button2 status
int bounceCheck1 = 0; // variable for debouncing
int bounceCheck2 = 0; // variable for debouncing
int buttonState1; // variable for reading the pushbutton status
int buttonState2; // variable for reading the pushbutton status
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(20); // return servo to throttle idle position
pinMode(buttonPin1, INPUT); // declare pushbutton1 as input
pinMode(buttonPin2, INPUT); // declare pushbutton2 as input
Serial.begin(9600); // begin serial communication at 9600 baud
}
void loop()
{
buttonState1 = digitalRead(buttonPin1); // read the state of the pushbutton1 value:
if (buttonPin1 == HIGH); // if it is, the Idle buttonState1 is HIGH:
buttonVal1 = digitalRead(buttonPin1); // read input value from button1
delay(10); // wait 10ms
bounceCheck1 = digitalRead(buttonPin1); // check again
if ((buttonVal1 == bounceCheck1)) { // if val is the same then not a bounce
if (buttonVal1 == HIGH && buttonState1 == 1) { // check if the input is HIGH
myservo.write(20); // return servo to throttle idle position
delay(20); // waits 20ms for the servo to get there
Serial.println("IDLE");
buttonState1 = 0;
}
{
buttonState2 = digitalRead(buttonPin2); // read the state of the pushbutton2 value:
if (buttonPin2 == HIGH); // if it is, the Idle buttonState1 is HIGH:
buttonVal2 = digitalRead(buttonPin2); // read input value from button1
delay(10); // wait 10ms
bounceCheck2 = digitalRead(buttonPin2); // check again
if (buttonVal2 == bounceCheck2) { // if val is the same then not a bounce
if (buttonVal2 == HIGH && buttonState2 == 1) { // check if the input is HIGH
myservo.write(val); // sets the servo position according to the scaled value
delay(20); // waits 20ms for the servo to get there
Serial.println("RUN");
buttonState1 = 0;
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 20, 50); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(20); // waits 20ms for the servo to get there
Serial.println(val); // send potVal 20-50 via serial
Hope that helps.
When I run the code it sits at "Throttle Idle" which is 20 on the servo position. If I push and hold Button2 "Throttle Resume" it will set the servo to the pot value. I would like it to set and hold the throttle positon at idle when button1 is pushed then resume to the pot value setting and allow speed changes when button2 is pushed.
What I need to learn is how to have it maintain state until a button (input) tells it to do something different then hold that state until another change is requested.
It might be easier to change the button to a maintain contact to accomplish this then add a power indication LED in the other buttons place but this is what I have for now.
You need a variable to hold the current value of the servo. Another variable to hold the value from the pot and a third to hold the idle value.
Then, depending on the buttons that are pressed you either copy the pot value or the idle value into the servo variable. All the time the servo responds to whatever is in the servo variable.
You might use code similar to the below where the pot code would go in the else statement.
//zoomkat servo button test 7-30-2011
//Powering a servo from the arduino usually *DOES NOT WORK*.
#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;
void setup()
{
pinMode(button1, INPUT);
servo1.attach(7);
digitalWrite(4, HIGH); //enable pullups to make pin high
}
void loop()
{
press1 = digitalRead(button1);
if (press1 == LOW)
{
servo1.write(160);
}
else {
servo1.write(20);
}
}