Leonardo w/ Amazon LCD not working after switch monotoring

Hello,
First of all this is my first thread in this forum. Thanks for developers for this opportunity.

Unfortunately I have a sever issue in my project and have no idea how to move forward.

I have an Ardurino Leonardo with a 20x4 LCD bought at Amazon and shipped from China. On that display the “hello world” example etc. runs perfect.
So I implemented it to my project. The code below is a cut down version but shows the same issue.

My code runs perfectly fine until I started to implement the void where I read the switch inputs. The void is called „AbfrageZeit()“. The Leonardo seems to get stuck and the Display shows strange things.

And now it’s getting really strange: This void as well results in that my Leonardo is not programmable any more. First I thought I disconnected the USB while programming to quick. But I tested the code with my second Leonardo and had the same effect. Luckily I was able to re-program the second after a while. Don’t know why. But the first is not program able any more. Seems to be the bootloader is damager. But I’m as well not able to programm the bootloader any more.

Now I’m afraid to go ahead because I have no idea what is the issue.

So I hope you can help? Is something wrong to my code or is it the display?

#include <LiquidCrystal.h>
#include <Wire.h>


LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
//----> switches config

int switchPinT1 = 4;  /*Timer gelb*/
int switchPinT2 = 5;  /*Timer rot*/
int switchPinT3 = 3;  /*Timer gruen*/


//----> switch status
int lastStateT[2];
int newStateT[2]; 

//---->
int selectHeat = 5;
int selectGroup = 11;
char* PilotName[] = {"Mustermann", "Beispiel", "Egal"};
char* PilotVorname[] = {"Max", "Moritz", "Franz"};
int indent = 1;
long Timeout = 0;
int idPt[2];
int idP1[2];
int idP2[2];
int idP3[2];
unsigned long currentTime;

void setup() {
  pinMode(switchPinT1, INPUT); 
  pinMode(switchPinT2, INPUT); 
  pinMode(switchPinT3, INPUT); 
  lcd.begin(20, 4);
  DisplayIndentSetup();
  
}

void loop() {
  AbfrageZeit(); // If this void becomes active the Leonardo crashed and was not re progamm able any more
  currentTime = millis();
  DisplayIndent();
  delay(100);
}

void DisplayIndent()
{
  
  int i=0;
  i = indent; 
    if(PilotVorname[i] != "NA"){
      
        if(currentTime -  Timeout < 400)
          {
          lcd.setCursor(12,i+1); 
          lcd.print("T"); lcd.print(" ");
          lcd.print("1"); lcd.print(" ");
          lcd.print("2"); lcd.print(" ");
          lcd.print("3"); lcd.print(" ");
          
          return;}
        if (currentTime -  Timeout > 1500){Timeout = currentTime;}
        
      lcd.setCursor(12,i+1); 
      lcd.print("T"); if(idPt[0] == 1){lcd.print((char)255);} else {lcd.print((char)219);} //eigentlich idP1[i], zu testzwecken idPt[0]
      lcd.print("1"); if(idPt[1] == 1){lcd.print((char)255);} else {lcd.print((char)219);} //eigentlich idP1[i], zu testzwecken idPt[1]
      lcd.print("2"); if(idPt[2] == 1){lcd.print((char)255);} else {lcd.print((char)219);} //eigentlich idP2[i], zu testzwecken idPt[2]
      lcd.print("3"); if(idP3[i] == 1){lcd.print((char)255);} else {lcd.print((char)219);}
     
    }
  
}
void DisplayIndentSetup()
{
  lcd.clear();
  lcd.print("Heat: "); lcd.print(selectHeat, DEC); lcd.print(" Group: "); lcd.print(selectGroup, DEC); 
  for (int i=0;i<3;i++) {
    if(PilotVorname[i] != "NA"){
      lcd.setCursor(0,i+1);
      lcd.print(PilotVorname[i][0]); lcd.print("."); lcd.print(PilotName[i]); 
    }
  }
}

//------------------------------------------------------------------------------
// If this void becomes active the Leonardo crashed and was not re progamm able any more
void AbfrageZeit() {
  newStateT[0] = digitalRead(switchPinT1);
  newStateT[1] = digitalRead(switchPinT2);
  newStateT[2] = digitalRead(switchPinT3);
  for (int i=0;i<3;i++){
    if(newStateT[i]!=lastStateT[i]) 
    {
      if(newStateT[i]==HIGH) {idPt[i] = 1; }
      lastStateT[i] = newStateT[i];
    }
  }
}

Thanks Christoph

Are you unable to reload "Hello World" ?

What are you digital reading on pins 3 to 5?

To reprogram your first Leonardo, you may be able to hold down the reset button on it while compiling and just when the IDE changes from compiling to uploading, release reset. You may need to try a few times to get the timing right.

Hey, thanks for reply!

I tried it with reset 100 times... but doesn't work. As well programming bootloader don't work... but have no expiriance with that.

With the working Leonardo hello world worked.

The read pin 3 to 5 are a standart switch. 5v into the switch. The other end to i/o and pulled down with 1K to GND. In a seperat program w/o display the switch worked fine...

I'm really lost on that one.

Could it be an issue with sampling rate of the display? Do I need a lcd.clear and print everything new each step? Is it a generell problem with this Amazon/China lcd?

EvilKnivel:
The read pin 3 to 5 are a standart switch. 5v into the switch. The other end to d/i and pulled down with 1K to GND and. In a seperat program w/o display it worked fine…

I’m really lost on that one.

Could it be an issue with sampling rate of the display? Or do I need a lcd clear and print everything new each step? Or could it be thats a problem with this Amazon/China lcd?

I have no idea what the above sentences you wrote mean, others on the forum who are familiar with your display may be able to help if you give them more info, diagrams, code, photos, etc. I’m not sure.

Its related to the question of CarlW:

CarlW: What are you digital reading on pins 3 to 5?

In attachment the wiring of my project.

Has nobody an idea what the issue is?

You have E and DB4 both connected to digital #8 pin.

Db4 is connected to 9

My apologies. I am sure that I saw a clear 8 at both arrows. Looking at it this morning, the 9 is quite distinct. Have you updated the JPEG?

Quite honestly, the GPIO pins that you choose for the LCD should make no difference to the bootloader.

Regarding your switches. Active-high is not very wise from an electrical point of view. You normally use active-low switches (with internal pull-ups).

As a last resort. I would re-install the bootloader from the IDE (if you possess a programmer or another Arduino)

David.

But do you think switches active high causes that issue? Maybe its not electrical wise but at least as well shown in Ardurino offical tutorials....

No, I do not think that active-high would be the cause of your problem. It is just unwise ™.

I doubt if active-high is used in any official tutorial for push-button input.
Active-high is certainly the Arduino convention for output.

For output, an AVR is quite capable of active-high or actve-low. It contains push-pull output drivers.
Active-low is marginally preferable for output circuits but is not something that the average Arduino User would understand. Active-high output is far more intuitive.

No, I have no explanation for your problem. I do not own a Leonardo or even a Pro-Micro. The USB bootloader should be perfectly safe. i.e. protected by the Lock Bits. Just like the UNO bootloader is safe.

Providing you start the USB bootloader correctly, it should give trouble-free service.
Note that anyone who has “fiddled” with any Arduino by connecting an ISP programmer has probably upset Fuses and Lock bits. Hence my advice to “re-install Bootloader from within the IDE”

David.

Has nobody an ideas why I get the issue with my Leonardos??? :o

I doubt anybody can figure out what issue you are having. This thread seems to skip around between working on some sort of circuit external to the Leo, and getting any sketch to upload to the Leo. Not sure if you're having problems getting a sketch to do what you want, or having problems with uploading any sketch at all.

First of all I would like to understand if something in my sketch is wrong?

Second is it possible that this sketch or a display failure can be the reason why the leonardos crashing while running that script.

In my point of view it can be something to the display or the sketch. Strang is that w/o the switch the display work. W/o the display the switches work. Both together causing a crash of Leonardo...

My apologies. Your earlier posts implied that your first Leonardo was completely inoperable. i.e. you could not upload any "good" sketches.

So most readers were wondering "how" you could ruin the Bootloader. In practice, a bootloader is 100% safe until someone plugs in an ISP programmer.

Anyway, I had made no attempt to look at your sketch. After all, it may not work but it cannot harm the Bootloader.

You have several "mishaps":

int lastStateT[2];
int newStateT[2];
...
int idPt[2];
int idP1[2];
int idP2[2];
int idP3[2];
...
    if(PilotVorname[i] != "NA"){

Arrays in C and C++ start at 0. So if you have a three-element array like idPt[] you should declare it as such. i.e. members idPt[0], idPt[1], idPt[2] is three elements.

C++ gives you no warning when you exceed the Array bounds. It just corrupts the next area of memory.

I am surprised that the comparison did not give a Warning. You are comparing a char* with a char* but I don't think Arduino has overloaded the comparison operator for char*. You are just comparing the addresses in memory, Not the strings that they point to.

There may be "other" problems. As suggested earlier, active-high buttons are unwise (tm).

David.