0

My goal with the code is simple. Let the Piezo buzz when temperature reaches 25 degrees. My piezo is hooked up correctly, because It makes a buzz when the arduino boots or when I put this line of code anywhere else in my code.

I also checked if the If statement was called. And yes it is! So the if statements is happening yet I don't hear a buzz.

I am aware that I am breaking my seven segmented screen by using the delay. But I don't mind the screen working when the buzzer is.

void loop()
{
  //local vars
  if (millis() % 5000 <= 10) {
      float lm35TempC = lm35.getTempC();
      sevseg.setNumber(lm35TempC, 2);
      if (lm35TempC > 25.00) {
        tone(1,500,1000);
        delay(3000);  <------- WHERE IT PROBABLY GOES WRONG
      }
      if (lm35TempC < 25.00) {
        noTone(1);
      }
  }
  sevseg.refreshDisplay(); // Must run repeatedly; don't use blocking code (ex: delay()) in the loop() function or this won't work right
}

To conclude: The If-statement is working yet I don't hear a buzz. How does this happen?

Thanks in Advance!

3
  • The windows of millis() % 5000 <= 10 is only 10ms every 5 second. How long does the function sevseg.refreshDisplay take? more than 10ms?
    – Jot
    Commented Aug 28, 2018 at 0:52
  • No I dont think that function will take much longer, but I have to admit its a ugly way of creating a delay of 5 seconds, but if I for example increase 10 to 50, my display will sometimes refresh twice within a short timeframe Commented Aug 28, 2018 at 0:55
  • Then stop doing it in a ugly way. Use arduino.cc/en/Tutorial/BlinkWithoutDelay with an interval of 5 seconds. After that you have to fix the tone, I think that is ugly as well. A tone with duration will delay and does not need noTone. There might be hardware problems as well. Fix everything one by one.
    – Jot
    Commented Aug 28, 2018 at 1:19

1 Answer 1

4

You connected the piezo to pin 1? Chose other pin. Pin 1 is Serial TX. You 'pump' PWM to USB.

tone() function will make sound no matter if you have a delay there. You set 1 sec with 1000 as third parameter. It will sound 1 sec and the turn of. You do not need the delay() and noTone().

500 is low frequency. If it sounds only one second, can you catch it?

try tone(TONE_PIN, 4500, 500). with speaker it is a nice beeeep

1
  • You were right! The only reason I used pin 1 was, that I allready used all the other Digital Pins (expect 0 but that one is like 1). I have now configured an Analog Pin as output and it works Perfectly! Commented Aug 28, 2018 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.