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);
}