Program kills Arduino Leonardo

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;
}

Hi, @matalog

What do you mean by "kill"?
Damage the controller so it is no longer possible to program?
Stop the code, but allows reprogramming?

Tom.. :grinning: :+1: :coffee: :australia:

It does not allow the Leonardo to be reprogrammed by the Standard method in Arduino software. It is no longer recognised as a USB device by the PC and not seen as connected on any Port in Arduino software.

The only way to get it to be recognised as an Arduino again, is to use the ICSP pins to reporgram it using that special method when connected to a second Arduino that is connected to PC, where the second Arduino reprograms the Bootloader in the 'problem' Arduino.

I think the problem is

int melody[] = {};

which should be

int melody[256];
1 Like

It fixes the problem, but I don't understand why it would render the Arduino unconnectable!

Because the memory is corrupted, the program crashes and only God knows what the micro does from there, what seems clear is that it does not execute the bootloader. :wink:

1 Like

The Leonardo has native USB (the USB port hardware is part of the same processor that your sketch is running on, the atmega32u4). When melody was declared incorrectly, you were overwriting memory that was being used for other purposes, in this case something that resulted in the USB code ceasing to function properly. Without the USB code running, the processor could not properly detect that you were attempting to program the Leonardo, so it never ran the bootloader.

Incidentally, double pressing the RESET button should cause the processor to enter the bootloader and stay there until you can upload new code, without having to use an ISP programmer.

2 Likes

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