Avrdude repeated errors with leonardo

Hi,
I have written a sketch for the leonardo, I have been uploading new versions with no issue.

Today the sketch would not upload stating that the avrdude programmer is not responding.

I have searched the internet forums and tried all the basic tips for solving this. New usb cable. disconnecting pins 0 and 1. hitting reset during boot. reinstalling the IDE and driver. All to no avail. I have tried rewriting the bootloader using a mega which solves the problem and allows me to upload the sketch to the arduino but only for 1 time, then I am back to the programmer not responding error. If I repeat the bootloader install it works again, but only for 1 time.

Can anyone give me advice on a permanent solution?

Thanks

You have to hold the button down, then click 'Upload' and continue holding the button down until Uploading is displayed - where you immediately release the Reset button.

image

Sorry, I had tried this too, it never even shows as uploading, I get a board not found error

Is a new USB device is appeared when you connect the board to the PC?

No, but the board shows up as a joystick and button presses show up in the usb control panel so the pc can 'see' the board

It seems the mouse is busy then, which can crap things up.

Arduino Leonardo Mouse Sketch Reprograming - Using Arduino / Installation & Troubleshooting - Arduino Forum

What if you hold the Reset button down even before you plug it in?
Or, if you have to re-do the bootloader, again, take a look at implementing a plan to keep the mouse quiet (like if an input is LOW, no mousing).
Just "brainstorming".

Sorry, this might be a stupid question.. But by 'no mousing' do you mean stopping the sketch from sending any x/y/z axis information to the pc?

Yes - that is correct.

Ok thank you, I'll give that a try and report back in case it helps others if it works.

It could be an INPUT check in setup(), a DO_WHILE loop where it stays waiting for that INPUT (it's an INPUT_PULLUP) to be HIGH.
You have this INPUT with a jumper to GND, cycle the power and it stays there till the jumper is removed, remove the jumper and life goes on.
Just 'brainstorming'.
But then - if the mouse x,y,z would only occur when this INPUT is 'true' (high, low, however you see it) - could be a better deal.

Thanks, I'll give it a go tomorrow :crossed_fingers:

Hi,

I just tried it, programmed in an interrupt while a button was pulled low, the interrupt worked as shown in the serial monitor

But the result was the same. I could only upload the code once then I'm back to the error message.

Is the error with my code, the arduino board or windows? Does anyone know what causes this?

I've ordered a new board in the hopes that might cure it, any other ideas/suggestions welcome

Try tapping reset twice then upload the code, that should hold it in boot load mode long enough to be able to upload.

Maybe post that ?

#include <Joystick.h>
#include <MPU6050_light.h>
#include <EEPROM.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

Joystick_ Joystick;
MPU6050 mpu(Wire);

int zAxis_ = 0; 
int xAxis_ = 0;                    
int yAxis_ = 0; 
int AcX, AcY, AcZ;
int i = 0;      

void setup()
{
  Joystick.begin();
  Serial.begin(9600);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);

    pinMode(A5, INPUT_PULLUP);

  Wire.begin();
  
  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(status);
  while(status!=0){ } // stop everything if could not connect to MPU6050
  
  Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(1000);
  mpu.calcOffsets(true,true); // gyro and accelero
  Serial.println("Done!\n");

    // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }
    delay(2000);         // wait for initializing
  oled.clearDisplay(); // clear display
  
  }
   
void loop(){
  readbuttons();
  readplunger();
  readaccelerometer();
  updatedisplay();


 if ((!digitalRead(12) == HIGH) && (!digitalRead(13) == HIGH)){calibrate();} // joystick buttons 11 and 12 trigger calibrate

    Serial.print(F("ACCELERO  X: "));Serial.print(xAxis_);
    Serial.print("\tY: ");Serial.print(yAxis_);
    Serial.print("\tY: ");Serial.print(analogRead(A0));Serial.print(" : ");Serial.println(analogRead(A1));
    Serial.println(F("=====================================================\n"));   

    
 
delay (50);

}

void readbuttons()
{
 for (int i=0; i<2; i++) Joystick.setButton(i, !digitalRead(i));
 for (int i=4; i<14; i++) Joystick.setButton((i - 2), !digitalRead(i));
 while (!digitalRead(A5) == HIGH){
     oled.setTextSize(1);          // text size
  oled.setTextColor(WHITE);     // text color
  oled.setCursor(0, 10);        // position to display
  oled.print(("INTERUPT"));
  oled.display(); 
  oled.clearDisplay();              // show on OLED
 }
}

void readplunger()
{
zAxis_ = analogRead(A0);  
zAxis_ = map(zAxis_,0,1023,0,255);
Joystick.setZAxis(zAxis_);  
}

void readaccelerometer()
{
 mpu.update();
     AcX = (mpu.getAccX() * 100);
     AcY = (mpu.getAccY() * 100);

 xAxis_ = map(AcX,-50,50,0,255);
 Joystick.setXAxis(xAxis_);   

 yAxis_ = map(AcY,-50,50,0,255);
 Joystick.setYAxis(yAxis_); 
}

void updatedisplay()
{
  oled.setTextSize(1);          // text size
  oled.setTextColor(WHITE);     // text color
  oled.setCursor(0, 10);        // position to display
  oled.print(("X: "));oled.println(xAxis_);oled.print(("Y: "));oled.println(yAxis_);oled.print(("Z: "));oled.println(zAxis_);
  oled.display(); 
  oled.clearDisplay();              // show on OLED
}

void calibrate()
{

}

Add this in declarations (etc.) --

const byte stallJumper = A5;  // I used A5, you can use what you decide
byte Removed;

and then

void setup()
{
//  ↓  ↓  ↓  My Additions  ↓  ↓  ↓
  pinMode(stallJumper, INPUT_PULLUP);

  do
  {  
    Removed = digitalRead(stallJumper);
  }while (!Removed);
//   ↑  ↑  ↑   End of MyAdditions   ↑  ↑  ↑ 

  Joystick.begin();    // everything the same from hereon

I'm not sure about this --

byte status = mpu.begin();

but. . . anyway.

If the jumper is in place (meaning A5 connected to GND), then it should "freeze" there when power is removed and re-connected ("cycled").
Is that how you did it (previously)?

I did it differently... I'll try adding in your code tomorrow and report back...

Thank you for the assistance, I do appreciate it

ok,
Thorugh much trial and error I have found that the avrdude is beig caused by my code somehow.

It is relatedf to updating the oled display. If I comment out the update display custom function the error goes away.
If I increase the delay in the main loop from 50 to 500 also the error goes away.

So I think the oled display is trying to update too frequently and causing the com error...has anyone heard of this before?

Here is the updated code that bypasses the error - I have added a 1000ms delay in updating the display only.

#include <Joystick.h>
#include <MPU6050_light.h>
#include <EEPROM.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

Joystick_ Joystick;
MPU6050 mpu(Wire);

int zAxis_ = 0; 
int xAxis_ = 0;                    
int yAxis_ = 0; 
int AcX, AcY, AcZ;
int i = 0;
long previousTime = 0;
long displayInterval = 1000;     

void setup()
{
  Joystick.begin();
  Serial.begin(9600);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);

  Wire.begin();
  
  byte status = mpu.begin();
  Serial.print(F("MPU6050 status: "));
  Serial.println(status);
  while(status!=0){ } // stop everything if could not connect to MPU6050
  
  Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(1000);
  mpu.calcOffsets(true,true); // gyro and accelero
  Serial.println("Done!\n");

    // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }
    delay(2000);         // wait for initializing
  oled.clearDisplay(); // clear display
  
  }
   
void loop(){
  readbuttons();
  readplunger();
  readaccelerometer();
 unsigned long currentTime = millis();
 if(currentTime - previousTime > displayInterval) {
   previousTime = currentTime;
  updatedisplay();
 }


 if ((!digitalRead(12) == HIGH) && (!digitalRead(13) == HIGH)){calibrate();} // joystick buttons 11 and 12 trigger calibrate

    Serial.print(F("ACCELERO  X: "));Serial.print(xAxis_);
    Serial.print("\tY: ");Serial.print(yAxis_);
    Serial.print("\tY: ");Serial.print(analogRead(A0));Serial.print(" : ");Serial.println(analogRead(A1));
    Serial.println(F("=====================================================\n"));   

    
 
delay (50);

}

void readbuttons()
{
 for (int i=0; i<2; i++) Joystick.setButton(i, !digitalRead(i));
 for (int i=4; i<14; i++) Joystick.setButton((i - 2), !digitalRead(i));

}

void readplunger()
{
zAxis_ = analogRead(A0);  
zAxis_ = map(zAxis_,0,1023,0,255);
Joystick.setZAxis(zAxis_);  
}

void readaccelerometer()
{
 mpu.update();
     AcX = (mpu.getAccX() * 100);
     AcY = (mpu.getAccY() * 100);

 xAxis_ = map(AcX,-50,50,0,255);
 Joystick.setXAxis(xAxis_);   

 yAxis_ = map(AcY,-50,50,0,255);
 Joystick.setYAxis(yAxis_); 
}

void updatedisplay()
{
  oled.clearDisplay();    
  oled.setTextSize(1);          // text size
  oled.setTextColor(WHITE);     // text color
  oled.setCursor(0, 10);        // position to display
  oled.print(("X: "));oled.println(xAxis_);oled.print(("Y: "));oled.println(yAxis_);oled.print(("Z: "));oled.println(zAxis_);
  oled.display(); // show on OLED
}

void calibrate()
{

}

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