Figure out an if else

I have code from a website and I’m trying to make an if, else loop and i can’t figure out what specifically to identify. New to arduino and trying to get more acclimated. Here’s the code without the if else:

//Code for the QRE1113 Digital board
//Outputs via the serial terminal - Lower numbers mean more reflected
//3000 or more means nothing was reflected.

int QRE1113_Pin = 13; //connected to digital 2

void setup(){
Serial.begin(9600);

}

void loop(){

int QRE_Value = readQD();
Serial.println(QRE_Value);

}

int readQD(){
//Returns value from the QRE1113
//Lower numbers mean more refleacive
//More than 3000 means nothing was reflected.
pinMode( QRE1113_Pin, OUTPUT );
digitalWrite( QRE1113_Pin, HIGH );
delayMicroseconds(10);
pinMode( QRE1113_Pin, INPUT );

long time = micros();

//time how long the input is HIGH, but quit after 3ms as nothing happens after that
while (digitalRead(QRE1113_Pin) == HIGH && micros() - time < 3000);
int diff = micros() - time;

return diff;

}

Thanks

What do you want the program to do ?

if something is true
  carry out action 1
end of if
else
  carry out action 2
end of else

What is the something that can be true or false ?
What are actions 1 and 2 ?

Please don't post the same question in multiple places, i.e crosspost - post once, have a little patience.

UKHeliBob:
What do you want the program to do ?

if something is true

carry out action 1
end of if
else
  carry out action 2
end of else



What is the something that can be true or false ?
What are actions 1 and 2 ?

I am trying to turn on solenoid valves with the program, basically whenever the IR sensor has some type of obstruction, it will turn the valve on.

This should be unsigned long

long time = micros( );

Do you you ever need this pin to go Low?

digitalWrite( QRE1113_Pin, HIGH );

The code I sent is from the website I got this from. I am trying to figure out how to label the if else statement.
Kind of like this one, but less than 3000, but I can't figure out the first part.
if (pinFiveInput > 3000)
{
// action A
}
else
{
// action B
}

if (pinFiveInput <= 3000) // less than or equal to 3000

I don't know what to label the "pinfive" part. That was just an example, I am trying to figure out what to put in its place based on the IR sensor code.

I think you are asking for something like this;

// if analog input pin no 5 (a5) value is less than/equal to 3000
if (analogRead(A5) <= 3000)
{
  // action A
}
else
{
  // action B
}

but a word of warning, it will always be less than 3000, the analog values on Analog inputs only go to 1023 max (they are 10 bit a-d converters & 10 bits = 1024)

if (pinFiveInput > 3000)
{
  // action A
}
else
{
  // action B
}

In this code "pinFiveInput" is the name of a variable holding some value. I have no idea what range of values it might have but that piece of code obviously assumes it could be higher or lower than 3000. Also I have no idea how the value was put into that variable.

Looking at your original code it seems there is data being obtained from some external device - whatever that QRE thing is. It seems to be a digital rather than an analog device so I don't think analogRead() s relevant. Can you post a link to its specifications?

...R

...R

Here's the webpage I got the sensor from, there a multiple links, including the place where I got the code. SparkFun Line Sensor Breakout - QRE1113 (Digital) - ROB-09454 - SparkFun Electronics
What I am trying to figure out is how to communicate to valves to turn on when this operates. Basically this device says nothing is being reflected at over 3000, hence why I wanted it to be less than 3000, because I want any obstruction to turn on the valves. So the original code doesn't have an if/else statement. I was just posting an example. Pinfive wasn't written by me. I wanted to know what to put in pinfive's place on the if/else statement based on the original code that makes the IR sensor work. The way the system should work, is that whenever there is something causing an obstruction in the IR sensor's path, then the valve will turn on, if nothing is in it's path then the valve will be off. I had it on Basic, but the sensor didn't want to cooperate so I am trying to do arduino. Sorry if this seems complicated, it's hard for me to jump from one type of coding to the next. But I appreciate your help.

In the example code these 2 lines and the readQD() function put a value in the QRE_Value variable and print it.

  int QRE_Value = readQD();
  Serial.println(QRE_Value);

It is the QRE_Value variable that you need to test the value of to determine whether something was detected or not. Try printing it as in the example code to see what values you are actually getting and when.