Go Down

Topic: 'if/else' statement not working. wont switch to "cooling" (Read 798 times) previous topic - next topic

modsbyus

Code: [Select]

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

}

Arrch

#1
Nov 16, 2012, 11:59 pm Last Edit: Nov 17, 2012, 12:03 am by Arrch Reason: 1

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: [Select]

 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?

PeterH

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: [Select]


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");
}
I only provide help via the forum - please do not contact me for private consultancy.

PaulS

Code: [Select]
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?

modsbyus

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);

}


PaulS

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

modsbyus


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


I guess I used the wrong thing?

GoForSmoke

#7
Nov 17, 2012, 02:59 am Last Edit: Nov 17, 2012, 03:03 am by GoForSmoke Reason: 1
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.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

modsbyus

Saweet!! :D

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.

Arrch


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.

modsbyus

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

GoForSmoke

We all share the same hobby. Your success is our success.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up