
23Chapter 1: The Foundation of Robotic Arms
#include <Servo.h>
Servo servoOne;
int btnOne;
int btnTwo;
int pos = 90;
void setup() {
Serial.begin(9600);
pinMode(2, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
servoOne.attach(3);
}
void loop() {
btnOne = digitalRead(2);
btnTwo = digitalRead(4);
if(btnOne == LOW){
pos++;
delay(50);
}
else if(btnTwo == LOW){
pos--;
delay(50);
}
servoOne.write(pos);
}
STEP 6
Now that we’ve added our second push-button, you should see that as you
press one push-button or the other, the servo will slowly move forward and
backward. We are now able to actively control the position of our motor
without having to upload new code. This piece of ...