Hello everybody,
I have a problem with the numbers displayed on my three 7 segment displays.
For context I have a project that consists of building a thermometer. The main functions are buttons(), calsius(), fahrenheit() and the loop. I might have variables that aren't used, but they will be gone by the time the project is done.
code:
unsigned long ctimeS;
unsigned long ptimeS = 0;
int timeS = 5;
String serial;
#include <string.h>
//leds 7 segment display
#define A 7
#define B 6
#define C 4
#define D 3
#define E 2
#define F 8
#define G 9
#define Dp 5
//7 segment displays
#define display1 10
#define display2 11
#define display3 13
int h;
//buttons
#define change A5
bool Change = false;
int counterCF = 0;
#define plus A3
bool Plus = false;
#define min A2
bool Min = false;
#define buzzer A1
bool Buzzer = false;
float buzzT = 10.0;
//temps
float Serial_Temp_C, Passing_Temp, Temp_C_or_F, Temp, NUM, x, BuzzT;
//time
unsigned long ctime, ptime = 0, ctimeC, ptimeC = 0, ctimeF, ptimeF = 0;
//sensor
#define LMT86 A0
float sensorValue;
//serial
int ScounterCF = 0; //counter for switch from celsius to fahreneheit and opposite
//debouncing
unsigned long const debounceDuration = 500; //interval
void setup() {
// put your setup code here, to run once:
//Leds segment display
Serial.begin(9600);
Serial.setTimeout(10); //this will stop everything in serial to give you, and check, the infromation
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(C, OUTPUT);
pinMode(D, OUTPUT);
pinMode(E, OUTPUT);
pinMode(F, OUTPUT);
pinMode(G, OUTPUT);
pinMode(Dp, OUTPUT);
//Buttons
pinMode(change, INPUT);
pinMode(plus, INPUT);
pinMode(min, INPUT);
pinMode(buzzer, INPUT);
//displays
pinMode(display1, OUTPUT);
pinMode(display2, OUTPUT);
pinMode(display3, OUTPUT);
celsius();
}
void loop() {
// put your main code here, to run repeatedly:
Passing_Temp = Temp_C_or_F;
BuzzT = buzzT;
buttons();
which();
tone();
if(Serial.available() > 0){ //if number of bytes is greater than 0 read the string
serial = Serial.readStringUntil("\n");
serial.trim();
Serial.println(serial);
serial.toLowerCase();
if(serial == "cel"){
//Celsius
Stemp();
}
else if(serial == "fahr"){
//Fahrenheit
Stemp();
}
else if(serial == "threshold"){
//Switch to threshold changer
buzzing();
}
else if(serial == "plus"){
//temp +0.5
buzzing();
}
else if(serial == "minus"){
//temp -0.5
buzzing();
}
else{
Serial.println("Not a correct string. Choose from cel, fahr, threshold, plus and minus!");
}
}
}
void buttons(){
ctime = millis();
if(ctime - ptime >= debounceDuration){
if(digitalRead(change) == true){
//change from c to f or f to c
if(ScounterCF%2 == 0){
celsius();
ScounterCF++;
ptime = ctime;
}
else if(ScounterCF%2 == 1){
fahrenheit();
ScounterCF++;
ptime = ctime;
}
Change = true;
Buzzer = false;
Plus = false;
Min = false;
}
else if(digitalRead(plus) == true){
//increase limit +0,5
Change = false;
Plus = true;
Buzzer = true;
number(buzzT);
buzzing();
ptime = ctime;
}
else if(digitalRead(min) == true){
//decrease limit -0,5
Change = false;
Buzzer = true;
Min = true;
number(BuzzT);
buzzing();
ptime = ctime;
}
else if(digitalRead(buzzer) == true){
//do something with the buzzer
Change = false;
Buzzer = true;
number(BuzzT);
buzzing();
ptime = ctime;
}
}
}
void which(){
if (((Change == true) && (Buzzer == false)) || serial == "cel" || serial == "fahr"){
number(Passing_Temp);
}
else if(((Buzzer == true) && (Change == false)) || serial == "plus" || serial == "minus" || serial == "threshold"){
number(BuzzT);
buzzing();
}
}
void tone(){
if(buzzT > Passing_Temp){
tone(buzzer, 1000);
}
else{
noTone(buzzer);
}
}
void buzzing(){
number(BuzzT);
if(Plus == true || serial == "plus"){
buzzT += 0.5;
number(buzzT);
Plus = false;
serial = "threshold";
}
else if(Min == true || serial == "minus"){
buzzT -= 0.5;
number(buzzT);
Min = false;
serial = "threshold";
}
else if(Buzzer == true || serial == "threshold"){
number(buzzT);
}
}
void Stemp(){
if(serial == "cel"){
sensorValue = analogRead(LMT86); //read value of analogpin
float voltageC = (sensorValue*5/1024.0);
voltageC *= 1000;
//Temp_C_or_F = voltageC - 2100; // 2100 comes from datasheet and is mV when 0 °C
//Temp_C_or_F = Temp_C_or_F*50; // 50 is the max range (0 to 50 °C) and -442 is mV from T1 - mv from T2
//Temp_C_or_F = Temp_C_or_F/(-442.0);
Temp_C_or_F = sqrt(pow(-10.888,2) + 4*0.00347*(1777.3 - voltageC));
Temp_C_or_F = (10.888 - Temp_C_or_F)/(2*-0.00347);
Temp_C_or_F = Temp_C_or_F + 30;
Temp_C_or_F = round(Temp_C_or_F * 10);
Temp_C_or_F = Temp_C_or_F/10;
ptimeC = ctimeC;
number(Temp_C_or_F);
Serial.print("Temperature in Celsius: ");
Serial.print(Temp_C_or_F);
Serial.println(" °C");
}
else if(serial == "fahr"){
sensorValue = analogRead(LMT86); //read value of analogpin
float voltageF = (sensorValue*5/1024.0);
voltageF *= 1000;
//Temp_C_or_F = voltageF - 2100;
//Temp_C_or_F = Temp_C_or_F*50;
//Temp_C_or_F = Temp_C_or_F/(-442.0);
Temp_C_or_F = sqrt(pow(-10.888,2) + 4*0.00347*(1777.3 - voltageF));
Temp_C_or_F = (10.888 - Temp_C_or_F)/(2*-0.00347);
Temp_C_or_F = Temp_C_or_F + 30;
Temp_C_or_F = (Temp_C_or_F*9/5) + 32;
Temp_C_or_F = round(Temp_C_or_F*10);
Temp_C_or_F = Temp_C_or_F/10;
ptimeF = ctimeF;
number(Temp_C_or_F);
Serial.print("Temperature in Fahrenheit: ");
Serial.print(Temp_C_or_F);
Serial.println(" °F");
}
}
void celsius(){
ctimeC = millis();
if(ctimeC - ptimeC >= debounceDuration){
sensorValue = analogRead(LMT86); //read value of analogpin
float voltageC = (sensorValue*5/1024.0);
voltageC *= 1000;
//Temp_C_or_F = voltageC - 2100; // 2100 comes from datasheet and is mV when 0 °C
//Temp_C_or_F = Temp_C_or_F*50; // 50 is the max range (0 to 50 °C) and -442 is mV from T1 - mv from T2
//Temp_C_or_F = Temp_C_or_F/(-442.0);
Temp_C_or_F = sqrt(pow(-10.888,2) + 4*0.00347*(1777.3 - voltageC));
Temp_C_or_F = (10.888 - Temp_C_or_F)/(2*-0.00347);
Temp_C_or_F = Temp_C_or_F + 30;
Temp_C_or_F = round(Temp_C_or_F * 10);
Temp_C_or_F = Temp_C_or_F/10;
ptimeC = ctimeC;
number(Temp_C_or_F);
Serial.print("Temperature in Celsius: ");
Serial.print(Temp_C_or_F);
Serial.println(" °C");
}
}
void fahrenheit(){
ctimeF = millis();
if(ctimeF - ptimeF >= debounceDuration){
sensorValue = analogRead(LMT86); //read value of analogpin
float voltageF = (sensorValue*5/1024.0);
voltageF *= 1000;
//Temp_C_or_F = voltageF - 2100;
//Temp_C_or_F = Temp_C_or_F*50;
//Temp_C_or_F = Temp_C_or_F/(-442.0);
Temp_C_or_F = sqrt(pow(-10.888,2) + 4*0.00347*(1777.3 - voltageF));
Temp_C_or_F = (10.888 - Temp_C_or_F)/(2*-0.00347);
Temp_C_or_F = Temp_C_or_F + 30;
Temp_C_or_F = (Temp_C_or_F*9/5) + 32;
Temp_C_or_F = round(Temp_C_or_F*10);
Temp_C_or_F = Temp_C_or_F/10;
ptimeF = ctimeF;
number(Temp_C_or_F);
Serial.print("Temperature in Fahrenheit: ");
Serial.print(Temp_C_or_F);
Serial.println(" °F");
}
}
void number(float x){
x = x*10;
int NUM = int(x);
//hecto
h = NUM/100;
digitalWrite(display3, HIGH);
digitalWrite(Dp, LOW);
delay(5);
segments(h);
digitalWrite(display3, LOW);
//deca
h = (NUM%100)/10;
digitalWrite(display1, HIGH);
digitalWrite(Dp, LOW);
delay(5);
segments(h);
digitalWrite(display1, LOW);
//integer
h = NUM%10;
digitalWrite(display2, HIGH);
digitalWrite(Dp, HIGH);
delay(5);
segments(h);
digitalWrite(display2, LOW);
}
void ghost() {
digitalWrite(A, LOW);
digitalWrite(B, LOW);
digitalWrite(C, LOW);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
}
void segments(int num){
ctimeS = millis();
if(ctimeS - ptimeS >= timeS){
ghost();
switch(num){
case 0:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, LOW);
break;
case 1:
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
case 2:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, LOW);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
break;
case 3:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, HIGH);
break;
case 4:
digitalWrite(A, LOW);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
case 5:
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
case 6:
digitalWrite(A, HIGH);
digitalWrite(B, LOW);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
case 7:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, LOW);
digitalWrite(E, LOW);
digitalWrite(F, LOW);
digitalWrite(G, LOW);
break;
case 8:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, HIGH);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
case 9:
digitalWrite(A, HIGH);
digitalWrite(B, HIGH);
digitalWrite(C, HIGH);
digitalWrite(D, HIGH);
digitalWrite(E, LOW);
digitalWrite(F, HIGH);
digitalWrite(G, HIGH);
break;
}
ptimeS = ctimeS;
}
}
Now the problem lies when I display the output on the displays. When I run the code by using the serial communication it always gives me a number higher than when i press on the dedicated button (change A5). I am wondering if I did something wrong with the code and if someone can help I would greatly appreciate it.
Output:
sensorValue: 394.00
Temperature in Celsius: 16.50 °C
sensorValue: 395.00
Temperature in Fahrenheit: 60.90 °F
cel
sensorValue: 387.00
Temperature in Celsius: 19.60 °C
fahr
sensorValue: 387.00
Temperature in Fahrenheit: 67.40 °F
sensorValue: 393.00
Temperature in Celsius: 16.90 °C
sensorValue: 395.00
Temperature in Fahrenheit: 60.90 °F
cel
sensorValue: 389.00
Temperature in Celsius: 18.70 °C
fahr
sensorValue: 390.00
where cel or fahr is written is from serial comm and the 2 lines below are the outputs of serial comm
when only sensorValue and Temperature in Celsius or fahrenheit is written is from pressing the button;
this is my schematic of the project