Created
March 16, 2014 14:05
-
-
Save edgarjcfn/9583750 to your computer and use it in GitHub Desktop.
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
// pin numbers: | |
const int button = 2; | |
const int green = 9; | |
const int yellow = 10; | |
const int red = 11; | |
// constants | |
#define SECS_PER_HOUR (3600UL) | |
#define SECS_PER_DAY (SECS_PER_HOUR * 24L) | |
#define MILLIS_PER_DAY (SECS_PER_DAY * 1000L); | |
//#define MILLIS_PER_DAY 1000L; //for debugging | |
// variables: | |
unsigned long lastClean = 0; | |
void setup() { | |
// initialize the LED pins as an output: | |
pinMode(green, OUTPUT); | |
pinMode(yellow, OUTPUT); | |
pinMode(red, OUTPUT); | |
// initialize the pushbutton pin as an input: | |
pinMode(button, INPUT); | |
Serial.begin(9600); | |
} | |
void loop(){ | |
unsigned long currentTime = millis(); | |
if (digitalRead(button)) | |
{ | |
lastClean = currentTime; | |
} | |
int daysSinceLastClean = elapsedDays(currentTime - lastClean); | |
if (daysSinceLastClean < 1) | |
{ | |
greenLed(); | |
} | |
else if (daysSinceLastClean < 2) | |
{ | |
yellowLed(); | |
} | |
else | |
{ | |
redLed(currentTime); | |
} | |
} | |
int elapsedDays(unsigned long time) | |
{ | |
return time / MILLIS_PER_DAY; | |
} | |
void greenLed() | |
{ | |
digitalWrite(green, HIGH); | |
digitalWrite(yellow, LOW); | |
digitalWrite(red, LOW); | |
} | |
void yellowLed() | |
{ | |
digitalWrite(green, LOW); | |
digitalWrite(yellow, HIGH); | |
digitalWrite(red, LOW); | |
} | |
// | |
// RED LED | |
// | |
const int STEP = 1; | |
const int blinkSpeed = 40; | |
int brightness = 0; | |
int brightnessStep = STEP; | |
void redLed(unsigned long time) | |
{ | |
digitalWrite(green, LOW); | |
digitalWrite(yellow, LOW); | |
if ((time % blinkSpeed) == 0) | |
{ | |
if (brightness >= 254) | |
{ | |
brightnessStep = -STEP; | |
} | |
else if (brightness <= STEP) | |
{ | |
brightnessStep = STEP; | |
} | |
brightness += brightnessStep; | |
} | |
analogWrite(red, brightness); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment