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