Pages: [1]   Go Down
Author Topic: How to write "then"  (Read 596 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49092
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pennsylvania, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

i.e.
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?

Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26249
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you're referring to this
Code:
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.
« Last Edit: April 19, 2012, 09:24:24 am by AWOL » Logged

"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.

Toronto, Canada
Offline Offline
Edison Member
*
Karma: 2
Posts: 1234
"Keep it R.E.I.L. - "Research, Experiment, Investigate and Learn"
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are taking about --->  IF   THEN   

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

Just :

Code:
if ( variable == 1 )
{
    // then do inside the braket
}

else
{
    // then else do inside this braket
}

Logged

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1025
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
   }
}

or

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

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// 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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: