Hola que tal, recién comienzo con la programación para Arduino y bueno necesito ayuda, necesito encender una secuencia de tres leds con un pulsador, pero que el led 1 permanezca encendido cuando encienda el led2 y que el led 1 y 2 permanezcan encendidos cuando encienda el led 3 y que se paguen en el orden 1-2-3
Hello
Post your current sketch to see how we can help you.
// C++ code
//
// Pin assignement
const int btnPin = 8;
const int led1Pin = 9;
const int led2Pin = 10;
const int led3Pin = 11;
enum fcnMode {
OFF,
LED1,
LED2,
LED3,
FADE1,
ALL,
BLINK,
NBSTATE
}; // OFF = 0 and NBSTATE=7
int ledState1 = LOW, ledState2 = LOW, ledState3 = LOW; // ledState used to set the LED
unsigned long buttonState = 0;
int funcState = 0;
unsigned long currentMillis1, currentMillis2, currentMillis3; // will store current time
unsigned long previousMillis1, previousMillis2, previousMillis3; // will store last time LED was updated
const long interval1 = 100; // interval at which to blink (milliseconds)
const long interval2 = 300;
const long interval3 = 500;
void setup() {
Serial.begin(9600); // initialize serial port
pinMode(btnPin, INPUT_PULLUP);
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
}
void loop() {
buttonPressed();
setMode();
}
void buttonPressed() {
buttonState = pulseIn(btnPin, HIGH, 1000000);
if (buttonState > 50) {
funcState += 1;
Serial.print("Button state n: ");
Serial.println(funcState);
}
funcState = funcState % NBSTATE;
}
void setMode() {
// All Off
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
Serial.print("Function : ");
Serial.println(funcState);
switch (funcState) {
case OFF:
break;
case LED1:
digitalWrite(led1Pin, HIGH);
break;
case LED2:
digitalWrite(led2Pin, HIGH);
break;
case LED3:
digitalWrite(led3Pin, HIGH);
break;
case FADE1:
fade1();
break;
case ALL:
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
break;
case BLINK:
blinkLed1();
blinkLed2();
blinkLed3();
break;
}
}
void fade1() {
int brightness = 0;
int fadeAmount = 5;
for (brightness = 0; brightness <= 255; brightness += fadeAmount) {
analogWrite(led1Pin, brightness);
delay(30);
}
for (brightness = 255; brightness >= 0; brightness -= fadeAmount) {
analogWrite(led1Pin, brightness);
delay(30);
}
}
void blinkLed1() {
currentMillis1 = millis();
if (currentMillis1 - previousMillis1 >= interval1) {
// save the last time you blinked the LED
previousMillis1 = currentMillis1;
// if the LED is off turn it on and vice-versa:
if (ledState1 == LOW) {
ledState1 = HIGH;
} else {
ledState1 = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(led1Pin, ledState1);
}
}
void blinkLed2() {
currentMillis2 = millis();
if (currentMillis2 - previousMillis2 >= interval2) {
// save the last time you blinked the LED
previousMillis2 = currentMillis2;
// if the LED is off turn it on and vice-versa:
if (ledState2 == LOW) {
ledState2 = HIGH;
} else {
ledState2 = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(led2Pin, ledState2);
}
}
void blinkLed3() {
currentMillis3 = millis();
if (currentMillis3 - previousMillis3 >= interval3) {
// save the last time you blinked the LED
previousMillis3 = currentMillis3;
// if the LED is off turn it on and vice-versa:
if (ledState3 == LOW) {
ledState3 = HIGH;
} else {
ledState3 = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(led3Pin, ledState3);
}
}
Hasta ahora solo he conseguido que al pulsar el pulsador se encienda el led azul, luego lo vuelvo a pulsar y se enciende el led verde, pero se apaga el azul
Moderador:
Por favor edita el código de tu post#4.
Todo código debe postearse usando etiquetas, esta etiqueta </>
Ve al post, corta todo el codigo, click en </> pega el código y listo. Muy fácil!!
Solo cambia esto a ver si cumple con lo que buscas
case LED1: digitalWrite(ledAzul, HIGH);
break;
case LED2: digitalWrite(ledAzul, HIGH);
digitalWrite(ledVerde, HIGH);
break;
case LED3: digitalWrite(ledAzul, HIGH);
digitalWrite(ledVerde, HIGH);
digitalWrite(ledRojo, HIGH);
break;
Perdona, yo cambie los led
// C++ code
//
// Pin assignement
const int btnPin = 8;
const int ledAzul = 9;
const int ledVerde = 10;
const int ledRojo = 11;
enum fcnMode {
OFF,
LED1,
LED2,
LED3,
FADE1,
ALL,
BLINK,s
NBSTATE
}; // OFF = 0 and NBSTATE=7
int ledState1 = LOW,
ledState2 = LOW,
ledState3 = LOW; // ledState used to set the LED
unsigned long buttonState = 0;
int funcState = 0;
unsigned long currentMillis1, currentMillis2, currentMillis3; // will store current time
unsigned long previousMillis1, previousMillis2, previousMillis3; // will store last time LED was updated
const long interval1 = 100; // interval at which to blink (milliseconds)
const long interval2 = 300;
const long interval3 = 500;
void setup() {
Serial.begin(9600); // initialize serial port
pinMode(btnPin, INPUT_PULLUP);
pinMode(ledAzul, OUTPUT);
pinMode(ledVerde, OUTPUT);
pinMode(ledRojo, OUTPUT);
}
void loop() {
buttonPressed();
setMode();
}
void buttonPressed() {
buttonState = pulseIn(btnPin, HIGH, 1000000);
if (buttonState > 50) {
funcState += 1;
Serial.print("Button state n: ");
Serial.println(funcState);
}
funcState = funcState % NBSTATE;
}
void setMode() {
// All Off
digitalWrite(ledAzul, LOW);
digitalWrite(ledVerde, LOW);
digitalWrite(ledRojo, LOW);
Serial.print("Function : ");
Serial.println(funcState);
switch (funcState) {
case OFF: break;
case LED1: digitalWrite(ledAzul, HIGH);
break;
case LED2: digitalWrite(ledAzul, HIGH);
digitalWrite(ledVerde, HIGH);
break;
case LED3: digitalWrite(ledAzul, HIGH);
digitalWrite(ledVerde, HIGH);
digitalWrite(ledRojo, HIGH);
break;
case FADE1: fade1();
break;
case ALL: digitalWrite(ledAzul, HIGH);
digitalWrite(ledVerde, HIGH);
digitalWrite(ledRojo, HIGH);
break;
case BLINK:
blinkLed1();
blinkLed2();
blinkLed3();
break;
}
}
void fade1() {
int brightness = 0;
int fadeAmount = 5;
for (brightness = 0; brightness <= 255; brightness += fadeAmount) {
analogWrite(ledAzul, brightness);
delay(30);
}
for (brightness = 255; brightness >= 0; brightness -= fadeAmount) {
analogWrite(ledAzul, brightness);
delay(30);
}
}
void blinkLed1() {
currentMillis1 = millis();
if (currentMillis1 - previousMillis1 >= interval1) {
// save the last time you blinked the LED
previousMillis1 = currentMillis1;
// if the LED is off turn it on and vice-versa:
if (ledState1 == LOW) {
ledState1 = HIGH;
} else {
ledState1 = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledAzul, ledState1);
}
}
void blinkLed2() {
currentMillis2 = millis();
if (currentMillis2 - previousMillis2 >= interval2) {
// save the last time you blinked the LED
previousMillis2 = currentMillis2;
// if the LED is off turn it on and vice-versa:
if (ledState2 == LOW) {
ledState2 = HIGH;
} else {
ledState2 = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledVerde, ledState2);
}
}
void blinkLed3() {
currentMillis3 = millis();
if (currentMillis3 - previousMillis3 >= interval3) {
// save the last time you blinked the LED
previousMillis3 = currentMillis3;
// if the LED is off turn it on and vice-versa:
if (ledState3 == LOW) {
ledState3 = HIGH;
} else {
ledState3 = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledRojo, ledState3);
}
}
Cuando postees algo siempre recuerda darle formato al código de modo que quede con tabuladores o identación. De ese modo es facil ver como funcionan los ciclos.
Ya lo hice por ti.
Es casi lo que necesito, pero también deben de apagarse en orden y no todos a la vez.
Si, pero @Surbyte ya te dió un gran empujón al mostrarte como encenderlos en secuencia, no es tan complicado agregar las líneas necesarias para apagarlos.
Aquí siempre tenemos voluntad en ayudar pero no vamos a hacer todo por vos, haz tu parte.
Además en el post #5 nos cuentas:
Si conseguiste llegar hasta ahí tu solo, ya con la modificación de @Surbyte, es tarea de niños lo que te falta!
Saludos
Lo que pone en duda que lo haya hecho el, porque el nivel de lo programado es mas que interesante y alguien que usa enumeracion, millis(), modulo, y una máquina de estados, no hace estas preguntas.
Agrega mas estados y resuelves el problema y claro completa los estados con las funciones inversas a las que yo te agregué. Basicamente LED2 y LED1 son los dos estados que deben replicarse para completar lo que deseas.
