Pages: [1]   Go Down
Author Topic: Add delay to "digitalWrite( ledPin,LOW);" via a pushbutton and resume code  (Read 1006 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello there, I'm a complete begginer in programming for the Arduino and need some help...
I'm building a lucid dreaming induction device, that while asleep and dreaming would deliver flashing lights to recognize the dream state.
The Arduino flashes my desktop lamp through a relay (works fine).
The code does the following:
Code:
relay off (40 min)

alarm (blinking)

relay off (10 min)

alarm (blinking)

relay off (10 min)

alarm (blinking)

relay off (10 min)

alarm (blinking)

The problem is:

that if the "alarm"  wakes me up I can't fall asleep again because the next alarm will go off in ten minutes.

-Possible solutions-

When woken up in the middle of the night, press a debounced pushbutton or switch to:

1. Somehow add a twenty minute delay (my sleep onset time) before resuming the alarm pattern. I've thought of using interrupts but as they cannot contain the delay function, I'm pretty lost...

or

2. When the switch is pressed, skip or silence the next two alarms.  Tried control statements but couldn't sort it out.

Hope I made myself clear... smiley-confuse
Thank you in advance

 
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8096
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps you could press the Reset button or briefly ground the Reset pin to re-start the sketch.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Hope I made myself clear...

Not really. Your code?

Read this before posting a programming question
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:

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.

Code:
/*
 -----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 smiley-wink
Logged

Maryland, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


I'm building a lucid dreaming induction device, that while asleep and dreaming would deliver flashing lights to recognize the dream state.
 

How exactly does the Arduino know you are in REM state?
Logged

There are three kind of people in the world: Those who can count, and those who can't

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

There are two ways (modes):

Mode 1 is very simplistic, it is a program that delivers flashes every 90 minutes (if you examine the code you can see it's a bit finetuned) hoping that it will coincide with your REM state. Unfortunately REM cycles differ from night to night making it very unreliable.

Mode 2 uses an amazing app for the iPhone called The Singularity Experience that integrates timing of your sleep cycles (similar to mode 1) and movement via the accelerometer of the device (in REM state you sleep still due to sleep paralysis ) providing a very good determination of the sleep state as experienced by the developer in comparison to a Zeo band. The app iluminates the iPhone screen when you are in REM and via a LDR the arduino gets the trigger signal to deliver the flashes through my bedside lamp.
Logged

Pages: [1]   Go Up
Jump to: