Hello I m using arduino Uno. One LCD of 16x2, 2 push buttons and LDR is connected to in. My test code increments number and prints on LCD when button is pressed. and every 5 second program reads LDR value using analogRead function. however what happens is first 5 seconds before LDR measurements, Button presses are detected as expected. but after exiting the section of code where i do AnalogRead, buttons presses are not detected anymore. can anyone point out why it happens?
Here is my test code :
#include "LiquidCrystal.h"
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
lcd.begin(16,2);
/* Initialized GPIO for push buttons */
pinMode(11,INPUT_PULLUP);
pinMode(12,INPUT_PULLUP);
}
int v = 0;
uint32_t cmillis, tflag0 = 0;
void loop() {
/* For test when button is pressed,
Increment Variable v and print it to LCD
*/
if(!digitalRead(11)){
lcd.setCursor(0,0);
lcd.print(v);
v++;
}
/* every 5 seconds read LDR value and adjust LCD Backlight */
cmillis = millis();
if((cmillis - tflag0)>5000){
tflag0 = millis();
int adc_raw = analogRead(A0);
int led_val = map(adc_raw,0,800,255,0);
analogWrite(10,led_val);
}
}
There is current limiting resistor on LCD Module so i think it should not cause any issue. However just to be sure, I disconnected the Backlight from arduino pin and ran code again but still same issue occurs. so it confirms issues is within code not connections.
I'd put a couple of Serial.println() statements in... to see what's happening.
Any key press is going to generate a lot of writes to the LCD... one per loop().. way faster than you can get your finger off the button. Maybe it's just flooding the LCD so you see nothing?
Okay finally found the issue. the problem was indeed with connections. I was connecting ground of Pushbuttons to AREF next to GND of UNO board all time. I should've checked connection more carefully lol. anyways, Thanks for all helpful responses.