Hello,
i can not get my servo to work at all, (and yes the wire is correct)
when i upload my program the servo moves around. Can someone explain that??
everything else runs just fine, LED lights, my counter, but for the love god i cant get the servo to work.
i tried running the servo on it's in another program and same thing happens, moves around when
uploading but nothing besides that.
please help!!!
#include <Servo.h>
Servo servo;
const int MaxNum = 5; // number times beam can be tripped
const int LEDPIN = 6; // Red LED
const int LEDPIN2 = 8; // Green LED
const int SENSORPIN = 4; // Servo Pin
const int RTime = 150; // delay time for Red LED
const int GTime = 150; // delay time for Green LED
const int Stime = 500; // delay time for Servo
const int Angle = 60; // max angle Servo moves
int sensorState = 0; // variable for reading the pushbutton status
void setup() {
servo.attach(2);
pinMode(LEDPIN2, OUTPUT);
pinMode(LEDPIN, OUTPUT);
pinMode(SENSORPIN, INPUT);
digitalWrite(SENSORPIN, HIGH);
}
int count = 0;
void loop(){
sensorState = digitalRead(SENSORPIN); // check if the sensor beam is broken
if (count < MaxNum) {
digitalWrite(LEDPIN2, HIGH);
if (sensorState == LOW) {
for (int x = 0; x <= 5; x ++) { // loop for LED.
digitalWrite(LEDPIN2, HIGH);
delay(GTime);
digitalWrite(LEDPIN2, LOW);
delay(GTime);
}
// runs Servo motor.
for (int pos = 0; pos < Angle; pos++) {
servo.write(pos);
}
for (int pos = Angle; pos > 0; pos--) {
servo.write(0);
}
count++;
}
else { // if beam is detected
servo.detach();
}
} else if (count == MaxNum) { // if the max count has been reached
digitalWrite(LEDPIN, HIGH);
servo.detach();
}
}