I have been trying to write a program, experimenting, and it seems that it has the ability to kill an Arduino Leonardo.
If I remove the FOR LOOP then it remains accessible to Arduino software and is recognised in the device manager, but if I enable the FOR LOOP as it is in this code, then after successful programming, the Arduino Leonardo is no longer recognised, and the only way to save it, is to re-program it via ICSP and another Arduino.
What is the FOR LOOP doing that is so bad? How is it possible that it stops the device from being recognised by PC?
/*
Created by ArduinoGetStarted.com
This example code is in the public domain
Tutorial page: https://arduinogetstarted.com/library/arduino-melody-example
Library References: https://arduinogetstarted.com/tutorials/arduino-buzzer-library
This example uses a piezo buzzer:
+ plays a melody once on background each time a button is pressed
+ stops playing a melody when another button is pressed
+ without using delay() function, this is a non-blocking example
*/
#include <ezBuzzer.h> // ezBuzzer library
const int START_BUTTON_PIN = 7;
const int STOP_BUTTON_PIN = 8;
const int BUZZER_PIN = 3;
int nts[]={31,33,35,37,39,41,44,46,49,52,55,58,62,65,69,73,
78,82,87,93,98,104,110,117,123,131,139,147,156,165,175,185,
196,208,220,233,247,262,277,294,311,330,349,370,392,415,440,466,
494,523,554,587,622,659,698,740,784,831,880,932,988,1047,1109,1175,
1245,1319,1397,1480,1568,1661,1760,1865,1976,2093,2217,2349,2489,2637,2794,2960,
3136,3322,3520,3729,3951,4186,4435,4699,4978};
int nt2[]={56,13,25,54,13,13,53,25,49,13,25,53,13,13,56,25,54,17,53,29,48,
17,29,29,17,17,29,29,17,17,29,29,58,10,22,60,10,10,61,22,53,10,22,54,10,10,56,22,
54,18,53,30,49,18,30,30,18,18,30,30,18,18,30,30,56,13,25,54,13,13,53,25,49,13,25,
53,13,13,56,25,54,17,53,29,48,17,29,29,17,17,29,29,17,17,29,29,58,22,34,60,22,22,
61,34,53,22,34,53,22,22,56,34,58,18,56,30,56,18,30,30,18,18,30,30,18,18,30,30,49,
13,53,25,56,13,49,53,13,56,25,49,53,13,56,25,48,17,53,29,56,17,48,53,17,56,29,48,
53,17,56,29,49,22,53,34,58,22,49,53,22,58,34,49,53,22,58,34,49,18,54,30,58,18,49,
54,18,58,30,49,54,18,58,30,49,13,53,25,56,13,49,53,13,56,25,49,53,13,56,25,48,17,
53,29,56,17,48,53,17,56,29,48,53,17,56,29,49,22,53,34,58,22,49,53,22,58,34,49,53,
22,58,34,49,18,54,30,58,18,49,54,18,58,30,49,54,18,58,30};
int lastStartButtonState = HIGH; // the previous state from the input pin
int lastStopButtonState = HIGH; // the previous state from the input pin
ezBuzzer buzzer(BUZZER_PIN); // create ezBuzzer object that attach to a pin;
// notes in the melody:
int melody[] = {
};
void setup() {
Serial.begin(9600);
pinMode(START_BUTTON_PIN, INPUT_PULLUP);
pinMode(STOP_BUTTON_PIN, INPUT_PULLUP);
// GUILTY FOR LOOP ON NEXT LINES
for (int i = 0; i < 256; i++) {
melody[i] = nts[nt2[i]];
Serial.println(melody[i]);
}
}
void loop() {
buzzer.loop(); // MUST call the buzzer.loop() function in loop()
int startButtonState = digitalRead(START_BUTTON_PIN);
int stopButtonState = digitalRead(STOP_BUTTON_PIN);
if (lastStartButtonState == HIGH && startButtonState == LOW) {
Serial.println("The START button is pressed");
if (buzzer.getState() == BUZZER_IDLE) {
int length = 256 ;
buzzer.playMelody(melody, 10, length); // playing
}
}
if (lastStopButtonState == HIGH && stopButtonState == LOW) {
Serial.println("The STOP button is pressed");
if (buzzer.getState() != BUZZER_IDLE) {
buzzer.stop() ; // stop
}
}
lastStartButtonState = startButtonState;
lastStopButtonState = stopButtonState;
}