Last active
January 21, 2018 22:39
-
-
Save Youz13/9dda66d79f706f78adbd2451119e4583 to your computer and use it in GitHub Desktop.
FADing (Fall Asleep Device)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
FADing : version 1.0 | |
Made by : Youz | |
1- Push ON button to power up until you see 1 flash. | |
2- To add more minutes to the process, you have 5 seconds to add 2 more minutes for each push on the "mode" button. | |
3- To switch betwin top light or inside light, quick push the "mode" button. | |
4- To shutdown, long push the "mode" button. | |
*/ | |
// for change time of default process | |
int initTime = 8; // initial time of process in minutes. YOU CAN MODIFY IT | |
// Pin | |
int led = 9; // the PWM pin the LED is attached to (light for celing) | |
int mode = A5; // the input pin for the mode button | |
int pwr = 4; // the power pin to set relay ON through the MOSFET | |
// Var | |
int button_delay = 0; // used to check long or short push on mode button | |
int brightness = 10; // how initial bright the LED is | |
int fadeAmount = 1; // how many points to fade the LED by | |
int start = 1; // used for startup | |
float delayM = 20; // delay between brigtness change for the first 10 seconds | |
unsigned long temps; // for compare with millis() | |
int cyclecompt = 1; // for both 'while' | |
int buttonCounter = 0; // used for the number of push of the mode button | |
int lastButtonState = 0; // the last state of mode button | |
int buttonState = 0; // the current state of mode button | |
float intervaL = 0; // the interval for slow down fade in/fade out | |
int md2 = 0; // use for minutes added by push mode button and for fulltime run of the EasySleep | |
void setup() { | |
temps = millis(); | |
pinMode(led, OUTPUT); pinMode(pwr, OUTPUT); pinMode(mode, INPUT); pinMode(10, OUTPUT); | |
digitalWrite(pwr, HIGH); | |
} | |
void loop() { | |
if (start == 1) { // Run startup loop | |
StarT(); | |
} | |
if ( millis() >= temps + 10000 ) { // every 10 seconds, fade in/fade out slow down | |
delayM = delayM + intervaL; | |
temps = temps + 10000; | |
} | |
if ( temps > md2 * 60000 ) { // check if the selected number of minutes is reach | |
EnD(); | |
} | |
cycle(); // run 1 fade in and 1 fade out step | |
} | |
void StarT() { | |
analogWrite(10, 30); delay(200); analogWrite(10, 0); delay(300); analogWrite(10, 30); delay(200); analogWrite(10, 0); delay(1000); // blink 2 times when arduino boot finish | |
while (cyclecompt < 500) { // check for 5 seconds the number of time mode button is pushed | |
buttonState = digitalRead(mode); | |
if (buttonState != lastButtonState) { | |
if (buttonState == HIGH) { | |
buttonCounter++; | |
delay(200); | |
} | |
} | |
lastButtonState = buttonState; | |
delay(10); | |
cyclecompt++; | |
} | |
if (buttonCounter == 0) { | |
md2 = initTime; | |
} | |
else { | |
md2 = initTime + ( buttonCounter * 2 ); // Add 2 minutes per push of the mode button | |
} | |
intervaL = 21.0 / ( (float)md2 * 6.0 ); //21 is the interval between brightness change from starting 11 breath per minute (20 ms) to ending 6 breath per minute (41 ms) | |
start = 0; | |
if ( md2 != initTime ) { // blink the number of time mode button is pushed | |
cyclecompt = 1; | |
while (cyclecompt < buttonCounter + 1) { | |
analogWrite(10, 50); delay(200); analogWrite(10, 0); delay(300); | |
cyclecompt++; | |
} | |
delay(800); | |
} | |
} | |
void cycle() { // 1 cycle of fade in/fade out | |
cyclecompt = 0; | |
while (cyclecompt < 220) { | |
analogWrite(led, brightness); | |
brightness = brightness + fadeAmount; | |
if ( brightness <= 10 ) { // On ending invert fadeAmount for next fade in | |
fadeAmount = -fadeAmount; | |
delay(1000); | |
} | |
if ( brightness >= 115 ) { // If led is full bright, invert fadeAmount to fade out | |
fadeAmount = -fadeAmount; | |
} | |
delay(delayM); | |
cyclecompt++; | |
if (digitalRead(mode) == HIGH) { | |
button_delay = 0; | |
while (digitalRead(mode) == HIGH) { | |
button_delay++; | |
delay(100); | |
if (button_delay > 10) { //no need to wait for user to release | |
digitalWrite(9, LOW); | |
digitalWrite(10, LOW); | |
EnD(); | |
break; | |
} | |
} | |
if (button_delay < 10) { //short press | |
if (led == 9) { | |
for (int fadeValue = brightness ; fadeValue >= 0; fadeValue -= 5) { | |
analogWrite(9, fadeValue); | |
delay(30); | |
} | |
digitalWrite(9, LOW); | |
led = 10; button_delay = 0; | |
} | |
else { | |
for (int fadeValue = brightness ; fadeValue >= 0; fadeValue -= 5) { | |
analogWrite(10, fadeValue); | |
delay(30); | |
} | |
digitalWrite(10, LOW); | |
led = 9; button_delay = 0; | |
} | |
} | |
} | |
} | |
} | |
void EnD() { | |
digitalWrite(pwr, LOW); delay(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment