Pages: [1]   Go Down
Author Topic: 'if/else' statement not working. wont switch to "cooling"  (Read 729 times)
0 Members and 1 Guest are viewing this topic.
USA
Offline Offline
Full Member
***
Karma: 0
Posts: 115
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
/*
      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);

}
Logged

California
Offline Offline
Faraday Member
**
Karma: 91
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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()).

Code:
 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?
« Last Edit: November 16, 2012, 06:03:44 pm by Arrch » Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:

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

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50086
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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?
Logged

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 115
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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..

Quote
/*
      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);

}

Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50086
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (val = 1)
Why are you assigning a value of 1 to val in an if statement?
Logged

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 115
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (val = 1)
Why are you assigning a value of 1 to val in an if statement?

I guess I used the wrong thing?
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
http://arduino.cc/en/Reference/HomePage

You might want to make an easy to find bookmark to that page.
« Last Edit: November 16, 2012, 09:03:26 pm by GoForSmoke » Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 115
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Saweet!! smiley-grin
 
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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 91
Posts: 3426
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 115
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: