Hello. I am currently programming a project that uses a switch to control an LED and 2 servos. I want the LED to be steadily on throughout the entire duration of my servo motion but want the LED to turn off once both servos stop moving. Below is my code, why is my low digital write at the end not working for this task?
#include<Servo.h>// include servo library
Servo myservo1;
Servo myservo2;
int POWERON = 6; //switch in position 6 will power on system
int LED = 5; //LED is in pin 5
void setup() {
// put your setup code here, to run once:
myservo1.attach(10); //Servo 1 is attached to pin 10
myservo2.attach(9); //Servo 2 is attached to pin 9
pinMode(POWERON, INPUT_PULLUP);
//pinMode(POWEROFF,INPUT);
pinMode(LED, OUTPUT);
if (digitalRead(POWERON) == HIGH)
{
digitalWrite(LED, HIGH);
myservo1.write(45);
delay(3000);
myservo1.write(0);
delay(1000);
for (byte times = 0; times < 30; times++){
myservo2.write(0);
delay(500);
myservo2.write(180);
delay(500);
}
digitalWrite(LED, LOW);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
#include<Servo.h>// include servo library
Servo myservo1;
Servo myservo2;
int POWERON = 6; //switch in position 6 will power on system
int LED = 5; //LED is in pin 5
void setup() {
// put your setup code here, to run once:
myservo1.attach(10); //Servo 1 is attached to pin 10
myservo2.attach(9); //Servo 2 is attached to pin 9
pinMode(POWERON, INPUT_PULLUP);
//pinMode(POWEROFF,INPUT);
pinMode(LED, OUTPUT);
if (digitalRead(POWERON) == HIGH)
{
digitalWrite(LED, HIGH);
myservo1.write(45);
delay(3000);
myservo1.write(0);
delay(1000);
for (byte times = 0; times < 30; times++){
myservo2.write(0);
delay(500);
myservo2.write(180);
delay(500);
}
}
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED, LOW);
}
[/quote]
#include<Servo.h>// include servo library
Servo myservo1;
Servo myservo2;
int POWERON = 6; //switch in position 6 will power on system
int LED = 5; //LED is in pin 5
void setup() {
// put your setup code here, to run once:
myservo1.attach(10); //Servo 1 is attached to pin 10
myservo2.attach(9); //Servo 2 is attached to pin 9
pinMode(POWERON, INPUT_PULLUP);
//pinMode(POWEROFF,INPUT);
pinMode(LED, OUTPUT);
if (digitalRead(POWERON) == HIGH)
{
digitalWrite(LED, HIGH);
myservo1.write(45);
delay(3000);
myservo1.write(0);
delay(1000);
for (byte times = 0; times < 30; times++){
myservo2.write(0);
delay(500);
myservo2.write(180);
delay(500);
}
}
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(LED, LOW);
}
I took your original code, changed the LED to 13 to use the onboard LED, and added some Serial.prints to see that it was not freezing up. It compiles fine and the LED turns on and off as expected.
#include<Servo.h>// include servo library
Servo myservo1;
Servo myservo2;
int POWERON = 6; //switch in position 6 will power on system
int LED = 13; //LED is in pin 5
void setup() {
// put your setup code here, to run once:
myservo1.attach(10); //Servo 1 is attached to pin 10
myservo2.attach(9); //Servo 2 is attached to pin 9
pinMode(POWERON, INPUT_PULLUP);
//pinMode(POWEROFF,INPUT);
pinMode(LED, OUTPUT);
Serial.begin(115200);
if (digitalRead(POWERON) == HIGH)
{
digitalWrite(LED, HIGH);
myservo1.write(45);
Serial.print(".");
delay(3000);
myservo1.write(0);
Serial.print(".");
delay(1000);
for (byte times = 0; times < 30; times++){
myservo2.write(0);
Serial.print(".");
delay(500);
myservo2.write(180);
Serial.print(".");
delay(500);
}
Serial.print("done");
digitalWrite(LED, LOW);
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Perhaps there is something not correct about pin 5 wiring. Do you have a current limit resistor in series with the LED?