Hey everyone,
For a project I am making a servo controlled security camera arm. the arm and camera serve as a prop, so the camera is not real.
I have made a arm with two servo’s in it. One to move horizontally (180*) and the other to move vertical (135*), controlled with a arduino uno.
-
The idea is that the camera is looking down to a person sitting on a chair.
-
when a input is given to the arduino (for now I am doing this with a switch), the camera will quickly look away, and will begin to look around facing up. As if it was not spying on the person.
-
when the input from the switch is removed, the camera will slowly go back to spying on the person in the chair. so facing down.
My problem is that i can’t get the camera to move correctly. it shoots down, in between tasks, and i don’t know why
my servo’s are connected to pin 10 and 9, and the switch is connected to pin 8.
and this is the code I am using:
If you know anything that could help I would be really greatfull, thanks!
const int buttonPin = 8; // the number of the pushbutton pin
#include <Servo.h>
Servo servotop; // create servo object to control a servo
Servo servobelow; // a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int buttonState = 0; // variable for reading the pushbutton status
void setup()
{
pinMode(buttonPin, INPUT);
servotop.attach(9);
servobelow.attach(10);
// attaches the servotop on pin 9 and servobelow on pin 10 to the servo object
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH)
{
// turn servotop on (look away):
for(pos = 30; pos < 150; pos += 1) // goes from 30 degrees to 150 degrees
{ // in steps of 1 degree
servotop.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 50ms for the servo to reach the position
}
for(pos = 150; pos < 30; pos += 1) // goes from 150 degrees to 30 degrees
{ // in steps of 1 degree
servotop.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 50ms for the servo to reach the position
}
// turn servobelow on (look away):
for(pos = 111; pos < 180; pos += 1) // goes from 111 degrees to 180 degrees
{ // in steps of 1 degree
servobelow.write(pos); // tell servo to go to position in variable ‘pos’
delay(50); // waits 50ms for the servo to reach the position
}
}
// return to start position (he will never know):
else {
{ // in steps of 1 degree
servotop.write(100); // tell servo to go to position in variable ‘pos’
delay(50); // waits …ms for the servo to reach the position
}
{ // in steps of 1 degree
servobelow.write(111); // tell servo to go to position in variable ‘pos’
delay(50); // waits …ms for the servo to reach the position
}
}
}