Last active
October 12, 2017 17:24
-
-
Save oveddan/22c7dc739ae6086c21a12e59bdb85129 to your computer and use it in GitHub Desktop.
Controls the flashing speed of an analog RGB led strip. The on duration is 10%. For use with a zoetrope
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
#define LOWER_READ 143 | |
#define UPPER_READ 940 | |
#define INTERVAL_PIN A0 | |
#define REDPIN 5 | |
#define GREENPIN 6 | |
#define BLUEPIN 9 | |
#define ON_PERCENTAGE .1 | |
long lastIntervalStart; | |
bool on; | |
void setup() { | |
Serial.begin(9600); | |
//Serial.println(frameDuration); | |
pinMode(INTERVAL_PIN, INPUT); | |
pinMode(REDPIN, OUTPUT); | |
pinMode(BLUEPIN, OUTPUT); | |
pinMode(GREENPIN, OUTPUT); | |
lastIntervalStart = millis(); | |
on = false; | |
setColor(255, 255, 255); | |
} | |
void setColor(int r, int g, int b) { | |
analogWrite(REDPIN, r); | |
analogWrite(BLUEPIN, b); | |
analogWrite(GREENPIN, g); | |
} | |
void loop() { | |
float intervalDuration = constrain(map(analogRead(INTERVAL_PIN), LOWER_READ, UPPER_READ, 0.0, 200.0), 0, 200.0); | |
float onDuration = intervalDuration * ON_PERCENTAGE; | |
float intervalElapsedTime = millis() - lastIntervalStart; | |
if (on && intervalElapsedTime > onDuration) { | |
on = false; | |
setColor(0, 0, 0); | |
} | |
if (intervalElapsedTime > intervalDuration) { | |
lastIntervalStart = millis(); | |
Serial.println(intervalDuration); | |
on = true; | |
setColor(255, 255, 255); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment