I have a code to controll a servo motor this is my main project
i would like to program this switch to control a LED
but the switch didn't work in my main project
this is the code
#include <Servo.h>
Servo myservo;
int pos = 0;
int buttonState = 0;
void setup() {
myservo.attach(9);
pinMode(12, OUTPUT); //led pin
pinMode(7, INPUT_PULLUP); // switch pin
}
void loop() {
buttonState = digitalRead(7);
if (buttonState == HIGH) {
digitalWrite(12, HIGH);
} else if (buttonState == LOW) {
digitalWrite(12, LOW);
}
for (pos = 0; pos <= 165; pos += 1) { /* goes from 0 degrees to 165 degrees in steps of 1 degree */
myservo.write(pos);
delay(25);
}
delay(3000);
for (pos = 165; pos >= 0; pos -= 1) { /* goes from 165 degrees to 0 degrees */
myservo.write(pos);
delay(25);
}
delay(3000);
}