I've discovered that using a delay messes up the sevseg display (and I'm also learning it's better to use Millis instead of delay. But the only way I've figured out to make sevseg work with Millis is by putting the display code into the middle of Millis delay (lines 44 & 45, 53 & 54 and 59 & 60). But if I try to put the sevseg code where I think it makes sense and is more simple (lines 26 & 27), I end up with the same scrambled display as I had with the delay function. Can you help me understand why the display code has to be in the "delay" code? Also, is there a better way to do this? It's just a count down timer that flashes "0000" at the end. Thanks.
#include <SevSeg.h>
SevSeg sevseg;
const long interval = 1000;
long previousMillis = 0;
unsigned long currentMillis = 0;
bool counting = true;
int number = 10;
void setup() {
Serial.begin(115200);
byte numDigits = 4;
byte digitPins[] = {2, 3, 4, 5};
byte segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
bool resistorsOnSegments = 0;
sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(30);
}
void loop() {
if (counting){
//sevseg.setNumber(number);
//sevseg.refreshDisplay();
delay();
number -= 1;
if (number < 0){
counting = !counting;
}
}
else{
flashing();
}
}
void delay(){
while (currentMillis - previousMillis < interval){
currentMillis = millis();
sevseg.setNumber(number);
sevseg.refreshDisplay();
}
previousMillis = currentMillis;
}
void flashing(){
while (currentMillis - previousMillis < interval){
currentMillis = millis();
sevseg.setChars("0000");
sevseg.refreshDisplay();
}
previousMillis = currentMillis;
while (currentMillis - previousMillis < interval){
currentMillis = millis();
sevseg.blank();
sevseg.refreshDisplay();
}
previousMillis = currentMillis;
}
10 posts - 7 participants