unsigned long now;
unsigned int counter;
# define button 2
# define LED 3
# define PRESSED LOW
# define RATE 1333
// ----- symbolic indices -----
enum {
ACTION,
STOP,
START,
LED_COUNT
};
void setup() {
Serial.begin(115200);
Serial.println("hi Mom!");
pinMode(button, INPUT_PULLUP);
pinMode(LED, OUTPUT);
setupFlash();
}
void loop() {
static unsigned long lastTime;
now = millis();
// loop rate
serviceFlash();
static bool flag;
// this could be a debounced button and just do the thing asynchronously
if (digitalRead(button) == PRESSED) flag = true;
// once per
if (now - lastTime < RATE) return;
lastTime = now;
Serial.println(counter); counter++;
if (flag) {
flag = false;
digitalWrite(LED, digitalRead(LED) == LOW ? HIGH : LOW);
flash(ACTION);
if (digitalRead(LED) == HIGH) flash(STOP);
else flash(START);
}
}
/*
//
place these up top
enum {
ACTION,
&c.
}
and in setup
setupFlash();
then always
serviceFlash();
so you can just
flash(ACTION);
*/
// flash a stab at an index of stab lamps
// will also have on/off entries
# include <Adafruit_NeoPixel.h>
# define PIN 8 // the pin
# define NPIXELS 15 // number of LEDs on strip
Adafruit_NeoPixel stab(NPIXELS, PIN, NEO_GRB + NEO_KHZ800);
# define ledCount LED_COUNT // argh! matches number of symbols
# define STAB 2000 // nice stab of light
unsigned long flashStart[ledCount];
bool dirt;
unsigned char stabMap[ledCount] = {1, 7, 14};
void setupFlash(void)
{
stab.begin();
stab.setPixelColor(1, 0xff0000);
stab.setPixelColor(2, 0x00ff00);
stab.setPixelColor(3, 0x0000ff);
stab.setPixelColor(NPIXELS - 1, 0xffffff);
stab.show();
delay(777);
stab.clear();
stab.show();
}
void flash(int index)
{
if (index < 0 || index >= ledCount) return;
stab.setPixelColor(stabMap[index], 0xff0000);
dirt = true;
flashStart[index] = millis();
}
void serviceFlash(void)
{
// unsigned long now = millis(); needs global *now*
for (unsigned char ii = 0; ii < ledCount; ii++) {
if (flashStart[ii] != 0 && now - flashStart[ii] >= STAB) {
stab.setPixelColor(stabMap[ii], 0x200020);
flashStart[ii] = 0;
dirt = true;
}
}
if (dirt) stab.show();
}
/*
void loop0() {
static unsigned long lastTime;
now = millis();
// loop rate
static bool flag;
static bool did;
if (digitalRead(button) == PRESSED) flag = true;
// once per
if (now - lastTime < RATE) return;
lastTime = now;
Serial.println(counter); counter++;
if (!did) {
if (flag) {
flag = false;
digitalWrite(LED, digitalRead(LED) == LOW ? HIGH : LOW);
did = true;
}
}
else did = false;
}
*/ON/OFF
ON
OFF