Hey all. I'm dead rotten at Algebra. Dyxlexia: It's wilding like spreadfire.
My 8 year old son is an insomniac. Hours of playing, reading, games etc will just NOT help him sleep. I've tried just about everything I can to exhaust him before bed, but to no avail. On that note - while advice on how to get the little one to sleep is more than welcome, if possible, an answer to my math related question would still be really great because he knows about this project and wants to try it!
I've created a lamp for my son's nightstand that is a 1w blue HB led driven with a lovely constant current driver / PWM that I had laying about. I'm using analogWrite() to the driver's PWM pin to dim and illuminate the LED. When the LED is getting brighter, he's to breathe in. When it's getting dimmer, he's to exhale. At the valley between breaths, he's to not breathe (you know... like we all do all the time including you as you're reading this now. yes, you.)
The issue I have is that I've decided to go too big for my skills on it. I can make it work if I just control the brightness, but leave the timing of the breathing exactly the same because I know all the variables. I can make it work if I change the timing of the breath but the brightness is set.
The big issue is the increment in my analogWrite loop for inhaling and exhaling.
I need to get from 5 (my brightness floor) to X (my max brightness) in Y (in breath) milliseconds. I can simply change the delay to do this, but with SHORTER breaths, I might not get all the way to Y before running out of time if the increment is too small on my fade up loop. Alternately, if I increase the increment, but then increase the in-breath length, the steps might be HUGE and really noticeable on a long in-breath. We want the increments as small as possible to make it possible for 5 to get to X in Y milliseconds with a delay long enough for the PWM to register but not so long that it looks choppy. I'm going to throw up. I'll restate:
I need to get a formula that will give me the smallest increment and lowest delay necessary to get from 5 to X in as close to exactly Y ms as possible.
I'll use the same formula to calculate the outbreath. The pauses are not an issue.
I have a pot for brightness that is mapped to return 5-200.
I have a pot for breath length that is mapped to return 2000-9000 (as in the seconds necessary to complete one breath cycle)
In the end, this would run as a macro of sorts that will start him at a nice 5000ish range, then slowly slow down and help him get to sleep. This technique has been used in cases like his as an alternative to drugs (EEEK!).
I would appreciate any assistance you might be able to give on this problem. I know this is long and I'm almost certainly missing an important piece of information that will frustrate you folks. I'm just brain dead on this and I missed the cutoff to give it to him tonight, but would love it if he had it for tomorrow.
I've attached a photo - it looks much better in person. No you may not have my box of M/M jumpers.
Assume:
- The human breath from start of in-breath 1 to the start of in-breath 2 can take between 2000 milliseconds and 9000 milliseconds.
- The breath is made up of:
- in-breath
- pause and hold breath
- out-breath
- pause with no breathing
- The following values are assumed for the ideal breath:
- in-breath = total breath * .50;
- pause and hold breath = totalbreath *.05
- out-breath = totalbreath * .35
- pause between breaths = totalbreath * .10
Code blocks with missing chunks:
int peak = 80; // max brightness
int start = 5; // min brightness (it looks creepy when it goes all the way off)
int totalBreath = 7500; // from start of in-breath to start of in-breath
int inbreathIncrement = 2; // steps to increase brightness on in-breath with analogWrite()
int outbreathIncrement = 2;// steps to decrease brightness on in-breath with analogWrite()
int inbreathDelay = 20; // delay in ms between increases in brightness
int outbreathDelay = 30;// delay in ms bet... The comments are too verbose, aren't they?
int pause = 1000; // the time to wait at the bottom of a breath before starting a new one.
}
void loop(){
setBrightness(); //see below
setSpeed(); //ditto
// at this point, it's assumed we know the speed of the breath and the brightness.
// that should give us inbreathIncrement and inbreathDelay
for(int i=start; i<=peak;i=i + inbreathIncrement){
analogWrite(3,i);
delay(inbreathDelay);
}
for(int i=peak; i>=start;i=i- outbreathIncrement){
analogWrite(3,i);
delay(outbreathDelay);
}
delay(pause);
}
void setBrightness(){
peak = map(analogRead(A1),0,1024,255,start);
}
void setSpeed(){
totalBreath = map(analogRead(A0),0,1024,9000,2000);
int inbreathtime = totalBreath *.55;
int outbreathtime = totalBreath * .35;
pause = totalBreath * .1;
inbreathDelay = ??????
outbreathDelay = ??????
}
Honestly, thanks everyone who made it this far. It's a thin line between not giving enough information for you to help and molesting you with so much mind-barf that you don't want to read this far. Half of you probably only read this far assuming that by looking this deep you'd find Carmen Sandiego. She's not here, but I'll tell you where I last saw her if you help me with my math issues!