Hello Everyone, I have been learning Arduino over the last month in order to incorporate it into one of my engineering and design projects.
I need to have a single button controlling a 180 degree servo. I need it to turn one way until it hits its limit of 5 degrees or the button is released. Then on the next press I need it to turn the other way until it hits its limit of 170 degrees or until the button is pressed in a cycle or loop.
I currently have the system working good with 2 basic push buttons that came in my Arduino kit right now with one controlling the clockwise direction and the other controlling the counter-clockwise direction. The single button that I will ultimately be using is a keyestudio button switch which I believe puts out a analog signal instead of completing the circuit.
Right now I am not really sure how to use the code that I have right now and convert it to be able to do the same thing with the new button switch.
Thank you All,
RK
Code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int angle =170; // initial angle for servo
int angleStep =5;
#define LEFT 12 // pin 12 is connected to left button
#define RIGHT 2 // pin 2 is connected to right button
void setup() {
Serial.begin(9600); // setup serial
myservo.attach(8); // attaches the servo on pin 9 to the servo object
pinMode(LEFT,INPUT_PULLUP); // assign pin 12 ass input for Left button
pinMode(RIGHT,INPUT_PULLUP);// assign pin 2 as input for right button
myservo.write(angle);// send servo to the middle at 90 degrees
Serial.println("Group 1");
}
void loop() {
while(digitalRead(RIGHT) == LOW){
if (angle > 5 && angle <= 180) {
angle = angle - angleStep;
if(angle < 0){
angle = 5;
}else{
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
}
}
delay(0); // waits for the servo to get there
}
while(digitalRead(LEFT) == LOW){
if (angle >= 5 && angle <= 170) {
angle = angle + angleStep;
if(angle >170){
angle =170;
}else{
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: ");
Serial.print(angle); // print the angle
Serial.println(" degree");
}
}
delay(1); // waits for the servo to get there
}//
}