2 sensor in the LCD KEYPAD SHIELD

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?

#include <LiquidCrystal.h>

LiquidCrystal lcd (8,9,4,5,6,7);

#define pinSensorUV    A5
#define ldr            3
char s [80];

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 ()
{
    
    
   
    while((analogRead(0))<80){
      lcd.clear();
      int leituraUV = analogRead (pinSensorUV);     // read sensor
    int indiceUV  = map (leituraUV, 0,203,0,10);  // map from 0-10
    lcd.setCursor (0,0);
    lcd.print ("indice UV:");
    lcd.setCursor (8,1);
    lcd.print (indiceUV);

    sprintf (s, "leitura %4d, indice %3d", leituraUV, indiceUV);
    Serial.println (s);
    }
    while((analogRead(0))<200 && (analogRead(0)) >80) {
    lcd.clear();
    pinMode(ldr,INPUT);
    int valorldr = 0;
    valorldr=analogRead(ldr);
    lcd.setCursor (0,0);
    lcd.print ("LUMINOSIDADE:");
    lcd.setCursor (8,1);
    lcd.print (valorldr);
    sprintf (s, "leitura %4d", valorldr);
    Serial.println (s);
    }
    delay (500); // slow output
}

Do you have a data sheet or some documentation for that.

A 0.1uF ceramic capacitor from the LDR output pin to ground can help to stabilize the LDR readings by shunting noise to ground.

You do not need to set the LDR pinMode to INPUT. The analogRead function sets the pin up.

Your topic was MOVED to its current forum category which is more appropriate than the original as it is not an Introductory Tutorial

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
}

thankyou! the automatic reading is working very fine, I only keep with the problem with the LDR my sensor is a digital one like this https://www.amazon.com/Detection-Optical-Sensitive-Resistance-Photosensitive/dp/B07XFZ99XL and the outputs values are very wrong

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.

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