Hello all, So currently im creating a code where if no buttons are pressed on a series of buttons, a few neopixel Leds will begin to cycle through RGB. The issue is, if i need to interrupt the code at any moment, i have to wait a few seconds before it finally changes out of the condition.
For context, every button will correlate with a color button A is red, button B is orange, etc. The code isn't fully down yet as i need to make sure all the condition due their intended result. Below is small snippit of how the condition will work:
if ((a && b && x && y && dl && (xs == 360 || xs == 361)) == 1) // If all buttons are stationary
{
rainbowCycle(50);
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}
I tried to replace delay with this:
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
if (currentMillis - previousMillis >= interval) //Replacing delay with a interval of 50
{
previousMillis = currentMillis;
}
}
}
But the code does not work. how is the proper way to replace delay in the original code with millis() so that i can do the color fade and be interrupted at any moment? Thank you
Below is the entire code:
#include <Adafruit_NeoPixel.h>
#define NUM_LEDS 3
#define BRIGHTNESS 75
#define PIN A0
//const int LED = A0;
const int Xs = 9;// pins the buttons are attached too
const int A = 4;//
const int B = 5;//
const int X = 8;//
const int Y = 7;//
const int DL = 6;//
int lastButtonState;
int buttonState;
double long aPressed; // values to insert millis value
double long bPressed;
double long xPressed;
double long yPressed;
double long dlPressed;
double long xsPressed;
byte R; // byte variables
byte G;
byte V;
int zero = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis = millis();
const long interval = 50;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
int a = digitalRead(A); //read digital inputs into integers
int b = digitalRead(B);
int x = digitalRead(X);
int y = digitalRead(Y);
int dl = digitalRead(DL);
int xs = analogRead(Xs); // 0-360-728
void setup() {
pinMode(A, INPUT);
pinMode(B, INPUT);
pinMode(X, INPUT);
pinMode(Y, INPUT);
pinMode(DL, INPUT);
pinMode(Xs, INPUT);
Serial.begin(9600);
strip.setBrightness(BRIGHTNESS);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
byte Wheel = 0;
}
void loop() {
double long Millis = millis();
int a = digitalRead(A);
int b = digitalRead(B);
int x = digitalRead(X);
int y = digitalRead(Y);
int dl = digitalRead(DL);
int xs = analogRead(Xs); // 0-360-728
//Serial.println("Letter b is");
Serial.print(a); // To freeze the lower limit
Serial.print(" ");
//Serial.print(2); // To freeze the upper limit
Serial.print(" ");
//Serial.println(a);
//Serial.println("Letter x is");
//Serial.println(x);
//Serial.println("-------------------------");
//rainbowCycle(10);
//------------------------CONDITIONS BEGIN HERE------------------------------------------//
if (b == 0) //when pressed, turn on LEDs to RED
{
strip.clear();
bPressed = Millis;
strip.setPixelColor(2, 255, 0, 0);
strip.show();
strip.setPixelColor(1, 255, 0, 0);
strip.show();
strip.setPixelColor(0, 255, 0, 0);
strip.show();
strip.clear();
}
else if (30 + bPressed > Millis) // bPressed will be higher than Millis momentarily, execute color fade
{
uint16_t ib, jb;
for (jb = 255; jb > 0; jb--) {
for (ib = 0; ib < 2; ib++) {
strip.setPixelColor(ib, jb, 0, 0);
}
strip.show();
//Serial.println(j);
strip.clear();
}
}
if (x == 0)
{
xPressed = Millis;
for (int ixx=0; ixx<3; ixx++)
strip.setPixelColor(ixx, 0, 255, 255);
strip.show();
}
else if (30 + xPressed > Millis)
{
uint16_t ix, jx;
for (jx = 255; jx > 0; jx--) {
for (ix = 0; ix < 2; ix++) {
strip.setPixelColor(ix, 0, jx, jx);
}
strip.show();
//Serial.println(j);
strip.clear();
}
}
else if (x == 1)
{
for (int I = 0; I < 2; I++)
strip.setPixelColor(I, 0, 0, 0);
strip.show();
}
strip.clear();
if ((a && b && x && y && dl && (xs == 360 || xs == 361)) == 1) // If all buttons are stationary
{
rainbowCycle(50);
}
void rainbowCycle(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
}
strip.show();
delay(wait);
}
}