different LED patterns using millis()

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();

 }
 
 

}

nicnut:
if (display1 = 1) {

= and == aren't interchangeable.

use CTRL-T to autoindent the code, makes it more readable

#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 pattern 2
matrix.writeDigitRaw(3,64);
matrix.writeDigitRaw(4,64);
matrix.writeDisplay();
}

if (display1 == 3)
{
matrix.clear();
matrix.writeDigitRaw(0,8);
matrix.writeDigitRaw(1,8); // LED pattern 3
matrix.writeDigitRaw(3,8);
matrix.writeDigitRaw(4,8);
matrix.writeDisplay();
}
}

by using an array of patterns and one function you can prevent "duplicate" code

#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;

int pattern[4] = { 
  0, 1, 64, 8 };

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;
  }

  updateMatrix( pattern[display1] );

}

void updateMatrix(int val)
{
  matrix.clear();
  matrix.writeDigitRaw(0, val);  
  matrix.writeDigitRaw(1, val); // LED pattern 3
  matrix.writeDigitRaw(3, val);
  matrix.writeDigitRaw(4, val);
  matrix.writeDisplay();
}

Hi, Thank you both for your replies. I think one thing I forgot to mention is that I want to cycle between these 3 LED patterns. robtillaart I used your code which worked, but didn't cycle back to the beginning for some reason, so I added this:

if (display1 >= 4) {

display1 = 1;
}

Which seemed to solve the problem. Also, I am still learning when to use = as opposed to ==. I also need to learn when to use a new function, which I don't quite understand, but will look into. Anyway thanks for your help.

Here is the code that worked in the end:

#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 = 200;
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 >= 4) {
   
  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 pattern 2
    matrix.writeDigitRaw(3,64);
    matrix.writeDigitRaw(4,64);
    matrix.writeDisplay();
  }

  if (display1 == 3) 
  {
    matrix.clear();
    matrix.writeDigitRaw(0,8); 
    matrix.writeDigitRaw(1,8); // LED pattern 3
    matrix.writeDigitRaw(3,8);
    matrix.writeDigitRaw(4,8);
    matrix.writeDisplay();
  }
}

Also, I am still learning when to use = as opposed to ==.

one way to help you is to put the constant in front if possible (real constant or a const var)

if (a == 4) ... will compile
if (a = 4) ... will compile (but is probably wrong)
if (4 == a) ... will compile
if (4 = a) ... will not compile

robtillaart:

Also, I am still learning when to use = as opposed to ==.

one way to help you is to put the constant in front if possible (real constant or a const var)

if (a == 4) ... will compile
if (a = 4) ... will compile (but is probably wrong)
if (4 == a) ... will compile
if (4 = a) ... will not compile

A handy tip, Rob, but of no use when comparing two variables.
if (a==b)...