I am doing a project for a temperature sensor. I have an LCD display, a temperature sensor, a keypad, a neo pixel and a sounder. The LCD displays a line of text which says to select A or B. When A of the keypad is selected, the LCD display shows the temperature in degrees Celsius, When B is selected it shows in Fahrenheit. Neither of the temperatures will update automatically on the display, they will only update when the key is pressed again. The neo pixel changes colour for different temperatures and appears to update itself as the temperature varies and the sounder goes off at a certain temperature also.
I have tried everything to get the temperature to update itself but nothing seems to work.
Any ideas would be really helpful.
My program is below.
#include <Key.h>
#include <Keypad.h>
#include <Adafruit_NeoPixel.h>
#include <Adafruit_LiquidCrystal.h> //Include the LCD library
#define NUM_LEDS 1 //This is the number of pixels in the strip (just 1 in our case)
#define PIN 8 //This is the pin number your neopixel is connected to
Adafruit_NeoPixel strip = Adafruit_NeoPixel (NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800); //Required by library
Adafruit_LiquidCrystal lcd(7, 6, 5, 4, 3, 2); //I/O pin numbers for the LCD
const int sensor = A0; //TMP36 connected to analog input A0
const int buzzer = A5; //Buzzer connected to analog input A5
const byte ROWS = 4; //the keypad has four rows
const byte COLS = 1; //and four columns
char keys [ROWS] [COLS] = {
{'A'},
{'B'},
{'C'},
{'D'},
}; //use an array to hold the key values
byte rowPins [ROWS] = {13, 12, 11, 10}; //define uno's I/O pin numbers
byte colPins [COLS] = {9};
Keypad keypad =
Keypad (makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup () {
lcd.clear();
//set up the LCD's number of columns and rows:
lcd.begin (16, 2); //Display has 16 columns and 2 rows
lcd.print ("Select A or B."); //Display a message
pinMode (sensor, INPUT); //A0 is an input
pinMode (buzzer, INPUT); //A5 is an input
strip.begin (); //Function called from the library
Serial.begin(9600);
keypad.setDebounceTime (100);
}
void loop () {
float sensorVoltage, sensorTemperature, sensorTempf;
int sensorValue;
//Get sensor value and convert to deg.C
sensorValue = analogRead (sensor);
//Convert value to voltage
sensorVoltage = (sensorValue / 1024.0) * 4.88;
//Convert voltage to temperature celsius
sensorTemperature = (sensorVoltage - 0.5) * 100;
//convert temperature c to f
sensorTempf = (sensorTemperature * 1.8) + 32;
//display temperature
lcd.setCursor (0, 1); //Begin on the second line
char key = keypad.getKey(); //let's check the keypad
if (key == 'A') { //lcd to display in degrees celsius when button A of keypad is pressed
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.setCursor(0,1);
lcd.print (sensorTemperature); //print the temperature
lcd.print (" ");
lcd.write (B11011111); //upper 'o' symbol
lcd.print ("C");
}
else if (key == 'B') { //lcd to display in degrees farenheit when button B of keypad is pressed
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.setCursor(0,1);
lcd.print (sensorTempf); //print the temperature
lcd.print (" ");
lcd.write (B11011111); //upper 'o' symbol
lcd.print ("F");
}
if (sensorTemperature >= 17 && sensorTemperature <= 24) {
Serial.println("green");
for (int i = 0; i <= 255; i++)
strip.setPixelColor (i, strip.Color (0, 255, 0)); //green led
strip.show (); //sends data to pixel
}
else if (sensorTemperature < 17) {
Serial.println("blue");
for (int i = 0; i <= 255; i++)
strip.setPixelColor (i, strip.Color (0, 0, 255)); //blue led
strip.show (); //sends data to pixel
}
else if (sensorTemperature > 24) {
Serial.println("red");
for (int i = 0; i <= 255; i++)
strip.setPixelColor (i, strip.Color (255, 0, 0)); //red led
strip.show (); //sends data to pixel
}
Serial.println (sensorTemperature);
if (sensorTemperature >= 27){
tone (buzzer, 2000); //buzzer on
for (int i = 0; i <= 255; i++)
strip.setPixelColor (i, strip.Color (255, 0, 0)); // red flashes
strip.show ();
delay (50);
for (int i = 0; i <= 255; i++)
strip.setPixelColor (i, strip.Color (0, 0, 0));
strip.show ();
delay (50);
}
else {
tone(buzzer, 0); //buzzer off
}
}