if/else statements

So, im looking to create a weather station-esque project, i am going to have an Arduino displaying temperature and time to an LCD screen via inputted data

from a Thermistor. I want to step it up a notch by telling me what i should wear when the temperature is at different levels. I don't really understand if then statements, so would i do something like the code below? (btw this is the first code i have EVER written, and i dont even have an arduino to test with. hopefully getting one for my birthday)

(I know that i haven't included any of the conversions for converting the raw data to Fahrenheit, but that's a question for later.)

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2)

//Thermistor connected to Analog A0

void setup()
{

lcd.begin(16,4);
lcd.clear();

}

void loop()
{

if (analogRead(0) > 50) // this means like 50 degrees after its been converted
{
lcd.print(Fahrenheit:)
lcd.print(int analogRead(0)) // display temperature
lcd.setCursor(0,2)
lcd.print("its a tit bit nipply out might want to wear pants")
}

else if (analogRead(0) < 80) // this means like 80 degrees after its been converted
{
lcd.print(Fahrenheit:)
lcd.print(int analogRead(0)) // display temperature
lcd.setCursor(0,2)
lcd.print("hey i would take off all your clothes its hot out")
}

else
{
lcd.print(Fahrenheit:)
lcd.print(int analogRead(0)) //display temperature
lcd.setCursor(0,2)
lcd.print("its mild out so just chill")
}
}

i also dont understand how the conversion code works for example:

The Simple Code

============================================================

#include <math.h>

double Thermister(int RawADC) {
double Temp;
Temp = log(((10240000/RawADC) - 10000));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
Temp = Temp - 273.15; // Convert Kelvin to Celcius
Temp = (Temp * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
return Temp;
}

void setup() {
Serial.begin(115200);
}

void loop() {
Serial.println(int(Thermister(analogRead(0)))); // How does this display Fahrenheit? when the value returned int the top equation is "Temp"
delay(100);
}

I don't really understand if then statements, so would i do something like the code below?

You have the right idea, and the syntax looks OK (except for the missing ;s). There is a flaw in your logic though. Removing the actual LCD set up code, your if block looks like this (with proper indentation):

if (analogRead(0) > 50) // this means like 50 degrees after its been converted
{
  lcd.print("its a tit bit nipply out might want to wear pants")
}
else if (analogRead(0) < 80) // this means like 80 degrees after its been converted
{
  lcd.print("hey i would take off all your clothes its hot out")
}
else
{
  lcd.print("its mild out so just chill")
}

Now, if the temperature is above 50, long pants might or might not be required. However, once that if test has been performed, the only cases for which it is not true is when the temperature is 50 or below. Now, for what values can the temperature be 50 or below AND less then 80? Yes, that's right. Any temperature that is less than or equal to 50 is going to be below 80. So, there is nothing left for the else to handle, and you will see one of two messages. Either it is cool enough to wear pants, when it is more than 50 degrees, or it is too hot to wear clothes when it is less then or equal to 50.

If that logic seems sound to you, fine, but it looks wrong to me.

Start with the highest breakpoint, and work down (or up) from there. Something like:

int temp = analogRead(0);
// Convert temp to temperature
if(temp > 80)
{
   // It's hot...
}
else if(temp > 50)
{
   // It's mild (between 50 and 80)...
}
else if(temp > 30)
{
   // It's cool out (between 30 and 50)...
}
else
{
   // It's damn cold out...
}

i also dont understand how the conversion code works for example:

The thermistor outputs a voltage based on the temperature. It's not a linear graph. It is probably not even a line on a logarithmic plot.

The data sheet for your thermistor should show you a graph of voltage vs. temperature. It should also show you the equation for converting voltage to temperature.

The code is simply implementing that equation.

haha okay so i did get how the if/else statements work, my temperatures were just off. i wrote this during the last 5 mins of class i just wanted to know if it was the right way to code if/else statements.

i probably worded how i didnt understand the conversion the value/variable thing (im super newb in programming) returned in that big conversion at the end is called "Temp" and it isnt even referred to in the lcd.print command? how does that work? is the actual value of the temperature the "Thermistor" value at the top? im just wicked confused on what that code returns that actually gets printed to the LCD screen

Because you call the Thermistor function, so it reads the analog value, converts the value in the function and prints out the returned value from the function in the lcd.

thanks a bunch i understand now. so its the function converting the voltage read that is called in the lcd.print command. i get it now.

Can anyone help me with this code i am new to programing and i just want this if else statement to work.

#define ping2pin      10      // left sensor
#define pingPin       9      // forward Ping ultrasonic sensor on pin D9.
#define leftDir1      8      // Left motor direction 1, to AIn1.
#define leftDir2      7      // Left motor direction 2, to AIn2.
#define rightDir1     6      // Right motor direction 1, to BIn1.
#define rightDir2     5      // Right motor direction 2, to BIn2.

#define BOUNDARY     50     // (cm) Avoid objects closer than 20cm.
#define INTERVAL     25      // (ms) Interval between distance readings.
#define limit        30
// setup
// Set motor pins as OUTPUTS, initialize Serial

void setup()                                                 
{
  pinMode(leftDir1, OUTPUT);        // Set motor direction pins as outputs.
  pinMode(leftDir2, OUTPUT);      
  pinMode(rightDir1, OUTPUT);      
  pinMode(rightDir2, OUTPUT); 
  Serial.begin(9600);
}


// Main program
// Roam around while avoiding objects.
// 
// Set motors to move forward,
// Take distance readings over and over, 
// as long as no objects are too close (determined by BOUNDARY).
// If object is too close, avoid it -- back up and turn.
// Repeat.

void loop()                     
{
  long xdistance;                    // Distance reading from front ping.
  long ydistance;                    // Distance reading from left ping
{                         // Robot moves forward continuously.
  if(((xdistance >= BOUNDARY)&&(ydistance <= BOUNDARY)&&(ydistance >= limit))); 
  {
    forward();
  }
  else if((xdistance < BOUNDARY)&&(ydistance > BOUNDARY));    // Loop while no objects close-by.
  {
    leftTurn(500);  
  } 
 else((xdistance > BOUNDARY)&&(ydistance <= limit));
{
  rightTurn(20);
}     // Turn left 500 ms.

} // end Main program
} 
  // Robot has sensed a nearby object and exited the while loop.
  // Take evasive action to avoid object.          
      



// forward
//
// Move robot forward by setting both wheels forward.
// Will persist until something else changes the
// motors' directions.

void forward()
{
  digitalWrite(leftDir1, LOW);                    
  digitalWrite(leftDir2, LOW);
  digitalWrite(rightDir1, HIGH);     // LEFT motor forward.
  digitalWrite(rightDir2, HIGH);     // RIGHT MOTOR FORWARD.
}  


// backward
//
// Move robot backward by setting both wheels backward.
// Will persist until something else changes the
// motors' directions.

void backward()
{
  digitalWrite(leftDir1, HIGH);     // Left motor backward.
  digitalWrite(leftDir2, HIGH);
  digitalWrite(rightDir1, LOW);    // Right motor backward.
  digitalWrite(rightDir2, LOW);
}


// leftTurn
//
// Turn robot to left by moving wheels in opposite directions.
// Amount of turning is determined by duration argument (ms).

void leftTurn(int duration)
{
  digitalWrite(leftDir1, HIGH);     // Left motor backward.
  digitalWrite(leftDir2, LOW);
  digitalWrite(rightDir1, HIGH);     // Right motor forward.
  digitalWrite(rightDir2, HIGH);
  delay(duration);                  // Turning time (ms).
}
void rightTurn(int duration)
{
  digitalWrite(leftDir1,LOW);
  digitalWrite(leftDir2, HIGH);
  digitalWrite(rightDir1,HIGH);
  digitalWrite(rightDir2,HIGH);
}


// readDistance
// Take a distance reading from Ping ultrasonic rangefinder.
// from http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor

long readxDistance()
{
  long duration, inches, cm;

  // The Ping is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the Ping: a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // Convert the time into a distance.
  cm = microsecondsToCentimeters(duration);
  return(cm);
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance traveled.
  return microseconds / 29 / 2;
}
long readyDistance()
{
  long duration, inches, cm;  //left sensor variable

  // The Ping is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(ping2pin, OUTPUT);
  digitalWrite(ping2pin, LOW);
  delayMicroseconds(2);
  digitalWrite(ping2pin, HIGH);
  delayMicroseconds(5);
  digitalWrite(ping2pin, LOW);

  // The same pin is used to read the signal from the Ping: a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off an object.
  pinMode(ping2pin, INPUT);
  duration = pulseIn(ping2pin, HIGH);

  // Convert the time into a distance.
  cm = microsecondsToCentimeters(duration);
  return(cm);
}

Where do you read xdistance and ydistance?

(maybe start a new thread, rather than hijacking someone elses)

im not sure i rely dont know much at all about this so are you saying that i need to tell it to read x and y distance before every statement?
do you think that an "if else" would be a better way to go?

im not sure i rely dont know much at all about this so are you saying that i need to tell it to read x and y distance before every statement?

Well, you need to read it at least once (hint).

You didn't like my suggestion about simplifying "readXDistance" and "readydistance"?

its not that i didnt like it i just didnt understand how to use it.

i just didnt understand how to use it.

You use it exactly how you use it at the moment - it was a drop-in replacement, with the advantage of less repeated code.
Or in the case of the code above, how you don't use it at all.

ok this compiled is this what you were talking about?

#define ping2pin      10      // left sensor
#define pingPin       9      // forward Ping ultrasonic sensor on pin D9.
#define leftDir1      8      // Left motor direction 1, to AIn1.
#define leftDir2      7      // Left motor direction 2, to AIn2.
#define rightDir1     6      // Right motor direction 1, to BIn1.
#define rightDir2     5      // Right motor direction 2, to BIn2.

#define BOUNDARY     50     // (cm) Avoid objects closer than 20cm.
#define INTERVAL     25      // (ms) Interval between distance readings.
#define limit        30
// setup
// Set motor pins as OUTPUTS, initialize Serial

void setup()                                                 
{
  pinMode(leftDir1, OUTPUT);        // Set motor direction pins as outputs.
  pinMode(leftDir2, OUTPUT);      
  pinMode(rightDir1, OUTPUT);      
  pinMode(rightDir2, OUTPUT); 
  Serial.begin(9600);
}


// Main program
// Roam around while avoiding objects.
// 
// Set motors to move forward,
// Take distance readings over and over, 
// as long as no objects are too close (determined by BOUNDARY).
// If object is too close, avoid it -- back up and turn.
// Repeat.

void loop()                     
{
  long xdistance;                    // Distance reading from front ping.
  long ydistance;                    // Distance reading from left ping
{                         // Robot moves forward continuously.
  if(((xdistance = readxDistance()) >= BOUNDARY) && 
                                      ((ydistance = readyDistance()) >= BOUNDARY)) {
    Serial.println(xdistance);       // Print it out.             
    delay(INTERVAL);                // Delay between readings.
    Serial.println(ydistance);
    delay(INTERVAL); 
  {
    forward();
  }
                                      }                                    
  else if(((xdistance = readxDistance()) >= BOUNDARY) && 
                                      ((ydistance = readyDistance()) >= BOUNDARY)) {
    Serial.println(xdistance);       // Print it out.             
    delay(INTERVAL);                // Delay between readings.
    Serial.println(ydistance);
    delay(INTERVAL);    // Loop while no objects close-by.
  {
    leftTurn(500);  
  } 
                                      }
 else if(((xdistance = readxDistance()) >= BOUNDARY) && 
                                      ((ydistance = readyDistance()) >= BOUNDARY)) {
    Serial.println(xdistance);       // Print it out.             
    delay(INTERVAL);                // Delay between readings.
    Serial.println(ydistance);
    delay(INTERVAL);
{
  rightTurn(20);
}     // Turn left 500 ms.

} // end Main program
} 
} // Robot has sensed a nearby object and exited the while loop.
  // Take evasive action to avoid object.          
      



// forward
//
// Move robot forward by setting both wheels forward.
// Will persist until something else changes the
// motors' directions.

void forward()
{
  digitalWrite(leftDir1, LOW);                    
  digitalWrite(leftDir2, LOW);
  digitalWrite(rightDir1, HIGH);     // LEFT motor forward.
  digitalWrite(rightDir2, HIGH);     // RIGHT MOTOR FORWARD.
}  


// backward
//
// Move robot backward by setting both wheels backward.
// Will persist until something else changes the
// motors' directions.

void backward()
{
  digitalWrite(leftDir1, HIGH);     // Left motor backward.
  digitalWrite(leftDir2, HIGH);
  digitalWrite(rightDir1, LOW);    // Right motor backward.
  digitalWrite(rightDir2, LOW);
}


// leftTurn
//
// Turn robot to left by moving wheels in opposite directions.
// Amount of turning is determined by duration argument (ms).

void leftTurn(int duration)
{
  digitalWrite(leftDir1, HIGH);     // Left motor backward.
  digitalWrite(leftDir2, LOW);
  digitalWrite(rightDir1, HIGH);     // Right motor forward.
  digitalWrite(rightDir2, HIGH);
  delay(duration);                  // Turning time (ms).
}
void rightTurn(int duration)
{
  digitalWrite(leftDir1,LOW);
  digitalWrite(leftDir2, HIGH);
  digitalWrite(rightDir1,HIGH);
  digitalWrite(rightDir2,HIGH);
}


// readDistance
// Take a distance reading from Ping ultrasonic rangefinder.
// from http://arduino.cc/en/Tutorial/Ping?from=Tutorial.UltrasoundSensor

long readxDistance()
{
  long duration, inches, cm;

  // The Ping is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the Ping: a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // Convert the time into a distance.
  cm = microsecondsToCentimeters(duration);
  return(cm);
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance traveled.
  return microseconds / 29 / 2;
}
long readyDistance()
{
  long duration, inches, cm;  //left sensor variable

  // The Ping is triggered by a HIGH pulse of 2 or more microseconds.
  // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
  pinMode(ping2pin, OUTPUT);
  digitalWrite(ping2pin, LOW);
  delayMicroseconds(2);
  digitalWrite(ping2pin, HIGH);
  delayMicroseconds(5);
  digitalWrite(ping2pin, LOW);

  // The same pin is used to read the signal from the Ping: a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off an object.
  pinMode(ping2pin, INPUT);
  duration = pulseIn(ping2pin, HIGH);

  // Convert the time into a distance.
  cm = microsecondsToCentimeters(duration);
  return(cm);
}

i know i didnt set the conditions i just wanted to work on the construction first.

It depends on how your actions are structured (how much delay between taking a reading and testing and performing an action based on that reading), but I'd be more inclined to read once.

void loop()                     
{
  long xdistance = readXdistance ();
  long ydistance = readYdistance ();

  if(((xdistance >= BOUNDARY) && 
                                      ((ydistance <= etc,

YMMV

ok i see let me give that a try.

ok how is this looking

void loop()                     
{
  long xdistance = readxDistance();                    // Distance reading from front ping.
  long ydistance = readyDistance();                    // Distance reading from left ping
{                         // Robot moves forward continuously.
  if(((xdistance >= BOUNDARY) && (ydistance <= BOUNDARY)&&(ydistance >= limit)))  {
    Serial.println(xdistance);       // Print it out.             
    delay(INTERVAL);                // Delay between readings.
    Serial.println(ydistance);
    delay(INTERVAL); 
  {
    forward();
  }
                                      }                                    
  else if((xdistance < BOUNDARY) && (ydistance > BOUNDARY)) {
    Serial.println(xdistance);       // Print it out.             
    delay(INTERVAL);                // Delay between readings.
    Serial.println(ydistance);
    delay(INTERVAL);    // Loop while no objects close-by.
  {
    leftTurn(500);  
  } 
                                      }
 else if((xdistance >= BOUNDARY) && (ydistance < limit)) {
    Serial.println(xdistance);       // Print it out.             
    delay(INTERVAL);                // Delay between readings.
    Serial.println(ydistance);
    delay(INTERVAL);
{
  rightTurn(20);
}     // Turn left 500 ms.

There are a number of issues with the code - why are right turn & left turn different - only one has a delay. Even so, the motors will be left in turn mode after the delay anyway, is that your intent? I'll reiterate an idea (AWOL?) from the other thread. Put your robot up on blocks and test your motor functions individually, preferably running them once, from setup. Make sure they're doing what you want & then gradually add the complexity of loop and the ping readings. You're making things harder for yourself by trying to do it all at once.