Help with improving code

So, basically, I am trying to replicate an experiment with Lucid Dreaming Goggles and I'm having some problems. Since this is the programming site, I wan't to know if this code is faulty, or if it can be improved. I warn you, I have NO prior knowledge in coding, but I do understand some basic functions in the code. Anyhow, here's the sample.

/*
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.
*/

void setup() {
// initialize the digital pin as an output.
// We are using Pin 2 on the Attiny85 microcontroller
pinMode(2, OUTPUT);

digitalWrite(2, HIGH);// blink LEDs once to signal power on
delay(2000);
digitalWrite(2, LOW);

delay(7200000); // Wait 2 hours for sleep to start
}

void loop() {
digitalWrite(2, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(2, LOW); // set the LED off
delay(500); // wait for a second
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(600000); //Wait 10 minutes

}
[/table]

I’m having some problems

…but you’re not going to let us in on the secret?

Please read the sticky topic at the top of this section.

That code does something. There are some potential problems, but, it does something. It is up to you to tell us what it does.

You want it to do something. What would that be?

We've hired a psychic. His start date is the 12th of never. If you need help before then, you've got to provide more info.

Too...many... delays... (passed out)

Your idea is simple, but your code setup is horrible. I know your new to coding, so look at the example blink without delay and also Real clock timers (RCT) don't use delays.

Real clock timers (RCT)

so. "RTC (real-time clock)"

Look at the Time and TimeAlarms examples in the library folder.
Try some of these examples before you dig further.

Well, it isn't my code. It's from Inception in Real Life: Make These Lucid Dreaming Glasses and Take Control of Your Dreams « Mad Science :: WonderHowTo

I mean, I am knowledgible in code enough to understand that the coding sucks. The function is as following: Blink ONCE when powered on, then BLINK with an interval after 2 hours, 10 minutes apart.

The function is as following: Blink ONCE when powered on, then BLINK with an interval after 2 hours, 10 minutes apart.

Is that what it is supposed to do, or what it actually does?

If that is what it is supposed to do, what does it actually do?

If that is what it actually does, what is it supposed to do?

PaulS:

The function is as following: Blink ONCE when powered on, then BLINK with an interval after 2 hours, 10 minutes apart.

Is that what it is supposed to do, or what it actually does?

If that is what it is supposed to do, what does it actually do?

If that is what it actually does, what is it supposed to do?

It's what it is supposed to do. In reality, if I programmed the chip correctly, it turns on for 8 seconds, and then it turns off, never to be seen again.

In reality, if I programmed the chip correctly, it turns on for 8 seconds, and then it turns off, never to be seen again.

No too surprising, really.

  delay(7200000); // Wait 2 hours for sleep to start

Literal constants, in the absence of directives to the contrary, are treated as ints. I'm pretty sure that 7200000 is larger than the maximum value (32767) that can fit in an int.

7200000UL, on the other hand, has "directives to the contrary", so is treated as an unsigned long, which is probably what you want.

PaulS:

In reality, if I programmed the chip correctly, it turns on for 8 seconds, and then it turns off, never to be seen again.

No too surprising, really.

  delay(7200000); // Wait 2 hours for sleep to start

Literal constants, in the absence of directives to the contrary, are treated as ints. I'm pretty sure that 7200000 is larger than the maximum value (32767) that can fit in an int.

7200000UL, on the other hand, has "directives to the contrary", so is treated as an unsigned long, which is probably what you want.

So, I insert UL after the 7200000? I really don't know. If you could rewrite the code, I would make sweet love with you!

So, I insert UL after the 7200000?

Yes.

If you could rewrite the code, I would make sweet love with you!

Ummm, I'll pass.

/*
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.
*/

void setup() {
// initialize the digital pin as an output.
// We are using Pin 2 on the Attiny85 microcontroller
pinMode(2, OUTPUT);

digitalWrite(2, HIGH);// blink LEDs once to signal power on
delay(2000);
digitalWrite(2, LOW);

delay(7200000UL); // Wait 2 hours for sleep to start
}

void loop() {
digitalWrite(2, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(2, LOW); // set the LED off
delay(500); // wait for a second
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**(600000UL**); //Wait 10 minutes

}
[/table]

Those in black?

Berkman:
Those in black?

Please read the sticky note at the top of the forum, and pay special attention to the part about posting code.

Other have replied but I'll make it just a bit easier ...

Your code cleaned up just a bit to be more readable.

You must have incredible patience ...

/*
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.
*/

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     HOUR            = 60 * MINUTE;

const unsigned long     TEN_MINUTES     = 10 * 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

void setup()
{
    pinMode(pinLED, OUTPUT); 
    
    // blink LEDs once to signal power on
    digitalWrite(pinLED, LED_ON);   delay(TWO_SECONDS);
    digitalWrite(pinLED, LED_OFF);  delay(TWO_HOURS);
}

void loop()
{
    digitalWrite(pinLED, LED_ON);   delay(SECOND);
    digitalWrite(pinLED, LED_OFF);  delay(HALF_SECOND);

    digitalWrite(pinLED, LED_ON);   delay(HALF_SECOND);              
    digitalWrite(pinLED, LED_OFF);  delay(HALF_SECOND);              

    digitalWrite(pinLED, LED_ON);   delay(QUARTER_SECOND);
    digitalWrite(pinLED, LED_OFF);  delay(QUARTER_SECOND);

    digitalWrite(pinLED, LED_ON);   delay(TENTH_SECOND);
    digitalWrite(pinLED, LED_OFF);  delay(TENTH_SECOND);

    digitalWrite(pinLED, LED_ON);   delay(TENTH_SECOND);
    digitalWrite(pinLED, LED_OFF);  delay(TENTH_SECOND);

    digitalWrite(pinLED, LED_ON);   delay(TENTH_SECOND);
    digitalWrite(pinLED, LED_OFF);  delay(TENTH_SECOND);

    digitalWrite(pinLED, LED_ON);   delay(TENTH_SECOND);
    digitalWrite(pinLED, LED_OFF);  delay(TENTH_SECOND);

    digitalWrite(pinLED, LED_ON);   delay(TENTH_SECOND);
    digitalWrite(pinLED, LED_OFF);  delay(TEN_MINUTES);
}

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.

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

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.

Yes, thanks for asking!

Now a simplified and smaller version.

/*
   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);
    }
}