ESP32 C3 Tone usage issue

I am using ESP32 C3. When I place 'noTone' in position 1 and execute playMusicPro1() for the second time, there is no signal generated for BUZZER-PIN. When I place 'noTone' in position 2 and execute playMusicPro1() for the seventh time, there is no signal generated for BUZZER-PIN. Changing to a different BUZZER-PIN is also not possible.
ESP32 library version: 3.03
ESP32 C3 SuperMini

int BUZZER_PIN = 3;
pinMode(BUZZER_PIN,  OUTPUT);
digitalWrite(BUZZER_PIN, HIGH);

int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
int noteDurations0[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};
void playMusicPro1() {
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000 / noteDurations0[thisNote];
    tone(BUZZER_PIN, melody[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);    
    //noTone(BUZZER_PIN);   1
  }
  noTone(BUZZER_PIN);//  2
}

If using AnalogWrite, there will be no signal generated from BUZZER-PIN during the 7th execution。

Are you using Arduino IDE? This usually requires setup() and loop() functions as a minimum... and putting your code inside those functions... and it looks like you need "pitches.h" or an equivalent.

This seems to work for me... (on a Nano)... with the sixth note a "rest"

#include <pitches.h>
int BUZZER_PIN = 3;

#define NOTE_REST 0
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, NOTE_REST, NOTE_B3, NOTE_C4
};

int noteDurations0[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
  pinMode(BUZZER_PIN,  OUTPUT);
  digitalWrite(BUZZER_PIN, HIGH);
}

void loop() {
  playMusicPro1();
}

void playMusicPro1() {
  for (int thisNote = 0; thisNote < 8; thisNote++) {
    int noteDuration = 1000 / noteDurations0[thisNote];
    tone(BUZZER_PIN, melody[thisNote], noteDuration);
    int pauseBetweenNotes = noteDuration * 1.50;
    delay(pauseBetweenNotes);
  }
  noTone(BUZZER_PIN);//  2
}

How do you think that will go? Remember that 1.3 is not an integer but the line will be executed as if t were.

why not try:-

int pauseBetweenNotes = (float)noteDuration * 1.30;

The whole of the code is required as requested.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.