|
@ -1,5 +1,6 @@ |
|
|
enum ledStates {INCREASE, DECREASE, STAY, WAVE, OFF, ON}; // Here we make nicknames for the different states our program supports.
|
|
|
enum ledStates {INCREASE, DECREASE, STAY, WAVE, OFF, ON}; // Here we make nicknames for the different states our program supports.
|
|
|
enum ledStates ledState; // We define 'ledState' as type ledStates'
|
|
|
enum ledStates ledState; // We define 'ledState' as type ledStates'
|
|
|
|
|
|
enum ledStates previousLedState = ledState; |
|
|
|
|
|
|
|
|
unsigned long startMillis; //some global variables available anywhere in the program
|
|
|
unsigned long startMillis; //some global variables available anywhere in the program
|
|
|
unsigned long currentMillis; |
|
|
unsigned long currentMillis; |
|
@ -21,12 +22,13 @@ void loop() { |
|
|
compose(); |
|
|
compose(); |
|
|
delay(10); |
|
|
delay(10); |
|
|
analogWrite(ledPin, brightness); |
|
|
analogWrite(ledPin, brightness); |
|
|
|
|
|
currentMillis = millis(); //store the current time since the program started
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void compose() { |
|
|
void compose() { |
|
|
// this is a state machine which allows us to decouple the various operations from timed loops.
|
|
|
// this is a state machine which allows us to decouple the various operations from timed loops.
|
|
|
// instead we just switch from state to state when particular conditions are met.
|
|
|
// instead we just switch from state to state when particular conditions are met.
|
|
|
|
|
|
// we switch states by calling the changeState() function.
|
|
|
|
|
|
|
|
|
switch (ledState){ |
|
|
switch (ledState){ |
|
|
|
|
|
|
|
@ -36,7 +38,8 @@ void compose() { |
|
|
plot("INCREASING", brightness); |
|
|
plot("INCREASING", brightness); |
|
|
|
|
|
|
|
|
if (brightness > 250){ |
|
|
if (brightness > 250){ |
|
|
ledState = WAVE; |
|
|
//ledState = WAVE;
|
|
|
|
|
|
changeState(WAVE); |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
@ -44,14 +47,19 @@ void compose() { |
|
|
brightness = decrease_brightness(brightness, 0.5); |
|
|
brightness = decrease_brightness(brightness, 0.5); |
|
|
plot("DECREASING", brightness); |
|
|
plot("DECREASING", brightness); |
|
|
if (brightness == 0){ |
|
|
if (brightness == 0){ |
|
|
ledState = OFF; |
|
|
changeState(OFF); |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case WAVE: |
|
|
case WAVE: |
|
|
plot("WAVE", brightness); |
|
|
plot("WAVE", brightness); |
|
|
doForMs(5000, wavyshine); // this you might want to do for number of pulses, rather than for duration
|
|
|
|
|
|
ledState = DECREASE; |
|
|
brightness = sinewave(1000,256,0); // you can tweak the parameters of the sinewave
|
|
|
|
|
|
analogWrite(ledPin, brightness); |
|
|
|
|
|
|
|
|
|
|
|
if (currentMillis - startMillis >= 5000){ //change state after 5 secs by comparing the time elapsed since we last change state
|
|
|
|
|
|
changeState(DECREASE); |
|
|
|
|
|
} |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
case STAY: |
|
|
case STAY: |
|
@ -67,21 +75,17 @@ void compose() { |
|
|
case OFF: |
|
|
case OFF: |
|
|
plot("OFF", brightness); |
|
|
plot("OFF", brightness); |
|
|
brightness = 0; |
|
|
brightness = 0; |
|
|
doAfterMs(5000, goBackOn); |
|
|
if (currentMillis - startMillis >= 1000){ |
|
|
break; |
|
|
changeState(INCREASE); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void goBackOn(){ |
|
|
|
|
|
ledState=INCREASE; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void wavyshine(){ |
|
|
void changeState(ledStates newState){ |
|
|
plot("WAVE", brightness); |
|
|
// call to change state, will keep track of time since last state
|
|
|
brightness = sinewave(1000,256,0); // you can tweak the parameters of the sinewave
|
|
|
startMillis = millis(); |
|
|
analogWrite(ledPin, brightness); |
|
|
ledState = newState; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void plot(char *state, int brightness){ |
|
|
void plot(char *state, int brightness){ |
|
@ -110,31 +114,3 @@ int sinewave(float duration, float amplitude, int offset){ |
|
|
value = value + offset; //offset allows you to move the wave up and down on the Y-axis. Should not exceed the value of amplitude to prevent clipping.
|
|
|
value = value + offset; //offset allows you to move the wave up and down on the Y-axis. Should not exceed the value of amplitude to prevent clipping.
|
|
|
return value; |
|
|
return value; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void doForMs(int duration, void (*function)()){ |
|
|
|
|
|
// this helper function allows us to execute another function for 'duration' amount of millisecs
|
|
|
|
|
|
bool doing = true; |
|
|
|
|
|
startMillis = millis(); |
|
|
|
|
|
while(doing){ |
|
|
|
|
|
currentMillis = millis(); |
|
|
|
|
|
(*function)(); |
|
|
|
|
|
if (currentMillis - startMillis >= duration){ |
|
|
|
|
|
doing = false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void doAfterMs(int duration, void (*function)()){ |
|
|
|
|
|
// this helper function allows us to execute another function AFTER a 'duration' amount of millisecs
|
|
|
|
|
|
bool doing = true; |
|
|
|
|
|
startMillis = millis(); |
|
|
|
|
|
while(doing){ |
|
|
|
|
|
currentMillis = millis(); |
|
|
|
|
|
if (currentMillis - startMillis >= duration){ |
|
|
|
|
|
doing = false; |
|
|
|
|
|
(*function)(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|