MAX6675 programming,need help...

hai everyone…i have a programming that i don’t really understand.i need an explanation for this program.not the whole program that i do not understand…just a small part…

#include <max6675.h>
#include <LiquidCrystal.h>
#include <Wire.h>

int thermoDO = 29;   //so
int thermoCS = 30;   //cs
int thermoCLK = 28;  //sck

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
int vccPin = 3;
int gndPin = 2;

LiquidCrystal lcd(22,23,27,26,25,24);

// make a cute degree symbol
uint8_t degree[8]  = {140,146,146,140,128,128,128,128};

void setup() {
  Serial.begin(9600);
  // use Arduino pins 
  pinMode(vccPin, OUTPUT); digitalWrite(vccPin, HIGH);
  pinMode(gndPin, OUTPUT); digitalWrite(gndPin, LOW);
  
  lcd.begin(16, 2);
  lcd.createChar(0, degree);

  // wait for MAX chip to stabilize
  delay(500);

 
}

void loop() {
  // basic readout test, just print the current temp
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("MAX6675 test");
  
  // go to line #1
  lcd.setCursor(0,1);
  lcd.print(thermocouple.readCelsius());
#if ARDUINO >= 100
  lcd.write((byte)0);
#else
  lcd.print(0, BYTE);
#endif
  lcd.print("C ");
  delay(1000);

 if (thermocouple.readCelsius()>130)   //this is the part that i add by myself to turn on motor
  {
  digitalWrite(50,HIGH);
  }else if (thermocouple.readCelsius()<130)
  {
    digitalWrite(50,LOW);
  } 

}

that is the overall program.the part that i don’t understand is this…

#if ARDUINO >= 100
  lcd.write((byte)0);
#else
  lcd.print(0, BYTE);
#endif
  lcd.print("C ");
  delay(1000);

the other part that i want to ask is about my motor.i already test the output…and i get the reading whenever the temperature turn into the reading that i want… but since the temperature are not so stable because the value always changes due to error and disturbance, i want to set some range.this is just an example,let say around 130 degree to 140 degree celcius,my motor will turn on. so how can i fix this program

if (thermocouple.readCelsius()>130)   //this is the part that i add by myself to turn on motor.turn on motor using relay and ext power supply.
  {
  digitalWrite(50,HIGH);
  }else if (thermocouple.readCelsius()<130)
  {
    digitalWrite(50,LOW);
  }
#if ARDUINO >= 100
  lcd.write((byte)0);
#else
  lcd.print(0, BYTE);
#endif
  lcd.print("C ");
  delay(1000);

if is a compiler directive, this means that that piece of code is evaluated by the compiler when your code is compiled. What it is doing is checking the version number of the Arduino library. In one of the Arduino library files that gets included automatically it defines a constant called ARDUINO which stores the version number.

Here it checks if it is greater than 100, if so your code will compile as:

lcd.write((byte)0);
lcd.print("C ");
delay(1000);

Otherwise (if ARDUINO is less than 100), it compiles:

lcd.print(0,BYTE);
lcd.print("C ");
delay(1000);

This is just because the lcd library was changed at Arduino version 100, but by including this directive is makes sure this code still compiles for people running the older versions.

I'm afraid I don't really understand what you are asking for the second part of your question. What actually is the problem? The code you have written looks reasonably valid (although you don't need the second if statement:

if(thermocouple>130){
   //do something
}
else{
   //do something else
}

oh i see… it means that it is just the matter of arduino version. that is why the program become like that…i understand already!!.. thanks a lot tobyb121. you really help me… i feel very grateful you are replying my question. let me explain again. this code i want to change it to set some range of temperature.i know this code already right.and i already get the output…but i want to set range.

 if (thermocouple.readCelsius()>130)   //this is the part that i add by myself to turn on motor
  {
  digitalWrite(50,HIGH);
  }else if (thermocouple.readCelsius()<130)
  {
    digitalWrite(50,LOW);
  }

as an example in something that more can be understand,i want my code to be like this

if(thermocouple.readCelcius() in range over 130 to 140)
{
  digitalWrite(50,HIGH);
  }else if (thermocouple.readCelsius() in range under 130 to 100)
  {
    digitalWrite(50,LOW);
  }

thanks a lot tobyb121 :)… and i just want to know another thing…i still new in this forum. under our name we have +karma… what is that for??sorry for asking such question…i am newbie.

You can combine logic expressions by using: && and ||
For example:

A=true
B=false
(A && B) => false
(A || B) => true
(A && !B) => true, A and not B (not false is true)

So for your code:

  if (thermocouple.readCelsius()>130 && thermocouple.readCelsius()<140)
  {
    digitalWrite(50,HIGH);
  }
  else if (thermocouple.readCelsius()<130)
  {
    digitalWrite(50,LOW);
  }

Be aware that for temperatures over 140 neither will be executed, I’m not sure what you are trying to do with this code but you will probably want to have some condition for the temperature being above 140 or it won’t look like it’s doing anything, after it’s got to 140 you haven’t got it setting the pin low, so it will just stay high.

One other thing I would change is to create a variable and store the value of thermocouple.readCelsius() in that, so that you only call it once, each time you call it takes time, and the value could even change between two calls.

thanks tobyb121..because this thing is the key for my project. i think i have improve my programming by learning from u. :) .actually i still a beginner in arduino.and with a short of time to complete my project,you have help me a lot.thank you very much. you have give a very good explanation. but sorry for asking once again. i do not understand about this

One other thing I would change is to create a variable and store the value of thermocouple.readCelsius() in that, so that you only call it once, each time you call it takes time, and the value could even change between two calls.

can u give me an example on how can i do like what you have said? ,so i can understand better or if u have any link or tutorial that i can read, i would like to learn about it.. :) thanks

void loop() {
   int   temperature=thermocouple.readCelsius();

  // basic readout test, just print the current temp
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("MAX6675 test");
  
  // go to line #1
  lcd.setCursor(0,1);
  lcd.print(temperature);
#if ARDUINO >= 100
  lcd.write((byte)0);
#else
  lcd.print(0, BYTE);
#endif
  lcd.print("C ");
  delay(1000);

 if (temperature>130 && temperature<140)   //this is the part that i add by myself to turn on motor
  {
  digitalWrite(50,HIGH);
  }else if (temperature<130)
  {
    digitalWrite(50,LOW);
  } 

}

This way the function thermocouple.readCelsius() only gets called once per loop, which is better than the 4/5 times it was being called before.

oh.i see..now i understad.thank you.I so greatly appreciate you are answering all my question. have a nice day. :)