Probably simple fixes but I'm new and need help please

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)

How is your switch connected to the Arduino ?

ntight:
If you have any idea what might be wrong with my setup/code, can you please help me out?

It's all the fault of the little yellow guy with the dark glasses :slight_smile:

He will go away, and it will be much easier to help, if you modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

...R

This will not help you:

 pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(2, INPUT);

Put some code in to give names to the I/O pins.

const byte redLED = 9; // Outside any function so it's global

This will make it easier for *everyone *to keep track of what's happening. Like so:

pinMode(redLED, OUTPUT);

and

digitalWrite(redLED, HIGH);

and head off errors like:

pinMode(2, INPUT); vs. switchState = digitalRead(7);

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

It looks like you have your button connected from the digital input pin to 5V,
When the button is not pressed the digital input does not go LOW, you have to put a 10K resistor between the digital input pin and gnd to make it got to gnd.

Tom... :slight_smile: