Hey, y'all. I'm fresh to the forum but I've been reading through all kinds of posts here and on other forums. I'm still having the hardest time trying to figure out how to ditch the delays and switch to using millis. I've been spending hours pouring through the Adafruit multi-tasking tutorial and through the NeoPatterns tutorial. I can get the neopatterns example to work, but I can't get it to function the way I need it to. The examples show it kicking off with a pattern and then switching patterns with a button press. I'm however trying to start off with all leds off then running a pattern when triggered by PIR. Basically I'm trying to get two 16bit rings to run colorwipe in opposite directions at the same time, which means I need to ditch the delay codes. I'd greatly appreciate if someone could take a look at this and help me figure out how to convert it. I've left out my coding for DFPlayerMini since it's mostly irrelevant here at the moment.
#include <Adafruit_NeoPixel.h>
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#define DEBUG //serial prints
Adafruit_NeoPixel EYE_l = Adafruit_NeoPixel(16, 11, NEO_GRB + NEO_KHZ800);
Adafruit_NeoPixel EYE_r = Adafruit_NeoPixel(16, 12, NEO_GRB + NEO_KHZ800);
static const uint8_t PIN_MP3_TX = 2; // Connects to module's RX
static const uint8_t PIN_MP3_RX = 3; // Connects to module's TX
SoftwareSerial mySoftwareSerial(3, 2); // RX, TX
int PIRmotion1 = 10; // choose the input pin (for PIR sensor)
int PIRmotion2 = 9;
int PIRstate = LOW; // we start, assuming no motion detected
int PIRval1 = 0; // variable for reading the pin status
int PIRval2 = 0; // variable for reading the pin status
void colorWipe_l(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<EYE_l.numPixels(); i++) {
EYE_l.setPixelColor(i, c);
EYE_l.show();
delay(wait);
}
}
void reverseColorWipe_r(uint32_t c, uint8_t wait) {
for(int16_t i=(EYE_r.numPixels()-1); i>=0; i--) {
EYE_r.setPixelColor(i, c);
EYE_r.show();
delay(wait);
}
}
void mirroredColorWipe(uint32_t c, uint8_t wait) {
for(int16_t i=(EYE_r.numPixels()-1); i>=0; i--) {
EYE_r.setPixelColor(i, c);
EYE_r.show();
delay(wait);
}
for(uint16_t i=0; i<EYE_l.numPixels(); i++) {
EYE_l.setPixelColor(i, c);
EYE_l.show();
delay(wait);
}
}
void lightsUp(){
colorWipe_l(EYE_l.Color(0,255,0), 22);
reverseColorWipe_r(EYE_r.Color(0,255,0), 22);
//delay(6666); // wait for a second
}
void lightsOut(){
mirroredColorWipe((0,0,0),22);
delay(666);
}
void showBoth(){
EYE_l.show();
EYE_r.show();
}
void setup() {
// put your setup code here, to run once:
EYE_l.begin(); //starts the NeoPixel sketch
EYE_r.begin(); //starts the NeoPixel sketch
showBoth();
EYE_l.setBrightness(66);
EYE_r.setBrightness(66);
Serial.begin(9600);
pinMode(PIRmotion1, INPUT); // declare sensor as input
pinMode(PIRmotion2, INPUT); // declare sensor as input
pinMode(11, OUTPUT);//define arduino pin
pinMode(12, OUTPUT);//define arduino pin
Serial.println("startup");
}
void loop() {
// put your main code here, to run repeatedly:
PIRval1 = digitalRead(PIRmotion1);
PIRval2 = digitalRead(PIRmotion2);
if(PIRval1 == HIGH || PIRval2 == HIGH)
{
lightsUp();
if(PIRstate == LOW)
{
Serial.println(F("Motinon Detected!"));
PIRstate = HIGH; //update to HIGH
delay(6000);
}
}
else{
lightsOut();
if(PIRstate == HIGH)
{
Serial.println(F("Motion stopped!"));
PIRstate = LOW;
}
delay(6000);
}
}