Thank you for your quick replies.
Pressing reset didn't solve it cause the delays between alarms change throught the night so the whole loop is 8 hours long.
I've finally come across a solution utilising a switch, and applying the following function (onset) before the alarm function:
void onset () {
reading = digitalRead(inPin);
if (reading == HIGH && previous == LOW && millis() - time > debounce && count == false) {
digitalWrite(pinLed, LOW);
delay(Onset);
time = millis();
count = true;
previous = reading;
}
if (reading == LOW && count == true && millis() - time > debounce) {
{ digitalWrite(pinLed, LOW);
delay(Onset);
previous = reading;
time = millis();
count = false;
}
}
}
Here's the whole code, please excuse the poor code readability, and spanish comments.
/*
-----DreamLux-----
Utilise any bedside lamp to trigger lucid dreams via flashing light from the lamp.
Activated in REM stage via The Singularuty App (iOS) or built-in REM timer.
*/
int inPin =7;
int Onset = 600000;
int state = HIGH;
int reading;
int previous = LOW;
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
boolean count = false;
//Aquí almacenamos los datos recogidos del LDR:
int valorLDR = 0;
//Decimos que pines vamos a utilizar para LED o Rele (pin 2)
int pinLed = 2;
//Y que pin para la LDR
int pinLDR = 0;
void setup()
{
//Establecemos como salida los pines para LED
pinMode(pinLed, OUTPUT);
pinMode(inPin, INPUT);
digitalWrite(pinLed, LOW);
delay(5000);
alarma (); //Alarma de TEST
digitalWrite(pinLed, LOW);
delay(60000); //Previene que se active el programa de forma accidental
}
void loop()
{
//Guardamos el valor leido en una variable
valorLDR = analogRead(pinLDR);
//Y comenzamos las comparaciones:
/*if(valorLDR >= 800)
{ alarma (); // ALARMA
digitalWrite(pinLed, LOW);
delay(20000); //Permite que aunque salga luz de la pantalla solo se den los flashes indicados (20s)
}
else
{
}
*/
while(valorLDR >=800)
{ alarma (); // ALARMA
digitalWrite(pinLed, LOW);
delay(40000); //Permite que aunque salga luz de la pantalla solo se den los flashes indicados (40s)
}
if(valorLDR > 1)
{
digitalWrite(pinLed, LOW); // Mantiene apagada la luz siempre que la pantalla este negra
}
else // Activa el programa 2 si no hay LDR conectada- REM TIMERS
{
for (int x=0;x<2;x++) // Señal de que DreamLux opera en modo REM TIMER ALARM
{
digitalWrite(pinLed, HIGH);
delay(500);
digitalWrite(pinLed, LOW);
delay(500);
}
digitalWrite(pinLed, HIGH);
delay(2000);
digitalWrite(13, LOW);
delay(1200000); // Sleep onset 20 min
digitalWrite(pinLed, LOW);
delay(5400000); //1 Cycle
digitalWrite(pinLed, LOW);
delay(5400000); //2 Cycle
digitalWrite(pinLed, LOW);
delay(4500000); //3 Cycle
onset ();
alarma ();
digitalWrite(pinLed, LOW);
delay(900000); // 15 min delay till next alarm
onset ();
alarma ();
digitalWrite(pinLed, LOW);
delay(4200000); // 4 Cycle
onset ();
alarma ();
digitalWrite(pinLed, LOW);
delay(600000); // 10 min delay till next alarm
onset ();
alarma ();
digitalWrite(pinLed, LOW);
delay(600000); // 10 min delay till next alarm
onset ();
alarma ();
digitalWrite(pinLed, LOW);
delay(3600000); // 5 Cycle
onset ();
alarma ();
digitalWrite(pinLed, LOW);
delay(600000); // 10 min delay till next alarm
alarma ();
digitalWrite(pinLed, LOW);
delay(600000); // 10 min delay till next alarm
onset ();
alarma_5 ();
digitalWrite(pinLed, LOW);
delay(600000); // 10 min delay till next alarm
// alarma desactivada poca eficacia final de REM [alarma ();]
digitalWrite(pinLed, LOW);
delay(2700000); // 6 Cycle
for(int i=0;i<10;i++)
{ alarma_5 ();
digitalWrite(pinLed, LOW);
delay(900000); // 15 min delay till next alarm
}
}
}
//Distintos tipos de alarma
void alarma_singular ()
{digitalWrite(pinLed, HIGH);
delay(500);
digitalWrite(pinLed, LOW);
delay(500);
}
void alarma () { for (int x=0;x<10;x++) // Alarma de 10 flashes a 1HZ
{ digitalWrite(pinLed, HIGH);
delay(500);
digitalWrite(pinLed, LOW);
delay(500);
}
}
void alarma_5 () { for (int x=0;x<5;x++) // Alarma de 5 flashes a 1HZ
{ digitalWrite(pinLed, HIGH);
delay(500);
digitalWrite(pinLed, LOW);
delay(500);
}
}
void alarma_15 () { for (int x=0;x<15;x++) // Alarma de 15 flashes a 1HZ
{ digitalWrite(pinLed, HIGH);
delay(500);
digitalWrite(pinLed, LOW);
delay(500);
}
}
void alarma_inception () { for (int x=0;x<4;x++) //Alarma ritmica repetida 4 veces
{
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(500);
digitalWrite(2, HIGH);
delay(500);
digitalWrite(2, LOW);
delay(500);
digitalWrite(2, HIGH);
delay(250);
digitalWrite(2, LOW);
delay(250);
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);
digitalWrite(2, HIGH);
delay(100);
digitalWrite(2, LOW);
delay(100);
}
}
void a_custom(int howManyTimes, int Freq) {
int i;
for (i=0; i< howManyTimes; i++) {
digitalWrite(pinLed, HIGH);
delay(((1/Freq)/2)*1000);
digitalWrite(pinLed, LOW);
delay(((1/Freq)/2)*1000);
}
}
//Funcion de retraso conectando interruptor
void onset () {
reading = digitalRead(inPin);
if (reading == HIGH && previous == LOW && millis() - time > debounce && count == false) {
digitalWrite(pinLed, LOW);
delay(Onset);
time = millis();
count = true;
previous = reading;
}
if (reading == LOW && count == true && millis() - time > debounce) {
{ digitalWrite(pinLed, LOW);
delay(Onset);
previous = reading;
time = millis();
count = false;
}
}
}
Thank you once again