Hi,
I'm new at using millis() instead of delay() and am still trying to wrap my head around how to use it. I have an LED display and I want to have it cycle through several patterns. Eventually I want to have many many patterns, but while I am trying to figure this out I am using just 3. Basically I want a certain LED pattern to be sustained for 1000 ms, then another one for 1000ms, etc.
I tried using currentMillis and previousMillis variables, then used a different variable, previousMillis2 for a different pattern, but it seemed like it was cycling between the two patterns really quickly. So I added a variable that would increment through each interval, but that's not really working either.
Does this make sense? Should I go about this another way? I put the code below. Thank you for any help.
#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
Adafruit_7segment matrix = Adafruit_7segment();
long previousMillis = 0; // last time a change happened
long interval = 1000;
int display1 = 1;
void setup() {
#ifndef __AVR_ATtiny85__
Serial.begin(9600);
Serial.println("7 Segment Backpack Test");
#endif
matrix.begin(0x70);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval && display1 < 4) {
previousMillis = currentMillis;
display1 = display1 + 1;
}
if (currentMillis - previousMillis > interval && display1 > 4) {
previousMillis = currentMillis;
display1 = 1;
}
if (display1 = 1) {
matrix.clear();
matrix.writeDigitRaw(0,1); // LED pattern 1
matrix.writeDigitRaw(1,1);
matrix.writeDigitRaw(3,1);
matrix.writeDigitRaw(4,1);
matrix.writeDisplay();
}
if (display1 = 2) {
matrix.clear();
matrix.writeDigitRaw(0,64);
matrix.writeDigitRaw(1,64); // LED pattern2
matrix.writeDigitRaw(3,64);
matrix.writeDigitRaw(4,64);
matrix.writeDisplay();
}
if (display1 = 3) {
matrix.clear();
matrix.writeDigitRaw(0,8);
matrix.writeDigitRaw(1,8); // LED pattern3
matrix.writeDigitRaw(3,8);
matrix.writeDigitRaw(4,8);
matrix.writeDisplay();
}
}