The setup:
Arduino Mega, LCD 2x16 (checked with 4x20), 4 buttons in pull-down configuration (100K Ohms to GND) in addition each one using a diode provides HS to interrupt, USB powering. There is common GND for buttons and LCD.
LCD R/W pin is grounded.
Problem:
I believe it is only a hardware problem. When I press any button, not always, LCD starts to flicker and shows garbage. I know that the uC still works because (it operates LEDs and other things). The board has to be reset.
Other observations:
I know that USB power is somehow stable. When I power only from external adapter +12V it is even more unstable - I can see that displayed characters are 'waving' more or less 50Hz. Quite similar when I power from +12 from computer power supply (this is stable for sure).
When I swap to bigger LCD 4x40 it is more sensitive and goes crazy much faster and really doesn't work with external +12 (powered via arduino board).
With smaller LCD and USB power only, usually when I press a button and hold it for a few seconds it doesn't cause any problems. Seems that problem occurs only if press-release a few times.
The code for buttons...
void ButtonCall() {
noInterrupts();
if (abs(millis() - last_btn) > 200) {
if (screensaver) {
lightPWM = ScreenConfig.lightPWMhigh;
analogWrite(backLight, lightPWM);
}
else if (digitalRead(btn_min)) {
screen_pnt->btn1();
}
else if(digitalRead(btn_pls)) {
screen_pnt->btn2();
}
else if(digitalRead(btn_ent)) {
screen_pnt->btn3();
}
else if(digitalRead(btn_esc)) {
screen_pnt->btn4();
}
last_btn = millis();
screensaver = false;
lcd.clear();
}
interrupts();
}
attachInterrupt(0, ButtonCall, HIGH);
To be safe for debounce I have not only 200ms interval but also deactivate interrupts.
So... any suggestions?