sensor Direction dtermination

Hey everyone, i'm working on a project where, i use 4 piezo electric sensors placed in series,i would want to know a way of determining the direction in which the sensors are triggered, i use a certain threshold to determine if the sensors are triggered.(the analog values are converted to voltages on a scale of 0 to 5)

if the sensors are triggered in the order (s1->s2->s3->s4) it should display as forward direction if the sensors are triggered in the order (s4->s3->s2->s1) it should display as backward direction

code:

if (s1>0.0) { if (s2>0.0) { if (s3>0.0) { if (s4>0.0) { Serial.print("DIR:FORWARD"); } } } }

if (s4>0.0) { if (s3>0.0) { if (s2>0.0) { if (s1>0.0) { Serial.print("DIR:BACKWARD"); } } } }

will this work ?

No...

1/ you don't read the values of the sensors, so there is no chance that works 2/ if you read them before your series of if then basically your cascading "if" are just a test if ALL are > 0, not the order in which they have been triggered, so both expressions are identical. 3/ are you sure your sensor is 0 when not touched? 4/ is your sensor returning a float? why do you compare to 0.0?

You need a state machine for doing this type of things

This is just the part of the loop that used to determine the direction ,i just removed this part to ask if this loop will function as described.

yashz123:
removed this part to ask if this loop will function as described.

J-M-L:
No…

You need a state machine for doing this type of things

You could approach that quite easily with a bunch of booleans, one for each possible state.

Basically you need to sit in some kind of idle state, poling the sensors. If you get a trigger from sensor 2 or 3 at idle, ignore them. If you get a hit from sensor 1, you go to state say 1A. In 1A, the only valid hit is sensor 2, which takes you to 2A. In 2A only a 3 is valid, to go to 3A; from there only a hit on sensor 4 is valid taking you to state 4A, a valid end state.

If at idle you get a hit from sensor 4, you go to state 4B, from which a hit on sensor 3 is the only escape… etc etc.

You need to differentiate between the 2 ways to get a hit on sensor 4: 4A means you got to an end via 1-2-3-4 and 4B means you’re starting a downward trip from idle.

If you ever get to 4A that means you completed the path going up, and if you ever get to 1B you went down.

Valid paths are thus:

Idle-1A-2A-3A-4A

Idle-4B-3B-2B-1B

edit: and presumably once you did whatever you were supposed to do when you got to an end state, you would go back to idle for next time. I would probably rename the states to indicate direction, rather than A and B

Idle-1goingUP-2goingUP-3goingUP-4End-Idle

Idle-4goingDOWN-3goingDOWN-2goingDOWN-1End-Idle

Doing this with booleans and ifs is a simple way. There are other ways of course, and in this thread PaulMurrayCbr shows how you could do that with classes and objects: probably not too n00b friendly though.