Hello everybody,
I have an issues with my code. Every time i try to implement my buzzer function in my code it does some weird glitching.
my thermometer can switch between fahrenheit and celsius if i push on one button. When the buzzer() function is in the I can push one time on the change button afterwards it keep switching every x second from fahrenehit to the threshold limit (10.0 in this case) and then goes back to fahrenheit and does this loop forever.
I think it the issue lies somewhere here
void loop() {
update();
if (update_millis - last_update >= update_interval) {
// If the time since the last update is greater than or equal to the interval
if (!boolthresh) {
Serial.print("Hello5\n");
Temperature(); // Continuously update temperature when in temperature mode
} else if (boolthresh){
Serial.print("Hello6\n");
threshold(); // Continuously update threshold when in threshold mode
}
last_update = update_millis; // Update the last update time
}
Serialcom();
buzzer();
if (!boolthresh) {
nummer(temp);
Serial.print("Hello1\n");
} else {
nummer(thresh_limit);
Serial.print("Hello2\n");
}
}
void buzzer() {
if (temp >= thresh_limit) { // Activate buzzer when temperature is above the threshold
tone(thresh, 1000); // 1000 Hz tone
} else {
noTone(thresh); // Turn off buzzer
}
}
void buttons() {
ctime = millis();
reading_cf = digitalRead(change);
reading_th = digitalRead(thresh);
if (ctime - ptime >= db) {
if (reading_cf == HIGH) {
boolthresh = false;
counter_cf++;
cel = counter_cf % 2 == 0;
fahr = !cel;
Temperature();
ptime = ctime;
Serial.print("boolthresh false\n");
}
else if (reading_th == HIGH) {
cel = false;
fahr = false;
boolthresh = !boolthresh;
threshold();
ptime = ctime;
Serial.print("boolthresh true\n");
}
}
}
and the output when buzzer is in the function is this.
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Temperature in Celsius: 25.70 °C
boolthresh false
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
boolthresh true
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
boolthresh true
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello5
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
Hello1
boolthresh true
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
ello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
Hello2
full code without buzzer()
String serial;
#include <string.h>
//Temperature sensor
#define tempsens A0
float sensorvalue = 0;
//LED
#define A 7
#define B 6
#define C 4
#define D 3
#define E 2
#define F 8
#define G 9
#define Dp 5
#define display1 10
#define display2 11
#define display3 13
//Buttons
//Change fron C to F
#define change A5
int counter_cf = 0;
bool reading_cf = LOW;
bool cel = LOW;
bool fahr = LOW;
//Increasing limit of buzzer +0.5
#define plus A3
bool reading_plus = LOW;
//Decrease limit of buzzer -0.5
#define min A2
bool reading_min = LOW;
//Change to threshold screen
#define thresh A1
bool reading_th = LOW, boolthresh = LOW, active = false;
float thresh_limit = 10.0;
//LOGIC
unsigned long ctime = millis(), ctimet = millis(), update_millis = millis(); //continuous time
unsigned long ptime = 0, ptimet = 0; //previous time
int db = 1000, dbt = 1000; //debounce duration
int h;
float temp, voltage, Vtot = 0;
unsigned long last_update = 0;
int update_interval = 3000;
void setup() {
// put your setup code here, to run once:
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(thresh, INPUT);
//displays
pinMode(display1, OUTPUT);
pinMode(display2, OUTPUT);
pinMode(display3, OUTPUT);
}
void update(){
updateMillis();
buttons();
}
void loop() {
update();
if (update_millis - last_update >= update_interval) {
// If the time since the last update is greater than or equal to the interval
if (!boolthresh) {
Temperature(); // Continuously update temperature when in temperature mode
} else {
threshold(); // Continuously update threshold when in threshold mode
}
last_update = update_millis; // Update the last update time
}
Serialcom();
if (!boolthresh) {
nummer(temp);
} else {
nummer(thresh_limit);
}
}
void buzzer() {
if (temp >= thresh_limit) { // Activate buzzer when temperature is above the threshold
tone(thresh, 1000); // 1000 Hz tone
} else {
noTone(thresh); // Turn off buzzer
}
}
void updateMillis() {
ctimet = millis();
ctime = millis();
update_millis = millis();
}
void Serialcom() {
if (Serial.available() > 0) {
serial = Serial.readStringUntil("\n");
serial.trim();
serial.toLowerCase();
if (serial == "cel") {
cel = true;
fahr = false;
boolthresh = false;
Temperature();
}
else if (serial == "fahr") {
fahr = true;
cel = false;
boolthresh = false;
Temperature();
}
else if (serial == "thresh") {
boolthresh = true;
Serial.print("Threshold limit is: ");
Serial.println(thresh_limit);
threshold();
}
else if (serial == "plus") {
thresh_limit += 0.5;
Serial.print("Threshold limit is: ");
Serial.println(thresh_limit);
threshold();
}
else if (serial == "minus") {
thresh_limit -= 0.5;
Serial.print("Threshold limit is: ");
Serial.println(thresh_limit);
threshold();
} else {
Serial.println("Not a correct string. Choose from cel, fahr, thresh, plus, and minus!");
}
}
}
void threshold() {
nummer(thresh_limit);
reading_plus = digitalRead(plus);
reading_min = digitalRead(min);
if(ctimet - ptimet >= dbt){
if(boolthresh){
if(reading_plus == HIGH) {
thresh_limit += 0.5;
Serial.print("Threshold limit increased to: ");
Serial.println(thresh_limit);
}
else if (reading_min == HIGH) {
thresh_limit -= 0.5;
Serial.print("Threshold limit decreased to: ");
Serial.println(thresh_limit);
}
}
ptimet = ctimet;
}
}
void buttons() {
ctime = millis();
reading_cf = digitalRead(change);
reading_th = digitalRead(thresh);
if (ctime - ptime >= db) {
if (reading_cf == HIGH) {
boolthresh = false;
counter_cf++;
cel = counter_cf % 2 == 0;
fahr = !cel;
Temperature();
ptime = ctime;
}
else if (reading_th == HIGH) {
cel = false;
fahr = false;
boolthresh = !boolthresh;
threshold();
ptime = ctime;
}
}
}
void Temperature() {
Vtot = 0;
for (int i = 0; i < 20; i++) {
sensorvalue = analogRead(tempsens);
voltage = sensorvalue * (5000 / 1024.0);
Vtot += voltage;
}
Vtot = Vtot / 20.0;
if (cel) {
temp = (Vtot - 2103) / (-10.9);
temp = round(temp * 10) / 10.0;
Serial.print("Temperature in Celsius: ");
Serial.print(temp);
nummer(temp);
Serial.println(" °C");
}
else if (fahr) {
temp = (Vtot - 2103) / (-10.9);
temp = (temp * 9.0 / 5.0) + 32.0;
temp = round(temp * 10) / 10.0;
Serial.print("Temperature in Fahrenheit: ");
Serial.print(temp);
nummer(temp);
Serial.println(" °F");
}
}
void nummer(float n){
n = n*10;
int num = int(n);
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){
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;
}
}
wiring diagram