Potentiometer LCD display set up.

Hello.

I need your help.

All I want to do is when I am starting to change potentiometer value display clear all information and show me current potentiometer and photoresistor value for display backlight set up. And after 5 second show me again normal operation sensor information. I know I need change something in void backlightSetUp (void), but can’t understand what.

Thanks.

This is my code.

#include <dht.h>
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_BMP085.h>

#define dht_apin A0
dht DHT;

#define DS1307_I2C_ADDRESS 0x68

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display

#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args)  write(args);
#else
#define printByte(args)  print(args,BYTE);
#endif

uint8_t simbol[8]  = {0x2, 0x5, 0x2};

Adafruit_BMP085 bmp;

#define photoPin A1
#define pirPin 2
#define potPin A2
#define sigPin 13
int photoValue = 0;
int pirValue = 0;
int potValue = 0;

void setup()
{
  lcd.init();                      // initialize the lcd
  lcd.createChar(0, simbol);
  lcd.home();
  Serial.begin(9600);
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1) {}
  }
  pinMode(pirPin, INPUT);
}

void loop()
{
  tmElements_t tm; // Print our characters on the LCD

  printTemperatureHumidity();

  printPressure();

  printDateAndTime();

  backlightControl();

  backlightSetUp();
}


void printTemperatureHumidity(void) {

  DHT.read11(dht_apin);
  lcd.setCursor(0, 0);
  lcd.print("T=");
  lcd.print(DHT.temperature, 0);
  lcd.printByte(0);
  lcd.print("C");
  lcd.setCursor(7, 0);
  lcd.print("H=");
  lcd.print(DHT.humidity, 0);
  lcd.print("%");

}

void printPressure(void) {

  lcd.setCursor(0, 1);
  lcd.print("T=");
  lcd.print(bmp.readTemperature(), 0);
  lcd.printByte(0);
  lcd.print("C ");
  lcd.setCursor(7, 1);
  lcd.print("P=");
  lcd.setCursor(9, 1);
  float hectoPascals = bmp.readPressure() / 100;
  lcd.print(hectoPascals, 0);
  lcd.print("hPa ");

}
void printDateAndTime(void)  {

  tmElements_t tm;
  if (RTC.read(tm)) {

    lcd.setCursor(0, 2); //Start at character 0 on line 2
    lcd.print("DATE :");
    lcd.print(tm.Day);
    lcd.print('/');
    lcd.print(tm.Month);
    lcd.print('/');
    lcd.print(tmYearToCalendar(tm.Year));

    lcd.setCursor(0, 3); //Start at character 0 on line 3
    lcd.print("TIME :");
    print2digits(tm.Hour);
    lcd.print(':');
    print2digits(tm.Minute);
    lcd.print(':');
    print2digits(tm.Second);

  }
  delay(1000);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    lcd.print('0');
  }
  lcd.print(number);
}

void backlightControl(void) {

  pirValue = digitalRead (pirPin);

  float photoValue = analogRead(photoPin);
  float potValue = analogRead(potPin);

  float photo = photoValue / 10.23;
  float pot = potValue / 10.23;

  Serial.print("Photo =");
  Serial.print(photo, 0);
  Serial.println(" %");
  Serial.print("Pot =");
  Serial.print(pot, 0);
  Serial.println(" %");
  Serial.print("PIR =");

  if (pirValue == HIGH) { // 13 pin indication for PIR sensor state

    digitalWrite(13, HIGH);
    Serial.println("ON");
  }
  else {

    digitalWrite(13, LOW);
    Serial.println("OFF");
  }

  if (photoValue >= potValue) {

    lcd.backlight();
  }
  else {

    lcd.noBacklight();
  }

  if (photoValue <= potValue) {

    lcd.noBacklight();

    if (pirValue == HIGH) {

      lcd.backlight();

    }
  }

}

void backlightSetUp (void) {

  float photoValue = analogRead(photoPin);
  float potValue = analogRead(potPin);

  float photo = photoValue / 10.23;
  float pot = potValue / 10.23;

  if (potValue != potValue) {

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Photo=");
    lcd.print(photo, 0);
    lcd.print("% ");
    lcd.setCursor(0, 1);
    lcd.print("Pot=");
    lcd.print(pot, 0);
    lcd.print("% ");
    delay(5000);
    lcd.clear();
  }
}
  if (potValue != potValue) {

What are the odds of that ever evaluating to true?

I know, it is not correct. That's why I am asking for your help.

Hello.

Realy no one can help me? What function I need to use?

Well, as the one poster indicated - your statement " if (potValue != potValue)" will never do what you want - you are saying "if (x != x)" -- comparing a variable to itself. I assume you meant to compare a previous reading with the current one or something, but that is not what your code is doing. Another thing worth considering - pots are notoriously noisy - it is not at all unusual to get readings like 535, 534, 535, 536 for example without touching the pot. What is a good idea is to add a "dead-band" where you ignore the current reading if the previous one was within say +/- 3 counts or something (you may need to experiment with that to get what you want).

heninsh:
Hello.

Realy no one can help me? What function I need to use?

What you need is an "old value"... like this:

cur_val = readPot(); // get current pot value

if (old_val != cur_val) {
    old_val = cur_val; // update old value
    display_something(); // whatever you do when the pot changes
}

or... if you want a little hysteresis (meaning the pot value can change a LITTLE and not trigger anything, but a larger change will):

cur_val = readPot(); // get current pot value

// trigger ONLY if the new value is 3 below or 3 above (a "window" of 6).
if (abs (old_val - new_val) > 3) { // abs is "absolute value"
    old_val = cur_val; // update
    display_something();
}

The second example allows the reading to change a little due to random noise and such without constantly triggering the "update display", and of course the window can be any size you desire.

Hopefully this helps you since I really don't know what you're trying to do....

All I want to do is make display back light set up when pot value is start changing. When I start change pot value I want see on display current photo resistor and pot value. And if it doesn’t change, do normal operation.

Thank you, now it’s work better, but not as I want. When pot value is 0 it’s show normal display, when pot value is changed (1-99%) every second is changing from normal to (photo and pot value) and back, doing it again and again. This is last thing what I need to solve.

What I think as normal display :smiley: :https://files.fm/g/psc9hh96#_

My code:

#include <dht.h>
#include <DS1307RTC.h>
#include <Time.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_BMP085.h>

#define dht_apin A0
dht DHT;

#define DS1307_I2C_ADDRESS 0x68

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 20 chars and 4 line display

#if defined(ARDUINO) && ARDUINO >= 100
#define printByte(args)  write(args);
#else
#define printByte(args)  print(args,BYTE);
#endif

uint8_t simbol[8]  = {0x2, 0x5, 0x2};

Adafruit_BMP085 bmp;

#define photoPin A1
#define pirPin 2
#define potPin A2
//#define sigPin 13
int photoValue = 0;
int pirValue = 0;
int potValue = 0;
int oldValue = 0;

void setup()
{
  lcd.init();                      // initialize the lcd
  lcd.createChar(0, simbol);
  lcd.home();
  Serial.begin(9600);
  if (!bmp.begin()) {
    Serial.println("Could not find a valid BMP085 sensor, check wiring!");
    while (1) {}
  }
  pinMode(pirPin, INPUT);
}

void loop()
{
  tmElements_t tm; // Print our characters on the LCD

  printTemperatureHumidity();

  printPressure();

  printDateAndTime();

  backlightControl();

  backlightSetUp();
}


void printTemperatureHumidity(void) {

  DHT.read11(dht_apin);
  lcd.setCursor(0, 0);
  lcd.print("T=");
  lcd.print(DHT.temperature, 0);
  lcd.printByte(0);
  lcd.print("C");
  lcd.setCursor(7, 0);
  lcd.print("H=");
  lcd.print(DHT.humidity, 0);
  lcd.print("%");

}

void printPressure(void) {

  lcd.setCursor(0, 1);
  lcd.print("T=");
  lcd.print(bmp.readTemperature(), 0);
  lcd.printByte(0);
  lcd.print("C ");
  lcd.setCursor(7, 1);
  lcd.print("P=");
  lcd.setCursor(9, 1);
  float hectoPascals = bmp.readPressure() / 100;
  lcd.print(hectoPascals, 0);
  lcd.print("hPa ");

}
void printDateAndTime(void)  {

  tmElements_t tm;
  if (RTC.read(tm)) {

    lcd.setCursor(0, 2); //Start at character 0 on line 2
    lcd.print("DATE :");
    lcd.print(tm.Day);
    lcd.print('/');
    lcd.print(tm.Month);
    lcd.print('/');
    lcd.print(tmYearToCalendar(tm.Year));

    lcd.setCursor(0, 3); //Start at character 0 on line 3
    lcd.print("TIME :");
    print2digits(tm.Hour);
    lcd.print(':');
    print2digits(tm.Minute);
    lcd.print(':');
    print2digits(tm.Second);

  }
  delay(1000);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    lcd.print('0');
  }
  lcd.print(number);
}

void backlightControl(void) {

  pirValue = digitalRead (pirPin);

  float photoValue = analogRead(photoPin);
  float potValue = analogRead(potPin);

  float photo = photoValue / 10.23; // calculate value from 0 to 100 %
  float pot = potValue / 10.23; // calculate value from 0 to 100 %

  Serial.print("Photo =");
  Serial.print(photo, 0);
  Serial.println(" %");
  Serial.print("Pot =");
  Serial.print(pot, 0);
  Serial.println(" %");
  Serial.print("PIR =");

  if (pirValue == HIGH) { // 13 pin indication for PIR sensor state

    //digitalWrite(13, HIGH);
    Serial.println("ON");
  }
  else {

    //digitalWrite(13, LOW);
    Serial.println("OFF");
  }

  if (photoValue >= potValue) {

    lcd.backlight();
  }
  else {

    lcd.noBacklight();
  }

  if (photoValue <= potValue) {

    lcd.noBacklight();

    if (pirValue == HIGH) {

      lcd.backlight();

    }
    if (pot >= 95) {

      lcd.noBacklight();
    }
  }

}

void backlightSetUp (void) {

  float photoValue = analogRead(photoPin);
  float potValue = analogRead(potPin);

  float photo = photoValue / 10.23; // calculate value from 0 to 100 %
  float pot = potValue / 10.23; // // calculate value from 0 to 100 %

  float currentValue = pot * 2;

  if (oldValue != currentValue) {

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Photo=");
    lcd.print(photo, 0);
    lcd.print("% ");
    lcd.setCursor(0, 1);
    lcd.print("Pot=");
    lcd.print(pot, 0);
    lcd.print("% ");
    delay(1000);
    oldValue = currentValue;
    lcd.clear();
  }
}

lots of things to work on here but take a look at this:

float photoValue = analogRead(photoPin);
float potValue = analogRead(potPin);
float photo = photoValue / 10.23; // calculate value from 0 to 100 %
float pot = potValue / 10.23; // calculate value from 0 to 100 %

consider:

int photoValue = analogRead(photoPin);
int potValue = analogRead(potPin);
int photo = map(photoValue, 0, 1023, 0, 100) ; // calculate value from 0 to 100 %
int pot = map(potValue, 0, 1023, 0 100); // calculate value from 0 to 100 %

here you go through a lot to paint the display and then wipe it…

  if (oldValue != currentValue) {

    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Photo=");
    lcd.print(photo, 0);
    lcd.print("% ");
    lcd.setCursor(0, 1);
    lcd.print("Pot=");
    lcd.print(pot, 0);
    lcd.print("% ");
    delay(1000);
    oldValue = currentValue;
    lcd.clear();  // <<<<<<<<<<<<<< does this belong here????
  }

also, while your code is well organized, and you clearly understand functions, maybe you need to focus on eliminating the delay()s and make your project more responsive

Thank you BulldogLowell.

Yes I changed it from float to int after my last post. But your comment with map function is better. Now I know one more function.

You forgot add (,) after second (0). I spend approximately half hour to fix it, because arduino program wrote a lot of errors. :smiley:

int pot = map(potValue, 0, 1023, 0 100); // calculate value from 0 to 100 %

Summary my project works well, but it's not that what I expect.

Summary my project works well, but it's not that what I expect.

What did you expect? Complete failure?

PaulS:
What did you expect? Complete failure?

No. :smiley:

I expected to saw floating values of photo sensor and potentiometer on display, when I start change pot value. Now I can see just fixed value for one second.

Now I can see just fixed value for one second.

Well, the "for one second" bit has been explained. Clearing the LCD after writing data to it is strange behavior, to say the least.

As for the "fixed value", that's because you changed from float to int.