Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

controlledMotion example for servo library #40

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
44 changes: 44 additions & 0 deletions examples/ControlledMotion/ControlledMotion.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Servo Demo

As Long as the button is pushed down the servo will move
in a counter-clockwise direction in a stepsize of one
degree till it reaches 180 degrees and then returns to
start from the zero position automatically.

The circuit:
* Servo GND pin to ground
* Servo power pin to 5V
* Servo signal pin to digital pin 9
* Button:
* Terminal 1b to ground
* Terminal 2b to digital pin 2

- A graphical representation of the circuit can be found in cotrolledMotion.png

created 12 Mar 2020

by Mohamed Lotfy
*/

#include <Servo.h>

Servo myservo; // create servo object to control a servo
int servoPos; // variable to store the servo position
int buttonState = 0; // variable to track the button state
int buttonPin = 2; // button attached to input pin 2 of the board


void setup() {
pinMode(buttonPin, INPUT_PULLUP); // set the button pin in the board to be an input pin
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop() {
buttonState = digitalRead(buttonPin); // store the button state
myservo.write(servoPos); // set the servo position with the intended position
if(buttonState == LOW) { // increment the servo position angle if the button is pressed
servoPos = (servoPos+1) % 180; // set the servo position to zero if it exceeds 180 degrees
delay(50); // damp the button press effect
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.