Help with alternating message on LCD Screen, Partially works.

Hello,

I first would like to ask for your patience as I am very new to coding and still learning the C++ language. I am an Electrical student but we never touch anything Electronic (Only the big stuff) so although I know electricity, I am learning this part of it very fresh. A project I am working on will require me to program and monitor various sensor inputs so I am teaching that to myself now.

I have the Arduino Uno R3, along with the 2x16 LCD display. I have it set up to two sensors, a temperature sensor, and a Phototransistor. It will change an RGB LED depending on the temperature, and display the Temperature and Light Level to the LCD (Along with the Serial Monitor). It is basically a modification of the tutorial program that comes with the Arduino book.

The program successfully displays the data on the LCD screen as I have formatted it, and will go back and forth between the two displays, but, not always. Sometimes it stays on one display for a very long time. Other times, it will go back and forth quickly. As far as I know, the code should dictate it to wait four seconds between each display. If anyone can point out an error, or tell me a more efficient way to do this, I would be very happy to learn more. The code is below.

Thank you!

const int PhotoSensorPin = A2;                                            
const int TempSensorPin = A0;                           
const float baselineLight = 12;                                                 
const float baselineTemp = 22.0;                        
const int ledPinR = 6;
const int ledPinB = 7;
const int ledPinG = 8;
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);                   

void setup() {
  
  Serial.begin(9600);                                   
  lcd.begin (16, 2);
  lcd.print("Light & Temp");
  lcd.setCursor (0, 1);
  lcd.print("Initializing...");
  pinMode(ledPinR, OUTPUT);
  pinMode(ledPinB, OUTPUT);
  pinMode(ledPinG, OUTPUT);
  delay(1000);
  
 }  

void loop() {

int PhotoSensorValue = analogRead(PhotoSensorPin);
int TempSensorVal = analogRead(TempSensorPin);                
Serial.print("Temp Sensor Value: ");                      
Serial.print(TempSensorVal);
float voltage = (TempSensorVal/1024.0) * 5.0;           
Serial.print(", Temp Volts: ");
Serial.print(voltage);
Serial.print(", Degrees C: ");
float temperature = (voltage - .5) * 100;                   
Serial.print(temperature);
Serial.print(", Photo Sensor Value: ");
Serial.println(PhotoSensorValue);
  if (temperature < baselineTemp - 2) {
     lcd.clear ();
    lcd.print("Air Temp: LOW!");
    lcd.setCursor(6, 1);
    lcd.print((char)223);
    lcd.print("C");
    lcd.setCursor(1, 1);
    lcd.print(temperature);
    digitalWrite(ledPinB, HIGH);
    digitalWrite(ledPinG, LOW);
    digitalWrite(ledPinR, LOW);
  } 
  else if (temperature >= baselineTemp && temperature < baselineTemp + 2) {
     lcd.clear ();
    lcd.print("Air Temp: OK");
    lcd.setCursor(6, 1);
    lcd.print((char)223);
    lcd.print("C");
    lcd.setCursor(1, 1);
    lcd.print(temperature);
    digitalWrite(ledPinG, HIGH);
    digitalWrite(ledPinB, LOW);
    digitalWrite(ledPinR, LOW);
  } 
  else if (temperature >= baselineTemp + 2) {
     lcd.clear ();
    lcd.print("Air Temp: HIGH!");
    lcd.setCursor(6, 1);
    lcd.print((char)223);
    lcd.print("C");
    lcd.setCursor(1, 1);
    lcd.print(temperature);
    digitalWrite(ledPinR, HIGH);
    digitalWrite(ledPinB, LOW);
    digitalWrite(ledPinG, LOW);
  }
  delay(4000);
 if (PhotoSensorValue < baselineLight - 2) {
     lcd.clear ();
    lcd.print("Light Lvl: LOW!");
    lcd.setCursor(1, 1);
    lcd.print(PhotoSensorValue);
  } 
  else if (PhotoSensorValue >= baselineLight && PhotoSensorValue < baselineLight + 2) {
     lcd.clear ();
    lcd.print("Light Lvl: OK");
    lcd.setCursor(1, 1);
    lcd.print(PhotoSensorValue);
  } 
  else if (PhotoSensorValue >= baselineLight + 2) {
     lcd.clear ();
    lcd.print("Light Lvl: HIGH!");
    lcd.setCursor(1, 1);
    lcd.print(PhotoSensorValue);
}
delay (4000);
}

In both of your if/else statements, you have a range between (baseline - 2) and baseline in which no action is taken.

Hmm, well if I change the baseline to make each of those if/else statements true, it does work. For instance, the temperature will make the LED blue if it is "< baselineTemp - 2", green if it is ">= baselineTemp && temperature < baselineTemp + 2" and red if it is ">= baselineTemp + 2"

The LED changes appropriately to reflect these statements. If the Baseline temp is 22, and the temp is 19, for example, the LED goes blue.

The LCD also shows the LOW! display for both temperature and light. Everything works as it should, except the screen sometimes doesn't alternate correctly is all.

When all of the if statements are false, nothing will be written to the LCD, and it will just retain whatever was last displayed.

As an example, if baselineTemp is 22 and temp is 21, no temperature information will be displayed, and you will only see light information on the LCD.

Ohh. It is a logic problem then. Simple enough! Funny I didn't catch that. Thanks, a second set of eyes always helps.

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