Go Down

Topic: How to write "then" (Read 956 times) previous topic - next topic


Ive had a look at the reference home page but cant find what I need, I want to write a code where one function followed by the other in sequence triggers another function. The two functions will not happen at the same they will happen one after the other. So in theory what i need is

Code: [Select]

if (LDRpin1 < 50 THEN LDRpin2 < 50) // If LDR1 falls below a value of 50 followed by LDR2 falling below a value of 50
digitalWrite(LEDpin1, HIGH) // if both happen in sequence light up LEDpin1


Ive had a look at the reference home page but cant find what I need

Because it doesn't exist.

You can use && (in place of THEN) to make the code syntactically correct, but that will do nothing about the comment.
The art of getting good answers lies in asking good questions.


You haven't really posted enough of your code to give you a very useful answer...

There's no built-in 'THEN' feature as you're describing.  You'll either need to track everything via state variables, or nest conditionals (if you know the two events will happen at the same time).

What does THEN signify in terms of timing?

Do you mean that both LDRpin1 < 50 and LDRpin2 < 50 at the same time?
Do you mean that any time in the next __ seconds after LDRpin1 < 50 when LDRpin2 < 50?
Do you mean any point ever, regardless of how long between pin1 and pin2?
Do you want the program to be able to do other things while waiting on pin2?
Or, should we deadlock until pin2 < 50?


Apr 19, 2012, 04:22 pm Last Edit: Apr 19, 2012, 04:24 pm by AWOL Reason: 1
If you're referring to this
Code: [Select]
int LDRpin1 = 0; //LDR1 in analog pin 0
int LDRpin2 = 1;
you posted earlier, you've got the problem that you're confusing the pin name with the value that you should be reading from that pin with an analogRead.

However, as has been pointed out, there is no concept of "after" or "then", and the test in your code(if they were syntactically correct) would be executed just a few microseconds apart.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.


If you are taking about --->  IF   THEN   

That is BASIC. In C / C++, no THEN.

Just :

Code: [Select]

if ( variable == 1 )
    // then do inside the braket

    // then else do inside this braket


If ... Then

What you are really asking would look like this in another language -

if condition1 then
   if condition2 then
       stuff for both conditions.
   endif  'condition 2
endif 'condition2

In C this would look like this -

if (condition1) {
   if (condition2){
      stuff for both conditions


if ( (condition1) && (condition2) ) {
    stuff to do for the conditions

The first form allows you to also trap for more secondary conditions.


Code: [Select]

// If LDR1 falls below a value of 50 followed by LDR2 falling below a value of 50

If you are trying to detect and handle sequences of events then a finite state machine is probably what you need.

Go Up