[SOLVED]Another question about BWD and millis()

Hi,

I am a newbie and a have been following along with some of the recent programming threads regarding BWD and millis(). I am trying to learn about this so I can get all the delay() commands out of my code, but haven't yet had any success.

I am working with one of those 7 segment display LED items from adafruit

and am trying to update my code to get rid of the delay()s.
I tried to work something off a variation of the BWD code, but it's not working. It's supposed to randomly blink to a new configuration every 2 seconds, but it looks like it's instead blinking every millisecond.

If anyone has any insight into this I would greatly appreciate it. I put my code below.
Thanks, Nick

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

unsigned long currentMillis = millis();
long previousMillis = 0; // last time a change happened
long interval = 2000;   // how often a change occurs

void setup() {
#ifndef __AVR_ATtiny85__
  Serial.begin(9600);
  

  Serial.println("7 Segment Backpack Test");
#endif
  matrix.begin(0x70);
}

void loop() {

  if (currentMillis - previousMillis > interval) {
    
    currentMillis = previousMillis;
  }
  
  if ( currentMillis == previousMillis)
    
      matrix.clear();
 matrix.writeDigitRaw(2,random(30)); // 30 possible combinations
   matrix.writeDisplay();
  
   
 }

Very easy answer. You need the millis() function in your loop() function.

EDIT: a little more details:

Watch the original BWD sketch:

// constants won't change. Used here to set a pin number :
const int ledPin =  13;      // the number of the LED pin

// Variables will change :
int ledState = LOW;             // ledState used to set the LED

// Generally, you shuould use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }
}

see the differnce between this and your loop() function:

void loop() {

  if (currentMillis - previousMillis > interval) {
    
    currentMillis = previousMillis;
  }
  
  if ( currentMillis == previousMillis)
    
      matrix.clear();
 matrix.writeDigitRaw(2,random(30)); // 30 possible combinations
   matrix.writeDisplay();
  
   
 }

you need this line in this function:

unsigned long currentMillis = millis();

Have you checked on that?http://arduino.cc/en/Tutorial/BlinkWithoutDelay It's a basic example of how to use millis(); instead of delay(); :wink:

OK thanks for the replies. That makes sense, but i'm still seeing the display blink really fast instead of every 2 seconds.

This is what I put in my loop after your suggestion. Is this correct:

void loop() {
  
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis > interval) {
    
    currentMillis = previousMillis;
  }
  
  if ( currentMillis == previousMillis)
    
      matrix.clear();
 matrix.writeDigitRaw(2,random(30)); // 30 possible combinations
   matrix.writeDisplay();
  
   
 }

What is the value of "interval" ?
For example if it is :interval = 1000; then it will blink every second.. 1000ms=1second.. so change that value to the appropriate one ..

I put this above the void setup()

long interval = 2000; // every 2 seconds

nicnut:
OK thanks for the replies. That makes sense, but i'm still seeing the display blink really fast instead of every 2 seconds.

This is what I put in my loop after your suggestion. Is this correct:

you need to fix this:

unsigned long currentMillis;
unsigned long previousMillis; // last time a change happened
unsigned long interval = 2000UL;   // how often a change occurs

move to your setup()

previousMillis = millis();

and your loop()...

void loop() 
{
  currentMillis = millis()
  if (currentMillis - previousMillis >= interval) 
  {
    matrix.clear();
    matrix.writeDigitRaw(2,random(30)); // 30 possible combinations
    matrix.writeDisplay();
    previousMillis = millis();
  }

}

every two seconds it will update the display... is that what you want to do?

I made some corrections , you can try that :

long previousMillis = 0;        
long interval = 2000;
void loop() {
  
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis > interval) {
    
     previousMillis = currentMillis;   
    
      matrix.clear();
 matrix.writeDigitRaw(2,random(30)); // 30 possible combinations
   matrix.writeDisplay();
}
  
   
 }

OK that solved the problem. Thanks everyone for your help. Zaxarias I used your code and that basically did it.
I was getting sort of confused. One mistake I was doing was declaring previousMillis as an unsigned long. When I used "long" that worked. Also I was a little confused if I should declare my variables above setup(), in setup(), or in loop(). I tried a bunch of different combinations till something worked. It seems that

unsigned long currentMillis = millis();

had to be in loop() and the other stuff had to be above setup(). Thank you everyone for your help I put the code below.

#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 = 2000; 
  
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) {
    
     previousMillis = currentMillis;   
    
      matrix.clear();
 matrix.writeDigitRaw(2,random(30)); // 30 possible combinations
   matrix.writeDisplay();
}
  
   
 }

nice to hear you made it :smiley: :smiley:

OK luisilva, Done. Thank you all again for your help.

Nick