// hacking at
// https://forum.arduino.cc/t/using-an-interrupt-in-code/972332/7
// which was modified from https://wokwi.com/projects/325673372202566226
// modified from https://wokwi.com/projects/326961789731340883
// modified to print mode, and change some processing
#include <Adafruit_NeoPixel.h>
#define STRIP1_COUNT 60
#define STRIP1_PIN 6
#define STRIP2_COUNT 60
#define STRIP2_PIN 7
#define RING_COUNT 16
#define RING_PIN 5
#define PIR1_PIN A0
#define PIR2_PIN 2
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel(STRIP1_COUNT, STRIP1_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel(STRIP2_COUNT, STRIP2_PIN, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel ring = Adafruit_NeoPixel(RING_COUNT, RING_PIN, NEO_GRB + NEO_KHZ800);
int pir1Pin = A0;
int pir2Pin = 2;
int strip1Pin = 7;
int strip2Pin = 6;
int ringPin = 5;
boolean oldState = LOW;
int mode = 0; // Currently-active animatin mode, 0-9
int mode_ii = 0;
int j = -1; // extra state variable for some modes
void setup() {
Serial.begin(115200);
pinMode(pir1Pin, INPUT);
pinMode(pir2Pin, INPUT);
pinMode(strip1Pin, OUTPUT);
pinMode(strip2Pin, OUTPUT);
pinMode(ringPin, OUTPUT);
strip1.begin();
strip1.show(); // Initialize all pixels to 'off'
strip2.begin();
strip2.show(); // Initialize all pixels to 'off'
ring.begin();
ring.show(); // Initialize all pixels to 'off'
}
void loop() {
// Get Current sensor state.
boolean newState = digitalRead(PIR1_PIN);
unsigned long now = millis();
static unsigned long lastStateMs = 0;
bool stateRose = false;
bool stateFell = false;
/*
// Check if state changed from low to high (sensor trigger)
if ((newState == HIGH) && (oldState == LOW)) {
// Short Delay to debounce sensor.
delay(20);
// Check if sensor is still high after debounce.
newState = digitalRead(PIR1_PIN);
if (newState == HIGH) {
if (++mode > 6) mode = 0;
}
}
// set the last-read sensor state to the old state.
oldState = newState;
*/
// deadtime latch debouncing and edge detection
if (newState != oldState && now - lastStateMs > 10) { // debounced
lastStateMs = now;
oldState = newState;
stateRose = newState;
stateFell = !newState;
}
if (stateRose) {
Serial.print("R");
//if ( ++mode > 6 ) mode = 0;
}
if (stateFell) {
Serial.print('F');
}
//Serial.print(newState);
// Manage the LED state bassed on mode
switch (mode) {
case 0: // blackout
strip1.clear();
strip2.clear();
strip1.show();
strip2.show();
ring.clear();
ring.show();
mode = 1;
break;
case 1: // idle in off
if (stateRose) {
mode = 2;
}
break;
case 2: // clear strips & transition to wiping
mode_ii = 0;
strip1.clear();
strip2.clear();
mode = 3;
break;
case 3: // wipe strips
//colorWipe_drf(strip1.Color( 0, 0, 255), 10); //Blue
colorWipe_drf(strip2.Color( 0, 0, 255), 100); //Blue
if (mode_ii > 255) {
Serial.print("!");
mode = 2;
}
if (digitalRead(PIR2_PIN) == HIGH) { // change to rainbow mode
// clean up strips
//colorFill(strip1.Color( 0, 0, 0), 0);
//colorFill(strip2.Color( 0, 0, 0), 0);
mode = 4;
}
break;
case 4: // clear strips & transition to rainbow
mode_ii = 0;
strip1.clear();
strip2.clear();
strip1.show();
strip2.show();
mode = 5;
break;
case 5: // run the ring
rainbow_drf(10);
///... how to get out of rainbow mode...
if (digitalRead(PIR1_PIN) == HIGH) {
mode = 2;
}
break;
case 6: // testing
colorWipe_drf(strip1.Color( 255, 0, 255), 10);
if (mode_ii >= 255) {
mode = 2;
mode_ii = -1;
}
break;
case 7:
Serial.print(7);
break;
default:
mode = 0;
Serial.print("D");
break;
}
//Serial.print(mode);
report();
}
void report(void) {
unsigned long interval = 500;
static unsigned long last = -interval;
if (millis() - last >= interval) {
last += interval;
Serial.print(mode);
Serial.print(',');
Serial.print(mode_ii);
Serial.print("; ");
}
}
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow(int wait) {
// Hue of first pixel runs 5 complete loops through the color wheel.
// Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
// means we'll make 5*65536/256 = 1280 passes through this loop:
for (long firstPixelHue = 0; firstPixelHue < 1 * 65536; firstPixelHue += 256) {
// strip.rainbow() can take a single argument (first pixel hue) or
// optionally a few extras: number of rainbow repetitions (default 1),
// saturation and value (brightness) (both 0-255, similar to the
// ColorHSV() function, default 255), and a true/false flag for whether
// to apply gamma correction to provide 'truer' colors (default true).
ring.rainbow(firstPixelHue);
// Above line is equivalent to:
// strip.rainbow(firstPixelHue, 1, 255, 255, true);
ring.show(); // Update strip with new contents
if (1 & digitalRead(PIR1_PIN) == HIGH)
mode = 3;
delay(wait); // Pause for a moment
}
for (int i = 0; i < ring.numPixels(); i++)
{
ring.setPixelColor(i, 0); //turn every pixel off
}
ring.show();
}
// Rainbow cycle along whole strip. Pass delay time (in ms) between frames.
void rainbow_drf(int wait) {
static unsigned long last = 0;
unsigned long now = millis();
if (now - last >= wait) {
last = now;
// Hue of first pixel runs 5 complete loops through the color wheel.
// Color wheel has a range of 65536 but it's OK if we roll over, so
// just count from 0 to 5*65536. Adding 256 to firstPixelHue each time
// means we'll make 5*65536/256 = 1280 passes through this loop:
//for (long firstPixelHue = 0; firstPixelHue < 1 * 65536; firstPixelHue += 256) {
if (mode_ii < 0) mode_ii = 0;
long firstPixelHue = mode_ii++ * 256L;
if (firstPixelHue > 5 * 65536L ) {
Serial.print("***");
mode = 0;
ring.clear();
ring.show();
return; // end early
}
// strip.rainbow() can take a single argument (first pixel hue) or
// optionally a few extras: number of rainbow repetitions (default 1),
// saturation and value (brightness) (both 0-255, similar to the
// ColorHSV() function, default 255), and a true/false flag for whether
// to apply gamma correction to provide 'truer' colors (default true).
ring.rainbow(firstPixelHue);
// Above line is equivalent to:
// strip.rainbow(firstPixelHue, 1, 255, 255, true);
ring.show(); // Update strip with new contents
if (0 & digitalRead(PIR1_PIN) == HIGH)
mode = 2;
//delay(wait); // Pause for a moment
for (int i = 0; i < ring.numPixels(); i++)
{
ring.setPixelColor(i, 0); //turn every pixel off
}
//ring.show();
}
}
// Fill strip pixels one after another with a color. Strip is NOT cleared
// first; anything there will be covered pixel by pixel. Pass in color
// (as a single 'packed' 32-bit value, which you can get by calling
// strip.Color(red, green, blue) as shown in the loop() function above),
// and a delay time (in milliseconds) between pixels.
void colorWipe(uint32_t color, int wait) {
for (int i = 0; i < (strip1.numPixels(), 1); i++) { // For each pixel in strip...
for (int i = 0; i < strip2.numPixels(); i++) { // For each pixel in strip...
strip1.setPixelColor(i, color); // Set pixel's color (in RAM)
strip2.setPixelColor(i, color); // Set pixel's color (in RAM)
strip1.show(); // Update strip to match
strip2.show(); // Update strip to match
if (digitalRead(PIR2_PIN) == HIGH)
;//mode = 4;
delay(wait); // Pause for a moment
}
for (int i = 0; i < strip1.numPixels(); i++) {
strip1.setPixelColor(i, 0); //turn every pixel off
}
for (int i = 0; i < strip2.numPixels(); i++)
{
strip2.setPixelColor(i, 0); //turn every pixel off
}
strip1.show();
strip2.show();
}
}
void colorWipe_drf(uint32_t color, int wait) {
static unsigned long last = millis();
unsigned long now = millis();
if (now - last >= wait) { // do something
//Serial.print('d');
last = now;
if (mode_ii < 0) {// initialize
mode_ii = 0;
}
if (mode_ii > strip1.numPixels() ) { // done
mode = 0; // reset
mode_ii = -10;
} else { // normal operation
strip1.setPixelColor(mode_ii, color);
strip2.setPixelColor(mode_ii, color);
strip1.show();
strip2.show();
}
//Serial.print(mode_ii);Serial.print(',');
mode_ii++;
}
}