Browse Source

add function to switch states, add example of switching after x amount of time, delete old locking timing functions

main
rra 4 years ago
parent
commit
606a50a662
  1. 64
      pointlights.ino

64
pointlights.ino

@ -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 ledState; // We define 'ledState' as type ledStates'
enum ledStates previousLedState = ledState;
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
@ -21,12 +22,13 @@ void loop() {
compose();
delay(10);
analogWrite(ledPin, brightness);
currentMillis = millis(); //store the current time since the program started
}
void compose() {
// 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.
// we switch states by calling the changeState() function.
switch (ledState){
@ -36,7 +38,8 @@ void compose() {
plot("INCREASING", brightness);
if (brightness > 250){
ledState = WAVE;
//ledState = WAVE;
changeState(WAVE);
}
break;
@ -44,14 +47,19 @@ void compose() {
brightness = decrease_brightness(brightness, 0.5);
plot("DECREASING", brightness);
if (brightness == 0){
ledState = OFF;
changeState(OFF);
}
break;
case WAVE:
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;
case STAY:
@ -67,21 +75,17 @@ void compose() {
case OFF:
plot("OFF", brightness);
brightness = 0;
doAfterMs(5000, goBackOn);
if (currentMillis - startMillis >= 1000){
changeState(INCREASE);
}
break;
}
}
void goBackOn(){
ledState=INCREASE;
}
void wavyshine(){
plot("WAVE", brightness);
brightness = sinewave(1000,256,0); // you can tweak the parameters of the sinewave
analogWrite(ledPin, brightness);
void changeState(ledStates newState){
// call to change state, will keep track of time since last state
startMillis = millis();
ledState = newState;
}
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.
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)();
}
}
}

Loading…
Cancel
Save