Servo Control with Push Button

 


              CODE

#include <Servo.h>
Servo myservo; // create servo object to control a servo
int servoPin = 9;// this pin must be of those with PWM ~
int pushButtonPin =2;// the pin where push button is connected
int angle =25; // initial angle for servo
int angleStep =5;

void setup() { // Servo button demo by Robojax.com
Serial.begin(9600);  // setup serial
myservo.attach(servoPin); // attaches the servo on pin 9 to the servo object
pinMode(pushButtonPin,INPUT_PULLUP); Serial.println("Robojax Servo Button "); //Full video details: https://youtu.be/qJebLpzLE24
  }
void loop() {
while(digitalRead(pushButtonPin) == LOW){
// change the angle for next time through the loop:
angle = angle + angleStep;
// reverse the direction of the moving at the ends of the angle:
if (angle <= 0 || angle >= 180) {
angleStep = -angleStep;
}
myservo.write(angle); // move the servo to desired angle
Serial.print("Moved to: "); Serial.print(angle); // print the angle
Serial.println(" degree"); delay(20); // waits for the servo to get there
}
}


Post a Comment

Previous Post Next Post

Contact Form