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. 84
      pointlights.ino

84
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 {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,38 +75,34 @@ void compose() {
case OFF: case OFF:
plot("OFF", brightness); plot("OFF", brightness);
brightness = 0; brightness = 0;
doAfterMs(5000, goBackOn); if (currentMillis - startMillis >= 1000){
changeState(INCREASE);
}
break; break;
} }
} }
void goBackOn(){ void changeState(ledStates newState){
ledState=INCREASE; // call to change state, will keep track of time since last state
} startMillis = millis();
ledState = newState;
void wavyshine(){
plot("WAVE", brightness);
brightness = sinewave(1000,256,0); // you can tweak the parameters of the sinewave
analogWrite(ledPin, brightness);
} }
void plot(char *state, int brightness){ void plot(char *state, int brightness){
// use this function to plot a graph. // use this function to plot a graph.
// it will normalize the auto-scaling plotter // it will normalize the auto-scaling plotter
Serial.print(state); Serial.print(state);
Serial.print(", "); Serial.print(", ");
Serial.print(brightness); Serial.print(brightness);
Serial.println(", 0, 300"); Serial.println(", 0, 300");
} }
int increase_brightness (int brightness, float velocity){ int increase_brightness (int brightness, float velocity){
return brightness = brightness + 1 * velocity; return brightness = brightness + 1 * velocity;
} }
int decrease_brightness (int brightness, float velocity){ int decrease_brightness (int brightness, float velocity){
return brightness = brightness - 1 * velocity; return brightness = brightness - 1 * velocity;
} }
int sinewave(float duration, float amplitude, int offset){ int sinewave(float duration, float amplitude, int offset){
@ -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)();
}
}
}

Loading…
Cancel
Save