Last active
April 3, 2016 15:42
-
-
Save vincenthauser/0f5ab45b52f80bf7f1eaa6db2a2795bd 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
int const PIN_FRONT = 3; | |
int const PIN_BELLY = 5; | |
int const PIN_DEFAULT = 8; | |
int const INPUT_PIN = 2; | |
//input reading stuff | |
int inputReading = LOW; | |
int previousRead; | |
long time = 0; // the last time the output pin was toggled | |
long debounce = 200; // the debounce time, increase if the output flickers | |
//Scheme states | |
boolean isInterrupted = false; //set by attachInterrupt() call | |
int const SCHEME_OFF = -1; | |
int const SCHEME_ON = 0; | |
int const SCHEME_ONE = 1; | |
int const SCHEME_TWO = 2; | |
int const SCHEME_THREE = 3; | |
int const SCHEME_FOUR = 4; | |
int const SCHEME_FIVE = 5; | |
int const SCHEME_SIX = 6; | |
int const SCHEME_SEVEN = 7; | |
int currentScheme = SCHEME_OFF; | |
//blink scheme stuff | |
int isBlinkOn = 0; | |
long blinkTime = 0; | |
//Slow blink specific | |
long const ONDURATION = 200; | |
long const OFFDURATION = 180; | |
void setup() { | |
Serial.begin(9600, SERIAL_8N1); | |
pinMode(INPUT_PIN, INPUT); | |
pinMode(PIN_FRONT, OUTPUT); | |
pinMode(PIN_BELLY, OUTPUT); | |
pinMode(PIN_DEFAULT, OUTPUT); | |
resetStates(); | |
attachInterrupt(digitalPinToInterrupt(INPUT_PIN), setinterrupted, CHANGE); | |
} | |
void loop() { | |
readInput(); | |
} | |
void readInput() { | |
inputReading = digitalRead(INPUT_PIN); | |
long timeStamp = millis() - time; | |
if (inputReading == HIGH && previousRead == LOW && timeStamp > debounce) { | |
currentScheme++; | |
if (currentScheme > SCHEME_SEVEN) { | |
currentScheme = SCHEME_OFF; | |
} | |
resetStates(); | |
time = millis(); | |
} | |
previousRead = inputReading; | |
switchScheme(currentScheme); | |
} | |
void resetStates() { | |
off(); | |
time = millis(); | |
blinkTime = 0; | |
isBlinkOn = 0; | |
} | |
void setinterrupted() { | |
isInterrupted = true; | |
} | |
void switchScheme(int scheme) { | |
switch (scheme) { | |
case SCHEME_ON: | |
on(); | |
break; | |
case SCHEME_ONE: | |
slowBlink(); | |
break; | |
case SCHEME_TWO: | |
fastBlink(); | |
break; | |
case SCHEME_THREE: | |
pulse(); | |
break; | |
case SCHEME_FOUR: | |
alternate(); | |
break; | |
case SCHEME_FIVE: | |
currentScheme++; //skip this section | |
case SCHEME_SIX: | |
bellyBlink(); | |
break; | |
case SCHEME_SEVEN: | |
bellyOn(); | |
break; | |
default: | |
off(); | |
break; | |
} | |
} | |
void slowBlink() { | |
blinkLed(ONDURATION, OFFDURATION); | |
} | |
void fastBlink() { | |
blinkLed(ONDURATION / 2, OFFDURATION / 2); | |
} | |
void blinkLed(long onDuration, long offDuration) { | |
long timeStamp = millis() - blinkTime; | |
if (isBlinkOn == 1 && timeStamp > offDuration) { | |
blinkTime = millis(); | |
isBlinkOn = 0; | |
off(); | |
} | |
else if (isBlinkOn == 0 && timeStamp > onDuration) { | |
blinkTime = millis(); | |
isBlinkOn = 1; | |
on(); | |
} | |
} | |
void blinkThisLed(long onDuration, long offDuration, int led) { | |
long timeStamp = millis() - blinkTime; | |
if (isBlinkOn == 1 && timeStamp > offDuration) { | |
blinkTime = millis(); | |
isBlinkOn = 0; | |
digitalWrite(led, LOW); | |
} | |
else if (isBlinkOn == 0 && timeStamp > onDuration) { | |
blinkTime = millis(); | |
isBlinkOn = 1; | |
digitalWrite(led, HIGH); | |
} | |
} | |
void alternate() { | |
long timeStamp = millis() - blinkTime; | |
if (isBlinkOn == 1 && timeStamp > OFFDURATION) { | |
blinkTime = millis(); | |
isBlinkOn = 0; | |
digitalWrite(PIN_FRONT, LOW); | |
digitalWrite(PIN_BELLY, HIGH); | |
} | |
else if (isBlinkOn == 0 && timeStamp > ONDURATION) { | |
blinkTime = millis(); | |
isBlinkOn = 1; | |
digitalWrite(PIN_FRONT, HIGH); | |
digitalWrite(PIN_BELLY, LOW); | |
} | |
} | |
void pulse() { | |
//since attach interrupt gives the signal over a a couple hundred milliseconds, several times, | |
//isInterrupted will be true inside the loop a couple of times in the beginning | |
//But the main loop() will continue calling pulse() since we are not changing | |
//currentScheme in loop(); (So, everything is ok :) ) | |
float in, out; | |
isInterrupted = false; | |
for (in = 0; in < 6.283; in = in + 0.001) { | |
out = sin(in) * 127.5 + 127.5; | |
analogWrite(PIN_FRONT, out); | |
analogWrite(PIN_BELLY, out); | |
analogWrite(PIN_DEFAULT, out); | |
if (isInterrupted) { | |
break; | |
} | |
} | |
} | |
void bellyBlink() { | |
blinkThisLed(ONDURATION / 2, OFFDURATION / 2, PIN_BELLY); | |
} | |
void bellyOn() { | |
digitalWrite(PIN_FRONT, LOW); | |
digitalWrite(PIN_BELLY, HIGH); | |
digitalWrite(PIN_DEFAULT, LOW); | |
} | |
void on() { | |
digitalWrite(PIN_FRONT, HIGH); | |
digitalWrite(PIN_BELLY, HIGH); | |
digitalWrite(PIN_DEFAULT, HIGH); | |
} | |
void off() { | |
digitalWrite(PIN_FRONT, LOW); | |
digitalWrite(PIN_BELLY, LOW); | |
digitalWrite(PIN_DEFAULT, LOW); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment