Piezosensor keeps sending sound from temperature reading

Hey! I made this setup with a temperaturereader and piezo.
The idea is, when the temperature reaches above 23 degrees, the piezo will play a sound. When it goes below, the sound will stop.
The sound doesn't stop.
What to do?

Thanks!

//variabler til lås
const int redPin = 13;
const int greenPin = 12;
const int knockSensor = A1;
const int threshold = 100;
int sensorReading = 0;
int lockLedState = HIGH;

//variabler til temperatur
float voltRead;
const int temperaturePin = A4;
const int yellowPin =  11;
int temperaturLedState = HIGH;
unsigned long previousMillis = 0;
int voltInCelc = 0;

//variabler til lyd
int soundLedState = LOW;
const int piezoSensor = A1;
//knockSensor A1, noteA, noteB;

boolean ON_OFF = false;

void setup() {
  Serial.begin(9600);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
}

void loop() {
  sensorReading = analogRead(knockSensor);
  lockSensor();
  getTemperature(temperaturePin);
  delay(500);
}

void getTemperature(int temperaturePin) {
  float voltRead = analogRead(temperaturePin);
  float volt = voltRead * (5.0 / 1023);
  float voltInCelc = (volt - 0.5) * 100;
  Serial.println(voltInCelc);

  Serial.println(ON_OFF);
  if (voltInCelc >= 23 || voltInCelc <= 1) { //Til tests er det 24 - normalt køleskab er måske maks 7 *c.
    ON_OFF = true;
    while (ON_OFF = true) {
      temperaturAlarm(50);
      piezoSound();
    }
  } else {
    ON_OFF = false;
  }
  ON_OFF = false;
}

void temperaturAlarm(int interval) {
  unsigned long currentMillis = millis();
  if (ON_OFF == true) {
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;

      if (temperaturLedState == LOW) {
        temperaturLedState = HIGH;
        soundLedState = HIGH;
      } else {
        temperaturLedState = LOW;
        soundLedState = LOW;
      }
      digitalWrite(yellowPin, temperaturLedState);
      Serial.println(ON_OFF);
    }
  } else {
    ON_OFF = false;
    Serial.println(ON_OFF);

  }
}

void piezoSound() {
  if (ON_OFF == true) {
    const int noteA = 440;
    const int noteB = 750;
    const int noteC = LOW;
    if (soundLedState == LOW) {
      tone(piezoSensor, noteA);
    } else if (soundLedState == HIGH) {
      tone(piezoSensor, noteB);
    } 
  }
}

void lockSensor() {

  if (ON_OFF == false) {
    if (sensorReading >= threshold) {
      lockLedState = !lockLedState;
      digitalWrite(greenPin, lockLedState);
    } else {
      digitalWrite(redPin, !lockLedState);
      delay(200);
    }
  }
}


//FRITZING
//Send temperatur til processing
    ON_OFF = true;

Does ON_OFF equal true mean on? Or does it mean off? Dumb name.

    while (ON_OFF = true) {
      temperaturAlarm(50);
      piezoSound();
    }

First, assigning the value true to ON_OFF in the while conditional is wrong. You want == there, not =.

Second, I would NOT expect temperatureAlarm() or piezoSound() to change the value of ON_OFF. If they do, I'd give you a failing grade on the project. If they don't, you have an endless loop.

@PaulS: 1. Yes that's the idea. Makes sense to me, so maybe you're just not seeing the idea here. Dont know how to explain that, its really simple.

  1. Yes i see that error, i've actually corrected that already, but it made no difference. Sorry for not updating the code.

  2. I wouldn't either, but i tried doing it, in case it actually worked. Since you you didn't bring any real suggestions to solve the problem, you actually just pointed the obvious by saying that, i dont know how to properly give you further information. I know i have an endless loop - how do i keep that loop going, but turn off the sound when the temperature reaches below 24 degrees. Same question.

And Im glad you're not my teacher on this project, or perhaps not even a teacher at all, because you're really no mentor in this field. A little something to think about during the easter.
Enjoy your weekend.

Actually PaulS tell you EXACTLY what is wrong.
You set ON_OFF=TRUE, immediately after that you enter a while loop with condition ON_OFF==TRUE. You are stuck in an endless loop as ON_OFF will never change inside the loop, just as PaulS told you.

Without looking closer at your code, change 'while' for 'if' might be the solution, at least for this part.

Happy Easter!

    ON_OFF = true;
    while (ON_OFF = true) {
      temperaturAlarm(50);
      piezoSound();
    }

A few more corrections with comments. I also added a noTone() :wink:

As there is ' if (ON_OFF == true) {' in beginning of both temperatureAlarm and piezoSound I would rather call those functions from loop() instead of getTemperature. I find it more easy to follow the code that way, instead of several levels of functions.

//variabler til lås
const int redPin = 13;
const int greenPin = 12;
const int knockSensor = A1;
const int threshold = 100;
int sensorReading = 0;
int lockLedState = HIGH;

//variabler til temperatur
float voltRead;
const int temperaturePin = A4;
const int yellowPin =  11;
int temperaturLedState = HIGH;
unsigned long previousMillis = 0;
int voltInCelc = 0;

//variabler til lyd
int soundLedState = LOW;
const int piezoSensor = A1;
//knockSensor A1, noteA, noteB;

boolean ON_OFF = false;

void setup() {
  Serial.begin(9600);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
}

void loop() {
  sensorReading = analogRead(knockSensor);
  lockSensor();
  getTemperature(temperaturePin);
  delay(500);
}

void getTemperature(int temperaturePin) {
  float voltRead = analogRead(temperaturePin);
  float volt = voltRead * (5.0 / 1023);
  float voltInCelc = (volt - 0.5) * 100;
  Serial.println(voltInCelc);

  Serial.println(ON_OFF);
  if (voltInCelc >= 23 || voltInCelc <= 1) { //Til tests er det 24 - normalt køleskab er måske maks 7 *c.
    ON_OFF = true;
  } else {
    ON_OFF = false;
  }
  temperaturAlarm(50);         //Gabriel -  I would consider moving these two lines to void loop() for less nested functions.
  piezoSound();
}

void temperaturAlarm(int interval) {
  unsigned long currentMillis = millis();
  if (ON_OFF == true) {
    if (currentMillis - previousMillis >= interval) {
      previousMillis = currentMillis;

      if (temperaturLedState == LOW) {
        temperaturLedState = HIGH;
        soundLedState = HIGH;
      } else {
        temperaturLedState = LOW;
        soundLedState = LOW;
      }
      digitalWrite(yellowPin, temperaturLedState);
      Serial.println(ON_OFF);
    }
  } else {
    noTone(piezoSensor);  //Gabriel - Was 'ON_OFF = false', but was already false as you ended up here
    Serial.println(ON_OFF); 

  }
}

void piezoSound() {
  if (ON_OFF == true) {
    const int noteA = 440;
    const int noteB = 750;
    const int noteC = LOW;
    if (soundLedState == LOW) {
      tone(piezoSensor, noteA);
    } else if (soundLedState == HIGH) {
      tone(piezoSensor, noteB);
    }
  }
}

void lockSensor() {

  if (ON_OFF == false) {
    if (sensorReading >= threshold) {
      lockLedState = !lockLedState;
      digitalWrite(greenPin, lockLedState);
    } else {
      digitalWrite(redPin, !lockLedState);
      delay(200);
    }
  }
}


//FRITZING
//Send temperatur til processing

@Gabriel
Thank you for the answer!
As i said, i already changed the = to ==, it was a minor typo from changing it 100 times before :slight_smile: Still didn't work.
BUT
The noTone() function actually did the trick! I dont know why the boolean thing didn't work before, also while having the == error fixed, but it does now.

I can see your point in the loop() method instead of nested functions inside a function. Will keep that in mind for next project.
Thank you again, and happy easter!