Programa 1
2 LED rojos y 1 LED verde se encienden durante 4 segundos y luego se apagan durante 4 segundos. Rojo es entonces alta durante 4 segundos y luego baja durante 4 segundos y luego se repite.
int LedR = 10;
int LedG = 11;
void setup() {
// put your setup code here, to run once:
pinMode (LedR, OUTPUT);
pinMode (LedG, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite (LedG, HIGH); //Light Greed Led
delay (4000); //delay 4s
digitalWrite(LedG, LOW);//Low Greed Led
delay (4000); //delay 4s
digitalWrite(LedR, HIGH);//Light Red Led
delay (4000); //delay 4s
digitalWrite (LedR, LOW); //Low Greed Led
delay (4000); //delay 4s`
}
Programa 2
El servo gira 20 grados espera 4 segundos vuelve a 0 grados espera 4 segundos y repite.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
delay(4000);
for (pos = 0; pos <= 20; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}delay(4000);
for (pos = 20; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Ambos programas funcionan de forma independiente pero son imposibles de integrar ya que el Arduino no permite bucles múltiples.
He intentado aprender la función millis sin éxito.
¿Hay alguna forma de conseguir que los LEDs se enciendan consecutivamente al mismo tiempo que gira el servo?
Por favor, ayúdeme. Este es el código que no funciona:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
int LedR = 10;
int LedG = 11;
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode (LedR,OUTPUT);
pinMode (LedG,OUTPUT);
}
void loop() {
digitalWrite (LedG, HIGH); //Light Greed Led
delay (4000); //delay 4s
digitalWrite(LedG, LOW);//Low Greed Led
delay (4000); //delay 4s
digitalWrite(LedR, HIGH);//Light Red Led
delay (4000); //delay 4s
digitalWrite (LedR, LOW); //Low Greed Led
delay (4000); //delay 4s
delay(4000);