Hi!
I'm having some issues with my LCD not printing as well as my switch state not registering with my code.
I'm attempting to make an Arduino that, when it turns on, the LCD prints, "What is your temperature?" and a red LED is turned on. Once the switch is pressed, the red LED turns off, a green LED begins flashing, and the user touches the temperature sensor. The switch stays pressed/the user holds the temperature sensor for 15 seconds, and afterwards, a Piezo sounds, indicating that the user can take their finger off. The LCD is then supposed to display the temperature.
As of now, when my code starts, it doesn't recognize that the switch isn't being pressed, and jumps immediately into the flashing green LED. (The piezo sound works after 15 seconds and isn't having any issues). However, it doesn't recognize that the switch isn't being pressed, and nothing at all will print on my LCD, despite the potentiometer being set up correctly and showing the black boxes that appear originally.
If you have any idea what might be wrong with my setup/code, can you please help me out?
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int switchState = 0;
int j;
const int sensorPin = A0;
const float baselineTemp = 20.0;
void setup() {
// put your setup code here, to run once:
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(2, INPUT);
lcd.begin(16, 2);
lcd.print("What is");
lcd.setCursor(0, 1);
lcd.print("your temperature?");
}
void loop() {
switchState = digitalRead(7);
if (switchState == LOW) {
digitalWrite(9, HIGH); // switch is not pressed - red LED on
}
else {
digitalWrite(9, LOW); // turn off red LED
for (j=0; j<=60; j=j+1) {
digitalWrite(10, HIGH);
delay(250);
digitalWrite(10, LOW);
delay(250);
// switch is pressed - flash green LED for 15 seconds
}
//after 15 seconds, sound Piezo and take temperature from serial monitor to display on LCD
tone(8, 1000);
delay(1000);
noTone(8);
int sensorVal = analogRead(sensorPin);
float voltage = (sensorVal/1024.0) * 5.0;
float temperature = (voltage - 0.5) * 100;
digitalWrite(10, LOW);
digitalWrite(9, HIGH); //turn green LED off and red LED back on signaling it's no longer taking temperature
if(temperature < baselineTemp+2) {
lcd.print("Ice cold");
lcd.setCursor(0, 1);
lcd.print(temperature);
}else if(temperature >= baselineTemp+2 && temperature < baselineTemp+6) {
lcd.print("Normal temp");
lcd.setCursor(0, 1);
lcd.print(temperature);
}
else {
lcd.print("Burning up!");
lcd.setCursor(0, 1);
lcd.print(temperature);
}
}
}
Arduino_Project.ino (1.71 KB)