-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbutton-servo-mover.ino
54 lines (40 loc) · 1.76 KB
/
button-servo-mover.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
author: Ammon Shepherd
date: 07 April 2020
title: button-servor-mover
desc: Use buttons to move a servo motor
license: Creative Commons Attribution-ShareAlike 4.0
International License
*/
#include <Servo.h> // Include the library for operating the servo motor
const int rightButton = 4; // Set the variable for the left button pin
int rbState; // Create a variable for state of right button, on (LOW) or off (HIGH)
const int leftButton = 2; // Set the variable for the left button pin
int lbState; // Create a variable for state of left button, on (LOW) or off (HIGH)
const int servoPin = 9; // Create the variable for the servo pin
int angle; // Create the variable for the servo motor angle
Servo myservo; // create servo object to control a servo
void setup() {
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
pinMode(leftButton, INPUT); // listen for data on the left button pin
pinMode(rightButton, INPUT); // listen for data on the right button pin
}
void loop() {
rbState = digitalRead(rightButton); // get the current state of the button
lbState = digitalRead(leftButton); // get the current state of the button
angle = myservo.read(); // get the current angle of the servo motor
// If the angle is between 0 and 180, then we can change it.
// Otherwise it would go too far in either direction.
if ( (angle > -1) && (angle < 181) ) {
//If the left button is pushed (causing a state of LOW), move the motor 10 degree positive
if (lbState == LOW) {
myservo.write(angle + 10);
delay(100);
}
// If the right button is pushed, move the motor 10 degrees negative
if (rbState == LOW) {
myservo.write(angle - 10);
delay(100);
}
}
}