aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClemens Fries <git-arduino@xenoworld.de>2017-02-23 22:47:49 +0100
committerClemens Fries <git-arduino@xenoworld.de>2017-02-23 22:47:49 +0100
commitce7de31f275afab6bd28dda4dc1821666866fa61 (patch)
treea0733a822374033b9192c67708524d3be9c0183e
parent458ce0a842c0ae6083822c9e76e09427b42fdbd7 (diff)
added code for fluorescent light demo
-rw-r--r--README.adoc5
-rw-r--r--lighting/strobe.ino130
2 files changed, 134 insertions, 1 deletions
diff --git a/README.adoc b/README.adoc
index a1195ec..15cfd55 100644
--- a/README.adoc
+++ b/README.adoc
@@ -1,7 +1,10 @@
== Various Arduino Snippets
Every now and then I create a small Arduino (test) project. I think I should
-publish the stuff here.
+publish the stuff here — this is mostly cargo cult code that has been hammered
+into some semi-working form.
Currently in this repository:
+lighting/strobe.ino::
+ The code behind my https://xenoworld.de/2017/01/18/blink.html[fluorescent light demo].
diff --git a/lighting/strobe.ino b/lighting/strobe.ino
new file mode 100644
index 0000000..eccc1c4
--- /dev/null
+++ b/lighting/strobe.ino
@@ -0,0 +1,130 @@
+// PWM: 3, 5, 6, 9, 10, and 11. Provide 8-bit PWM output with the analogWrite() function.
+byte LED_A = 3;
+byte LED_B = 5;
+
+#define STROBE_STATE_SLOW 1
+#define STROBE_STATE_FAST 2
+#define STROBE_STATE_NONE 3
+
+class Strobe {
+ byte slow, fast;
+ byte state;
+ byte led;
+ boolean on;
+ unsigned long next = 0;
+
+public:
+ boolean stopped = false;
+
+ Strobe(byte l, byte s, byte f) {
+ state = STROBE_STATE_SLOW;
+ led = l;
+ slow = s;
+ fast = f;
+ on = false;
+ }
+
+ boolean run() {
+ if (next > 0 && (millis() < next)) {
+ return false;
+ }
+
+ switch(state) {
+ case STROBE_STATE_SLOW:
+ on = !on;
+ analogWrite(led, on ? 255 : 0);
+ if(on) {
+ next = millis() + 10;
+ return false;
+ } else {
+ next = millis() + 300 + random(0, 300);
+ }
+ if(--slow <= 0) {
+ state = STROBE_STATE_FAST;
+ }
+ return false;
+ case STROBE_STATE_FAST:
+ on = !on;
+ analogWrite(led, on ? 255 : 0);
+ if(on) {
+ next = millis() + 10;
+ return false;
+ } else {
+ next = millis() + 75 + random(0, 75);
+ }
+ if(--fast <= 0) {
+ state = STROBE_STATE_NONE;
+ on = false;
+ }
+ return false;
+ default:
+ if(!on) {
+ analogWrite(led, 255);
+ on = true;
+ stopped = true;
+ return true;
+ }
+ }
+ }
+};
+
+void setup() {
+ pinMode(2, INPUT_PULLUP);
+ Serial.begin(9600);
+}
+
+void flash(byte led, byte duration)
+{
+ analogWrite(led, 255);
+ delay(duration);
+ analogWrite(led, 0);
+}
+
+void fade(byte led, byte stepDuration, byte stepSize, boolean in) {
+ if(in) {
+ for (int fadeValue = 0 ; fadeValue <= 255; fadeValue+=stepSize) {
+ analogWrite(led, fadeValue);
+ delay(stepDuration);
+ }
+ } else {
+ for (int fadeValue = 255 ; fadeValue >=0; fadeValue-=stepSize) {
+ analogWrite(led, fadeValue);
+ delay(stepDuration);
+ }
+ }
+}
+
+void fade(byte led, byte stepDuration, boolean in)
+{
+ fade(led, stepDuration, 1, in);
+}
+
+unsigned long lastMillis;
+
+void loop() {
+ delay(1000);
+
+ Strobe s1 = Strobe(3, random(1,4), random(2, 6));
+ Strobe s2 = Strobe(5, random(1,4), random(2, 6));
+
+ while(!(s1.stopped && s2.stopped)) {
+ if(lastMillis > millis()) {
+ lastMillis = millis();
+ return;
+ }
+ s1.stopped ? : s1.run();
+ s2.stopped ? : s2.run();
+ lastMillis = millis();
+ }
+
+
+ delay(2500);
+
+ analogWrite(LED_A, 0);
+ analogWrite(LED_B, 0);
+
+ Serial.println(lastMillis);
+ Serial.println(millis());
+}
+
+