Hi there,
In my Arduino learning kit I got a seven-digit led display and a temperature sensor. I wanted to display the temperature on the display. I managed to learn everything from the forum and other sources by combining different things to get where I want. So far so good.
At first the display was flashing because it was displaying at the rate of the sensor input. I tried to use the delay function to delay the sensor input, but that also delayed the led-display. So I tried used the millis() function instead, which did the trick. It wasn't flashing anymore and the led-display worked properly.
But now, right before it gets any data from the sensor, the display is showing 0.0C. I'd like it to wait until it has the data to display.
My code:
#include "SevSeg.h"
SevSeg sevseg;
int sensorPin = A2;
float sensorValue;
float vOut;
float tempC;
int tempLED;
int tempCstring;
const unsigned long eventTime_temp = 10000;
unsigned long previousTime= 0;
void setup(){
pinMode(sensorPin, INPUT);
Serial.begin(9600);
byte numDigits = 4;
byte digitPins[] = {10, 11, 12, 13};
byte segmentPins[] = {9, 2, 3, 5, 6, 8, 7, 4};
bool resistorsOnSegments = true;
bool updateWithDelaysIn = true;
byte hardwareConfig = COMMON_CATHODE;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments);
sevseg.setBrightness(90);
}
void loop(){
unsigned long currentTime = millis();
if( currentTime - previousTime >= eventTime_temp ){
sensorValue = analogRead(sensorPin);
vOut = (sensorValue * 5000.) / 1024.;
tempC = vOut / 10.;
tempC = round(tempC*10.)/10.;
previousTime = currentTime;
Serial.println(tempC);
}
char tempCstring[10];
dtostrf(tempC, 4, 1, tempCstring);
char tempCled[10];
sprintf(tempCled, "%sC", tempCstring);
sevseg.setChars(tempCled);
sevseg.refreshDisplay();
}
If you have any other comments on cleaning up my code or other dumb beginner mistakes, please feel free to tell me as well. Thank you in advance.