Problem with tone() within if()

Good day everyone,

I have this code and was wondering if any of you can tell me why my tone(12,450,500); does not execute within the if() statement BUT only in the void setup() .... ?

int led = 13;
int incomingByte = 0;

void setup() {
    pinMode(led, OUTPUT);
    Serial.begin(9600);   
//tone(12,450,500);
}

void loop() {
     
    if (Serial.available() > 0) {
        incomingByte = Serial.read();

        if (incomingByte == 48)

        {
                        tone(12,450,500);
                    digitalWrite(led, HIGH);
                    delay(500);
                        digitalWrite(led, LOW);
        }
    }
}
...
if (incomingByte == '0')
...

What are you sending?

I am Reading a byte sent from my PC over the serial port and then I send a signal to pin 13 ...

What are you sending from the PC?

When a test such as

if (incomingByte == 48)

does not work as expected then it is a good idea to print what you are testing to see if its value is what you think it should be.

Its a zero …

Not 48 then...

Mind you, 48 is the decimal ASCII value for zero.

Precisely ....

I am Reading a byte sent from my PC over the serial port

Its a zero

Not 48 then..

Precisely ....

What application is sending from the PC?

Certainly when I run your code from the IDE and the associated serial monitor I can see 48 for the entered 0, and can confirm both the led and the tone.

Arduino. I am running the code in the Arduino IDE … Can I just make sure I understand correctly cattledog, does tone work on your Arduino (The tone() within the IF function)

You realize that the serial monitor doesn’t send anything until you press , right?

I should at the very least hear a tone from the speaker when I run the code via Arduino IDE .... even before I program any operating system commands or any external commands then surely I should be able to hear the speaker ?!

The_Crow: I should at the very least hear a tone from the speaker when I run the code via Arduino IDE .... even before I program any operating system commands or any external commands then surely I should be able to hear the speaker ?!

What code are you referring to ? The code in your original post has no tone() command that does not depend on suitable serial input being received. Does the LED turn on/off even though you don't hear the tone ?

If you have tried some test code then please post it and describe how the speaker is wired.

Can I just make sure I understand correctly cattledog, does tone work on your Arduino (The tone() within the IF function)

Yes. I confirmed it by running the output of the tone pin to an interrupt pin and counting the cycles from the tone output.

I did not use a speaker or buzzer. I would check out the wiring and your "speaker".

Here is the test code I ran. Jumper pin 12 to pin 2. Open the serial monitor, choose "no line ending" enter 0 and click the send button(or enter on your keyboard) . The led blinks and you will see a count of 225 on the monitor.

int led = 13;
int incomingByte = 0;
volatile int count;

void countPulses()
{
  count++;
}

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
  attachInterrupt(0, countPulses, RISING);
  //tone(12,450,500);

}

void loop() {

  if (Serial.available() > 0) {
    incomingByte = Serial.read();

    if (incomingByte == 48)

    {
      tone(12, 450, 500);
      digitalWrite(led, HIGH);
      delay(500);
      digitalWrite(led, LOW);
      
      noInterrupts();
      int readCount = count;
      count = 0;
      interrupts();

      Serial.println(readCount);

    }
  }
}