Hey guys,
I was just wondering if someone could help me with inputting the program code for a limit switch to start my servo motor, as i am completely new to this. I would like to also know on how to go about when my servo motor does one cycle (0 to 180), come back to its original postion of 0 degrees, and then stop completly.
My servo motor program increments by 10 degrees until it reaches 180.
Program Code-
//Create Servo Object
#include <Servo.h>
Servo davidsServo;
//Define Servo Pin
int servoPin = 9
void setup()
{
//"Attach" The Servo
davidsServo.attach(servoPin);
}
void loop()
{
//Turn the Servo from 0 to 180
//in increments of 10 degrees
for (int i = 0; i <=180; i=i+10)
{
davidsServo.write(i);
delay(1000);
}
}
Thanks a lot guys
I was just wondering if someone could help me with inputting the program code for a limit switch to start my servo motor, as i am completely new to this.
Use the digitalRead() function to determine the state of the limit switch. Exactly how to do so requires knowing how the switch is wired - which pin, whether or not external pull-up or pull-down resistors are used, etc. You seem to have omitted a few necessary details.
I would like to also know on how to go about when my servo motor does one cycle (0 to 180), come back to its original postion of 0 degrees, and then stop completly.
Use a boolean variable, initialized to true. In loop, execute the code to move the servo only if the boolean variable is true. When the servo move code completes, set the boolean variable to false.
boolean needToMoveMyServo = true;
void loop()
{
if(needToMoveMyServo)
{
// Move the servo
needToMoveMyServo = false;
}
}