/* Two button to control servo with Arduino UNO
20240427
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 90; // variable to store the servo position
//float pos = 0.0; // Variable where the arm's position will be stored (in degrees)
float step = 15.0; // Variable used for the arm's position step
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(1, INPUT_PULLUP); //used for button
pinMode(2, INPUT_PULLUP);
}
void loop() {
// for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// // in steps of 1 degree
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
// }
// for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
// myservo.write(pos); // tell servo to go to position in variable 'pos'
// delay(15); // waits 15ms for the servo to reach the position
// }
if (digitalRead(1)==LOW) // Check for the blue button input
{
if (pos>0) // Check that the position won't go higher than 180°
{
pos-=step; // Increment "pos" of "step" value
if(pos<0) pos=0;
myservo.write(pos); // Set the arm's position to "pos" value
delay(150); // button lasting time(ms)
}
}
if (digitalRead(2)==LOW) // Check for the blue button input
{
if (pos<180) // Check that the position won't go higher than 180°
{
pos+=step; // Increment "pos" of "step" value
if(pos>180) pos=180;
myservo.write(pos); // Set the arm's position to "pos" value
delay(150); // button lasting time(ms)
}
}
}