'if/else' statement not working. wont switch to "cooling"

/*
      TickerTape.cpp, based off of LCD_example from
      http://www.hacktronics.com/Tutorials/arduino-character-lcd-tutorial.html
*/

#include <LiquidCrystal.h>



//  LCD pin 1 to pin 1 to ground
 // LCD pin 2 to + 5 volts
 // LCD RS pin to digital pin 4
 // LCD Enable pin to arduino digital pin 5
 // LCD D4 (pin 11) to arduino digital pin 6
 // LCD D5 (pin 12) to arduino digital pin 7
 // LCD D6 (pin 13) to arduino digital pin 8
 // LCD D7 (pin 14) to arduino digital pin 9
 // LCD R/W pin to ground
 // ends to +5V and ground
 // wiper to LCD  pin 3
LiquidCrystal lcd(4,5,6,7,8,9);
int sencePin = 0;
int coolingPin = 11;
int heatingPin = 12;
int val = analogRead(sencePin);



void setup()
{
      
      lcd.clear();        // Start with a blank screen
      lcd.begin(16, 2);
      Serial.begin(9600);
      pinMode(coolingPin, OUTPUT);
      pinMode(heatingPin, OUTPUT);
}

void loop(){
  heatCool();
  readTemp();
  

}

void heatCool()
{
  
  if (val > 1) 
 { 
   digitalWrite(coolingPin, HIGH);
  digitalWrite(heatingPin, LOW);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("COOLING");
  
  
 }
  else if (val < 1)
  {
  digitalWrite(coolingPin, LOW);
  digitalWrite(heatingPin, HIGH);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("HEATING");
  
  
}
delay(500);
}
   
void readTemp(){
 // print the button state to a serial terminal
  Serial.println(analogRead(sencePin));
  lcd.clear();
  lcd.setCursor(0, 1);
  lcd.print(analogRead(sencePin)); 
  delay(1000);

}

modsbyus:
int val = analogRead(sencePin);

Code like this should be inside the setup() or loop() function. In this case, you probably want it inside loop(), otherwise, it will never update outside of the initial read (like if it was in setup()).

  if (val > 1) 
  ...
  else if (val < 1)

You realize analogRead() returns a value between 0 and 1023? Is it your intention to do nothing if you get a reading of 1, the "else if" if you get 0 and the "if" if you get 2 to 1023?

As Arrch says, you need to read the analog value in loop(). You also need to decide which values should cause heating, nothing and cooling. Probably, you will want to compare the analog value against a couple of thresholds.

val = analogRead(sencePin);

if (val > COOLING_THRESHOLD) 
{ 
  digitalWrite(coolingPin, HIGH);
  digitalWrite(heatingPin, LOW);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("COOLING");
}
else if (val < HEATING_THRESHOLD)
{
  digitalWrite(coolingPin, LOW);
  digitalWrite(heatingPin, HIGH);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("HEATING");
}
else
{
  // not heating or cooling
  digitalWrite(coolingPin, LOW);
  digitalWrite(heatingPin, LOW);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("NOTHING");
}
void loop()
{
  heatCool();
  readTemp();
}

I also question why you are doing something about the temperature BEFORE you determine what the temperature is. Do you put your shoes on first, then your socks?

Okay, Sorry guys. I'm very new. Right now I am just trying to get the if/else statements to work right now. I don't have my temp sensor yet. I am still waiting for that to come in the mail. With the if/else statement I am trying to use a spdt switch with a common leg to switch the 'thermostat' between heat and cool mode. I am successfully reading a one one a zero based on the switch position. However, It is stuck saying cooling now since the changes that you guys suggested. I don't understand why it won't switch..

/*
TickerTape.cpp, based off of LCD_example from
http://www.hacktronics.com/Tutorials/arduino-character-lcd-tutorial.html
*/

#include <LiquidCrystal.h>

// LCD pin 1 to pin 1 to ground
// LCD pin 2 to + 5 volts
// LCD RS pin to digital pin 4
// LCD Enable pin to arduino digital pin 5
// LCD D4 (pin 11) to arduino digital pin 6
// LCD D5 (pin 12) to arduino digital pin 7
// LCD D6 (pin 13) to arduino digital pin 8
// LCD D7 (pin 14) to arduino digital pin 9
// LCD R/W pin to ground
// ends to +5V and ground
// wiper to LCD pin 3
LiquidCrystal lcd(4,5,6,7,8,9);
int sencePin = 2;
int coolingPin = 11;
int heatingPin = 12;

void setup()
{

lcd.clear(); // Start with a blank screen
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(coolingPin, OUTPUT);
pinMode(heatingPin, OUTPUT);
}

void loop(){
readTemp();
heatCool();

}

void heatCool()
{
int val = digitalRead(sencePin);
if (val = 1)
{
digitalWrite(coolingPin, HIGH);
digitalWrite(heatingPin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("COOLING");

}
else if (val = 0)
{
digitalWrite(coolingPin, LOW);
digitalWrite(heatingPin, HIGH);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("HEATING");

}
delay(500);
}

void readTemp(){
// print the button state to a serial terminal
Serial.println(digitalRead(sencePin));
lcd.clear();
lcd.setCursor(0, 1);
lcd.print(digitalRead(sencePin));
delay(1000);

}

  if (val = 1)

Why are you assigning a value of 1 to val in an if statement?

PaulS:

  if (val = 1)

Why are you assigning a value of 1 to val in an if statement?

I guess I used the wrong thing?

You did say new.

= means assign what is on the right side of = to what is on the left side.

byte A = 1; // assigns the value 1 to the byte variable A

== means 'equal to'

if ( A == 1 ) Serial.println( "Yes, A == 1" );
else Serial.println( "No, A != 1" );

Check the left side of this page about halfway down where it says Comparison Operators:

You might want to make an easy to find bookmark to that page.

Saweet!! :smiley:

Any Ideas as to why my top and bottom lines on my LCD are giving each other turns flashing?
(One flashes on and at the same time the other flashes off and the alternates)
Please let me know if I need to ask this question somewhere else.

modsbyus:
Any Ideas as to why my top and bottom lines on my LCD are giving each other turns flashing?
(One flashes on and at the same time the other flashes off and the alternates)
Please let me know if I need to ask this question somewhere else.

Because you keep clearing between prints, don't do that.

Thank you again. You guys have been great. This is by far the most responsive forum I have ever been in.

We all share the same hobby. Your success is our success.