Go Down

Topic: Need to edit program (Lucid Dream Machine) (Read 1 time) previous topic - next topic

Alex Chambers

Nov 26, 2010, 07:52 am Last Edit: Nov 26, 2010, 07:54 am by ReadyBlack Reason: 1
Hello! Currently I am trying to get back into Arduino and programming. I am planning on making a Lucid Dream Machine. Simply put, I want the Arduino to have a button, two LED's and a timer integrated into it. The problem here is really the software (which is why I am writing here).

The Program:

When the program begins, it checks the state of the button. When a person is holding the button, then he/she is awake and I do not want the led sequence of lights to start. However, once a person begins to fall asleep, the button will be released and the countdown timer will begin. While one LED fades out, the other fades in. The sequence of LED fading lights will last for 15 minutes or so. And repeat 90 minutes later. And then another 90 minutes later.

The Problem:

1. I need to have a timer once the button is released.

and

2. I need to have the cycle repeated after the 15 minutes of fading.

If you can point me into the right direction or a direct solution that would be greatly appreciated!

Thank you to all those helping hands on Arduino Forums!  :)








Code:



/*
LucidDreamMachine

Inspired from the idea of Lucid Dreaming, this sketch is supposed to be
used while a person begins to fall asleep. As the individual begins to fall
asleep, he/she will let go of a button, which will then be read as LOW, and
begin a countdown. After "X" amount of time has elapsed, two LED lights will
to fade in and out, the left being LED 11 and the right being 12. After
twenty minutes has elapsed then there will be a 70 minute delay in which the
code will start over again.

*/

const int ledPinLeft = 10;  // the left LED will be output PIN 11
const int ledPinRight = 11; // the right LED will be outpit PIN 12
     int BUTTON = 3;       // the number of the pushbutton pin
     int brightnessA = 0;  // how bright setting A is
     int brightnessB = 0;  // how bright setting B is
     int fadeAmount = 5;   // how many points to fade the LED by
     int cycleCounter = 0; // measures the amount of cycles in the loop
     int buttonState = 0;  // variable fot reading the pushbutton status
     int REMtimer = 1000;  // the timer used to count after button is released
     int fadeSpeed = 30;   // a delay value of 30 milliseconds. its used in
                           // this function to make the delay visible

void setup()  {
 
 // declare pin 10 & 11 to be outputs:
 pinMode(10, OUTPUT);
 pinMode(11, OUTPUT);
 
 // the pushbutton is going to input value, so we initialize it as INPUT
 pinMode(BUTTON, INPUT);
}

void loop()  {
 // read the state of the pushbutton value:
 buttonState = digitalRead(BUTTON);
 
 // check if the pushbutton is pressed.
 // if the button is pressed, then do nothing
 if (buttonState == HIGH)
 {digitalWrite(ledPinLeft, LOW);
  digitalWrite(ledPinRight, LOW);}
 
 else
 {
 
 // set the brightness A value to pin 10:
 analogWrite(ledPinLeft, brightnessA);

 // set the brightness B value to pin 11
 analogWrite(ledPinRight, brightnessB);
 
 // change brightness A & B for next time through the loop:
 brightnessA = brightnessA + fadeAmount;
 brightnessB = brightnessB - fadeAmount;

 // reverse the direction of the fading at the ends of the fade:
 if (brightnessA == 0 || brightnessA == 255) {
   fadeAmount = -fadeAmount ;
 }

 // wait for 30 milliseconds to see the dimming effect    
 delay(fadeSpeed);
 }
}

Groove

#1
Nov 26, 2010, 11:28 am Last Edit: Nov 26, 2010, 11:33 am by GrooveFlotilla Reason: 1
Well, a very simple way would be to use a very bad word - 'delay' (that's almost as bad as 'goto').
The only thing to watch out for is to use 'long'-qualified constants.
I would put the fade code it its own function.
Per Arduino ad Astra

PaulS

You have described a state machine. One state is the user awake. Another is the user falling asleep. You have a transition between these states (button released) and an action to be performed when that transition occurs. That action is to note WHEN the transition occurred.

Once in the new state, you want to fade some LEDs. That's easy enough to do, without using delay. In fact. you have already described how you would do it, using nothing more than a watch.

Think about the differences between a timer and a watch, and look at the blink without delay example for how to do the fading without using a timer.

Groove

#3
Nov 26, 2010, 04:32 pm Last Edit: Nov 26, 2010, 04:45 pm by GrooveFlotilla Reason: 1
I can't think of a sensible way of doing it without using a timer.

Per Arduino ad Astra

Go Up