Buenas a todos, tengo realizado un sencillo código para un pequeño proyecto que estoy desarrollando en un local con unos sensores y una sirena en plan alarma, el problema viene que al usar delay hasta que no pasa el tiempo el arduino se queda parado en esa acción.
He mirado el ejemplo que hay de hacer el intermitente sin usar delay y he añadido otro led de manera que consigo hacer que cada led papadee con diferente frecuencia, pero claro a la hora de sustituir los delay por millis en el código del local no se muy bien como hacerlo para que actué según el estado de los sensores.
Aquí el código de los 2 leds con diferente tiempo de parpadeo
// constants won't change. Used here to set a pin number :
int ledPin = 13; // the number of the LED pin
int ledPin1 = 12;
// Variables will change :
int ledState = LOW; // ledState used to set the LED
int ledState1 = LOW; // ledState used to set the LED
// Generally, you shuould use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long previousMillis1 = 0; // will store last time LED was updated
// constants won't change :
const long interval = 300; // interval at which to blink (milliseconds)
const long interval1 = 30; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
}
void loop()
{
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
unsigned long currentMillis1 = millis();
if(currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
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(ledPin1, ledState1);
}
}
Aquí el código de la alarma intentando sustituir los delay por millis
int ledPin = 13; // LED conectado a pin digital 13
int ledPin1 = 12;
int analogPin1 = 1; // Entrada de señal sensor 2
int analogPin0 = 0; // potentiómetro conectado a pin analógico 3
int val = 0; // variable para almacenar el valor capturado
int threshold = 512; // valor de disparo o umbral (1024/2)
int ledState = LOW;
int ledState1 = LOW;
unsigned long previousMillis = 0;
unsigned long previousMillis1 = 0;
const long interval = 300; // interval at which to blink (milliseconds)
const long interval1 = 30; // interval at which to blink (milliseconds)
void setup() {
delay(30);
pinMode(ledPin, OUTPUT); // asigna modo salida el pin digital 13
}
void loop() {
unsigned long currentMillis = millis();
unsigned long currentMillis1 = millis();
val = analogRead(analogPin1); // captura el pin de entrada
if (val >= threshold) {unsigned long currentMillis = millis(); // Se toma el tiempo actual
// se comprueba si el tiempo actual menos el tiempo en que el LED cambió
// de estado por última vez es mayor que el intervalo.
if (currentMillis - previousMillis > interval){
// Si se cumple la condición se guarda el nuevo tiempo
// en el que el LED cambia de estado
previousMillis = currentMillis;
// Y ahora cambiamos de estado el LED, si está encendido a
// apagado o viceversa.
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
} else {
digitalWrite(ledPin, LOW); // apaga el LED
}
{
val = analogRead(analogPin0); // captura el pin de entrada
if (val >= threshold) { // 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(ledPin1, ledState1);
} else {
digitalWrite(ledPin1, LOW); // apaga el LED
}
}
}
}
Saludos y perdonar pero por mas que leo, sigo ejemplos y leo, no se muy bien como integrarlo.