Leonardo does not connect with PC - cannot upload new sketch

I was trying to integrate some native C code with my arduino project and after a few uploads my Arduino Leonardo stopped working.

After connecting USB cable, boards starts faulty sketch and dies. Pressing reset button does not help, there is no open COM port on PC so a cant select any in Arduino Studio to upload good sketch.

There was always specific sound in windows on usb device connection - now nothing, like board is not communicating with PC in any way.

Is it possible to somehow repair it? Or it is now worthless piece of junk? :sob:

After connecting USB cable, boards starts faulty sketch and dies.

Dies? what do you mean ? Can you show what you uploaded to the leonardo ?

Keep the reset button pressed and connect the board to the PC, leave the reset button and try.

MalharD: Dies? what do you mean ?

Nothing happens, leds are not blinking, not reacting to potentiometer changes, just nothing happens.

MalharD: Can you show what you uploaded to the leonardo ?

This: http://pastebin.com/eqEQMwzW

MalharD: Keep the reset button pressed and connect the board to the PC, leave the reset button and try.

I've tried this but it wont work, as virtual COM of leonardo is not appearing so i cant select it in Studio to upload sketch...

Use the code tags </> to post the code.
Do you have another board ?

#include <avr/interrupt.h>
#include <avr/pgmspace.h> 
#include "audio.h"

struct fadeLED {
  int pin;
  int brit;
  int fade; 
};

const int ledCount = 4;
const int ledHigh = 200; 
const int fadeStep = 10;

fadeLED leds[ledCount];

unsigned long pMilis = 0;
long interval = 20;

int i;

int curLed = 0;

volatile uint16_t pos;


void start_timer(){
    TCCR0A = (1 << WGM01); 
    OCR0A = ((F_CPU / 8 / SAMPLE_RATE)-1); 
    TIMSK0 |= (1 << OCIE0A);
    TCCR0B = (1 << CS01); 
}

void stop_timer(){
    cli(); 
    TIMSK0 &=~ (1 << OCIE0A); 
    TCNT0 = 0; 
}

void start_pwm(){
    TCCR1A = (1 << COM1A1) | (1 << WGM10);
    TCCR1B = (1 << WGM12) | (1 << CS10);
    OCR1A = 128;
}

void setup() {
  fadeLED tled = {3, 0, 5};
  leds[0] = tled;
  tled = {5, ledHigh, fadeStep};
  leds[1] = tled;
  tled = {6, ledHigh, fadeStep};
  leds[2] = tled;
  tled = {10, ledHigh, fadeStep};
  leds[3] = tled;

  for(i=0; i < ledCount; i++) {
    pinMode(leds[i].pin, OUTPUT);
    analogWrite(leds[i].pin, leds[i].brit);
  }

  DDRB = (1 << PB1);
  DDRB |= (1 << PB5);

  start_pwm();
  delay(500);
  pos = (START_POS + 0);
  sei();
  start_timer();
}

void loop() {
  if (pos == AUDIO_SIZE) {
      stop_timer(); 
      pos = (START_POS + 0);
      PORTB |= (1 << PB5);
  }
  interval = round(analogRead(0)/10);
  unsigned long curMilis = millis();
  if(curMilis - pMilis >= interval) {
    pMilis = curMilis;
    int nextLed = curLed + 1;
    if(nextLed >= ledCount) {
      nextLed = 0;
    }
    bool shift = false;
    bool setNext = false;
    leds[curLed].brit += leds[curLed].fade;
    if(leds[curLed].brit <= 0 || leds[curLed].brit >= ledHigh) {
      if(leds[curLed].brit > ledHigh) leds[curLed].brit = ledHigh;
      if(leds[curLed].brit < 0) leds[curLed].brit = 0;
      if(leds[curLed].brit >= ledHigh) {
        shift = true;
      }
      leds[curLed].fade = -leds[curLed].fade;
      if(leds[curLed].fade > 0) {
        leds[nextLed].fade = -fadeStep;
        setNext = true;
      }
    }
    if(setNext) {
      leds[nextLed].brit += leds[nextLed].fade;
      if(leds[nextLed].brit > ledHigh) {
        leds[nextLed].brit = ledHigh;
      }
      if(leds[nextLed].brit < 0) {
        leds[nextLed].brit = 0;
      }
    }
    for(i=0; i < ledCount; i++) {
      if(i == curLed || i == nextLed) {
        analogWrite(leds[i].pin, leds[i].brit);
      }
    }
    if(shift) {
      curLed++;
      if(curLed >= ledCount) {
        curLed = 0;
      }
    }
  }
}

ISR(TIMER0_COMPA_vect)
{
    OCR1A = pgm_read_byte(&AUDIO_DATA[pos]); 
    if (pos < AUDIO_SIZE) {
      pos++;
    }
}

Leonardo boards are sensitive to sketches that malfunction. If you have a sketch that crashes, the USB port no longer works, so it is difficult to upload a new sketch. This is different compared to Uno where the USB function is offloaded to a separate chip and is therefore always available.

If pressing the reset button does not run the bootloader and present a USB port that you can use, the next easiest way to recover the board to working order is to burn a fresh bootloader on it. Use another Arduino as ISP, or use an ISP device such as USBasp, to burn a fresh bootloader on the Leonardo using the IDE’s Burn Bootloader function.

I recommend installing the arduino.cc Arduino IDE, and use it when troubleshooting with members of this forum, who are more likely to be familiar with arduino.cc software. Do not use the arduino.org Arduino Studio, which is in alpha development.

MalharD:
Do you have another board ?

No. I have USB<->UART converter which i successfully used to upload sketch to standalone atmega328p.

I tried in same way with leonardo but did not work.

dmjlambert:
If pressing the reset button does not run the bootloader and present a USB port that you can use, the next easiest way to recover the board to working order is to burn a fresh bootloader on it. Use another Arduino as ISP, or use an ISP device such as USBasp, to burn a fresh bootloader on the Leonardo using the IDE’s Burn Bootloader function.

Bootloader works in my opinion, after reset, L led flashes for 6 or 8 seconds - like waiting for connection. Only usb is not working so i cant upload anything.

When you burn a fresh bootloader onto the Leonardo, it replaces the bootloader and at the same time also loads a fresh and properly working blink sketch that presents a USB port. At that point you should be recovered.

Connect the USBtoUART to a 328p, load the arduinoISP on it,use the 328p like arduino as ISP and burn the bootloader to leonardo.

MalharD: Connect the USBtoUART to a 328p, load the arduinoISP on it,use the 328p like arduino as ISP and burn the bootloader to leonardo.

Im not enough experienced to do it from your description, and google gives me only how to program standalone atmega with arduino..

What Arduino boards, chips, FTDI adapters, programmers, and other stuff do you have on hand? Perhaps you could upload a picture of your collection. Maybe we can figure this out.

ndd12:
I have USB<->UART converter which i successfully used to upload sketch to standalone atmega328p.

I should have read more carefully earlier.
Do you have a 16MHz crystal on the standalone ATmega328P, and does it have the Uno bootloader on it, and can you upload various sketches to it using the Uno board selection in the Arduino app?
If so, you actually have an Uno, and we may be able to help you use your Uno as an ISP programmer and Burn Bootloader to the Leonardo.

dmjlambert: I should have read more carefully earlier. Do you have a 16MHz crystal on the standalone ATmega328P, and does it have the Uno bootloader on it, and can you upload various sketches to it using the Uno board selection in the Arduino app? If so, you actually have an Uno, and we may be able to help you use your Uno as an ISP programmer and Burn Bootloader to the Leonardo.

That's what I ment, however my hands were tired after whole day and night of typing code.

dmjlambert: Do you have a 16MHz crystal on the standalone ATmega328P

Yes

dmjlambert: and does it have the Uno bootloader on it

Yes

dmjlambert: and can you upload various sketches to it using the Uno board selection in the Arduino app?

Yes

I don't immediately see anything in that code that should brick the arduino. My first step would be to reboot and unplug and replug in the arduino to try to get the bootloader com port to show up in device manager. But if you already worked at that for a while, then yeah, burning a new bootloader may be necessary

ndd12:
Yes
Yes
Yes

Ok, you have an Uno there. Use it as an ISP programmer to burn a fresh bootloader on the Leonardo.

Here is the Arduino pin number mapping for the chip/Uno.

Select the Uno board and port in the IDE. Upload the example sketch ArduinoISP onto the Uno.
Then disconnect the FTDI DTR to 0.1uF cap to ATmega328P RST pin wire to prevent further auto resets.
On the tools menu select the programer “Arduino As ISP”

Connect between the Uno and Leonardo as follows:
Uno Leo
SCK SCK
MOSI MOSI
MISO MISO
SS RST
VCC VCC
GND GND

Those pins are available on the ICSP header on the Leonardo on the far right side of the board, which is mapped like this:

Hookup (diagram does not show FTDI, crystal, capacitors, and any other support circuitry you have):
pic.png

In the IDE preferences checkmark the show verbose output during upload
On the Tools menu select the board Leonardo
On the Tools menu select Burn Bootloader

The IDE should display at the bottom Done Burning Bootloader after a minute or 2. If you get an error, the verbose output it shows you may be useful to see.

Sorry for long time without a reply, but i just had no time to try it out.

I connected leonardo do atmega with your instructions, uploaded ArduinoISP sketch to atmega (works, heartbeat led blinks slowly), set board as leonardo, serial port (COM5, no other option, but worked with sketch upload) and programmer as ArduinoISP. After clicking Burn Bootloader i got this error:

avrdude: Error: Could not find USBtiny device (0x2341/0x49)

avrdude done.  Thank you.

the selected serial port 
 does not exist or your board is not connected

I double checked all connections, all seems to be ok.

dmjlambert: On the tools menu select the programer "Arduino As ISP"