I have this lcd shield with buttons, the part of reading data and showing it on the screen is ok, I have 2 problems, to update the values of the variables I have to click, I want it to update continuously with a click of the button, and according to the LDR reader it shows very unstable numbers, I'm using a 3-pin LDR with DIGITAL input, could you help me with that part?
You need to get rid of your while() loops. Just let loop() repeat. Every time through loop, read your sensors and read your buttons. Then, depending on the buttons, update your display with the correct information.
#include <LiquidCrystal.h>
LiquidCrystal lcd (8, 9, 4, 5, 6, 7);
#define pinSensorUV A5
#define ldr A3
char s [80];
int currentMode = 1;
void setup () {
Serial.begin (9600);
lcd.begin (16, 2);
lcd.setCursor (0, 0);
lcd.print ("ESCOLHA O BOTÃO ");
lcd.setCursor (0, 1);
}
void loop () {
int leituraUV = analogRead (pinSensorUV); // read sensor
int indiceUV = map (leituraUV, 0, 203, 0, 10); // map from 0-10
int valorldr = analogRead(ldr);
int button = analogRead(A0);
int mode;
if ( button > 200 ) {
// do nothing, stay in same mode
mode = currentMode;
}
else if ( button > 80 ) {
// between 80..200
mode = 1;
}
else {
// betweeen 0..80
mode = 2;
}
if ( mode != currentMode ) {
// something changed
lcd.clear();
currentMode = mode;
switch (mode) {
case 1:
lcd.print ("LUMINOSIDADE:");
break;
case 2:
lcd.print ("indice UV:");
break;
}
}
// udpate display
lcd.setCursor (8, 1);
switch (currentMode) {
case 1:
lcd.print (valorldr);
break;
case 2:
lcd.print (indiceUV);
break;
}
sprintf (s, "leitura %4d, indice %3d", leituraUV, indiceUV);
Serial.println (s);
delay (500); // slow output
}
if your LDR is digital, you should be using digitalRead() to get the signal. It will read HIGH when the light level is low and LOW when the light level is above the threshold.
I would suggest writing a new sketch that just deals with getting this working and adjusting the threshold, etc.