Go Down

Topic: Help with improving code (Read 743 times) previous topic - next topic

Berkman

The code that you posted, is that still functional without the UL-part, Paul talked about?

Two hours after you go to sleep, you first start going into REM-sleep. We don't want to interfere before going to sleep. Hence the 2 hour waiting period.

Berkman



GOD BLESS ARDUINO AND THIS GUY WHO I CANT BOTHER TO CHECK HIS NAME. IT WORKS. OIL!

lloyddean

#17
Jan 14, 2013, 03:35 am Last Edit: Jan 14, 2013, 03:47 am by lloyddean Reason: 1

The code that you posted, is that still functional without the UL-part, Paul talked about?

Two hours after you go to sleep, you first start going into REM-sleep. We don't want to interfere before going to sleep. Hence the 2 hour waiting period.


Yes, thanks for asking!

Now a simplified and smaller version.

Code: [Select]

/*
   Inception Sleep Goggles

   LEDs in the goggles blink an a timed pattern to alert you when you are
   dreaming. The program waits 2 hours for you to fall asleep before starting
   the blink sequence every 10 minutes.
   
   This example code is in the public domain.
*/

#define ENTRIES(ARRAY)  (sizeof(ARRAY) / sizeof(ARRAY[0])

const unsigned long     TENTH_SECOND    =  100UL;
const unsigned long     QUARTER_SECOND  =  250UL;
const unsigned long     HALF_SECOND     =  500UL;
const unsigned long     SECOND          = 1000UL;
const unsigned long     TWO_SECONDS     =  2 * SECOND;
const unsigned long     MINUTE          = 60 * SECOND;
const unsigned long     TEN_MINUTES     = 10 * MINUTE;
const unsigned long     HOUR            = 60 * MINUTE;
const unsigned long     TWO_HOURS       =  2 * HOUR;

const uint8_t           LED_OFF         = LOW;
const uint8_t           LED_ON          = HIGH;

const uint8_t           pinLED          = 2;    // Attiny85

struct timing_pair_t
{
    const unsigned long _msON;
    const unsigned long _msOFF;
};

timing_pair_t           sequence[] =
{
    // SEQUENCE TABLE IS REVERSED

      {   TENTH_SECOND,    TEN_MINUTES }        // Sequence End
    , {   TENTH_SECOND,   TENTH_SECOND }
    , {   TENTH_SECOND,   TENTH_SECOND }
    , {   TENTH_SECOND,   TENTH_SECOND }
    , {   TENTH_SECOND,   TENTH_SECOND }
    , { QUARTER_SECOND, QUARTER_SECOND }
    , {    HALF_SECOND,    HALF_SECOND }           
    , {         SECOND,    HALF_SECOND }
    , {    TWO_SECONDS,      TWO_HOURS }        // Sequence Begin
};

void blink(unsigned long msOn, unsigned long msOff)
{
    digitalWrite(pinLED, LED_ON);   delay(msOn);
    digitalWrite(pinLED, LED_OFF);  delay(msOff);
}

void setup()
{
    pinMode(pinLED, OUTPUT);
}

void loop()
{
    // start at the end of 'sequence[]'

    for ( int i = ENTRIES(sequence); i--; )
    {
        blink(sequence[i]._msON, sequence[i]._msOFF);
    }
}


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy