i'm having a hard time cycling through analog values displayed on a 4 digit 7 segment led display. the display works as expected and it will display the values read via A0 to A3 but I can't seem to get it to really show one value after the other. it seems like it randomly displays the values.
of course this has to do with my approach using millis() currentmillis and previousmillis, but since delay() does not seem to work with sevseg.h ( values just flash for a fractions of a second when using delay() ) i had to go this way.
but see for yourselves, here's my sketch:
#include <SevSeg.h>
SevSeg display;
int moisture1 = 0, moisture2 = 0, moisture3 = 0, moisture4 = 0;
long previousMillis = 0;
long interval = 2000;
void setup() {
// initialize 7-Segment Display Common Anode
display.Begin(1,2,3,4,5,6,7,8,9,10,11,12,13);
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop() {
moisture1 = analogRead(A0);
unsigned long currentMillis = millis();
display.PrintOutput();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
display.PrintOutput();
display.NewNum(moisture1,(byte) 4);
Serial.println("moisture1: ");Serial.println(moisture1);}
moisture2 = analogRead(A1);
display.PrintOutput();
currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
display.PrintOutput();
display.NewNum(moisture2,(byte) 4);
Serial.println("moisture2: ");Serial.println(moisture2);}
moisture3 = analogRead(A2);
display.PrintOutput();
currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
display.PrintOutput();
display.NewNum(moisture3,(byte) 4);
Serial.println("moisture3: ");Serial.println(moisture3);}
moisture4 = analogRead(A3);
display.PrintOutput();
currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
display.PrintOutput();
display.NewNum(moisture4,(byte) 4);
Serial.println("moisture4: ");Serial.println(moisture4);}
}
i added serial output as well and this clearly shows that all of the values get displayed eventually, the code just doesnt cycle through them in a specific order and constant interval:
moisture3:
359
moisture4:
376
moisture4:
374
moisture2:
470
moisture2:
471
moisture1:
370
moisture4:
374
moisture3:
359
moisture3:
359
moisture2:
471
moisture1:
370
moisture2:
468
moisture2:
469
Any suggestions on how to modify my code for this to work properly?
thnx in advance, and please excuse me if this is a really stupid question, i'm just starting programming for the arduino.