Sequence detection using arduino

Hello,
I want to know how to detect the sequence of the input given to arduino.
Like if 3 LEDs on and off in sequence I want to check whether the LED turned on in required sequence.
Or if I given 3 inputs to the Arduino, I want to check whether the given input is in required sequence of order or not.
example if three three sensors are placed linearly, If I activate those in order wise Program should tell whether it activated orderly or not .
Please anybody respond soon.

Pretty easy…
Probably you might define an array of the input pins in the required order, and any ‘per-pin’ timing specifics if necessary.

Then , Once you detect the first input, start a millis() timeout, waiting for each remaining inputs to be asserted.

The result is up to you.

If you need more help, post your code (in code tags),:and I’m sure someone will help push you along.

@dileepchandrus, your topic has been moved to a more suitable location on the forum.

Sorry, I didn't get your idea.
what exactly I want to do is I have 3 hall sensors which output signal is either on or off depending on presence of magnetic field.
If i placed them in a sequence and move the magnet in sequence, program should return pass or else in other case like in unorder movement of magnet it should return failed.
Please help me out to solve this easily.

If you make a start, we’ll gladly push you along…

Let’s forget pin timing at the moment…
Just detect inputs on pins 2, 5, and 3 in order…

The basic idea…. something like this…


Int currentPinIndex = 0; // start with the first input pin
int numPins;

byte inPin[] = {2 ,5, 3};  // the order of input pins

void setup() {
  numPins = sizeof(inPin);
}

// in your loop…
if (currentPinIndex < numPins) {
  // test if inPin[currentPinIndex] goes true…
  // do something
  currentPinIndex ++;
} else {
  // currentPinIndex has reached the last pin…
  // do the important thing….
  currentPinIndex = 0; // reset the input counter
}

This just example pseudo code… give it your best shot.

1 Like

Thank you very much @lastchancename for helping me getting to start.
I will start working on it.

Looking for more solutions Please.

Hi,
Welcome to the forum.

Have you built your project?
Can you post a circuit diagram?
A photograph of a hand drawn diagram would be fine.
Can you please post link to data/spec of the hall devices?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

I don't think you are. I think you are looking for a simple solution you can understand. The thing is that this is not as simple to solve as you might think. Especially if you want to change the sequence rather than have a code that will always follow a single one.

To properly solve this problem you must draw a state diagram. This shows all the states that the inputs can be in. Each state is given a number based on the binary number given by the inputs. The diagram shows each state you can be in and what transitions you need to drive the state towards the end. It must also cover what happens when every possible transition occurs, for example does the state remain the same or does it reset or does it go back to the beginning?

Once the state machine is drawn then this gives you a template that you can write your code from. In other words you then have enough information to make a state machine in software.
A few months back I had to do this for a rotary encoder, this generates two signals called clock and data. Together these two signals give four different combinations which I labelled 0 to 3. It had to detect a set of transitions to determine if the encoder was moved clockwise or anticlockwise and update a counter to reflect this. This is what my state diagram looked like.
State Machine

It could be that you need a bit more experience with coding before you can tackle a problem like this.

Hi,
Thanks for responding.
Please find the attached image.

Hi,
Thanks for the basic diagram.
Have you got some code that firstly tests that the Hall Effect devices are working?

What is your output that indicates the sequence is correct?

Can you please tell us your electronics, programming, arduino, hardware experience?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

No, I don't have any code I am searching for that only.
But when i Read each sensor value separately , when there is no magnet near it , it will return 0 and when we brought magnet near to it its output will be high.

Thanks for responding :hugs:

Hi,

Can you please tell us your electronics, programming, arduino, hardware experience?

Tom.... :smiley: :+1: :coffee: :australia:

can you start at the beginning ?

it looks like you want to detect if something spinning, is spinning clock wise or counter clock wise.

Since the sample that lastchancename offered was not working for you, I would guess that you are very new and just starting out.

we were all there once, so no problem with that. but if you answer post 18, we will have a MUCH better chance to give you help that is on your level of experience.

This. It would help to know what you're trying to make and why.

WARNING: If the position starts between 2 and 3, the sequence can never match since there is no way to get to 1 without going past 2 or 3 first.

const byte HallInputPins[] = {3, 7, 9};
const byte HallCount = sizeof HallInputPins / sizeof HallInputPins[0];
const byte Sequence[] = {0, 1, 2};
const byte SequenceCount = sizeof Sequence / sizeof Sequence[0];
byte SequenceIndex = 0;

const byte PassOutputPin = 5;

void setup()
{
  for (byte i = 0; i < HallCount; i++)
    pinMode(HallInputPins[i], INPUT);
  pinMode(PassOutputPin, OUTPUT);
  digitalWrite(PassOutputPin, LOW);
}

void loop()
{
  for  (byte i = 0; i < HallCount; i++)
  {
    if (digitalRead(HallInputPins[i] == HIGH))
    {
      if (i == Sequence[SequenceIndex])
      {
        SequenceIndex++; // Correct Match
        if (SequenceIndex >= SequenceCount)
        {
          // All sequence steps complete.
          digitalWrite(PassOutputPin, HIGH);
          for (;;);  // Infinite loop
        }
        // Wait for the matched iput to go LOW
        while (digitalRead(HallInputPins[i]) == HIGH) ;
      }
      else // DOES NOT MATCH SEQUENCE
      {
        for (;;); // Infinite loop (PassOutput stays LOW)
      }
    }
  }
}

Hello,
when I use digital read to read hall sensor out data it will read 0 if no magnet is present and 1 if there is magnet.
Solution I am looking for is If I move the magnet from sensor1 to sensor2 and then to to sensor 3 My test should pass or in any other cases like 3 to 1 then to 2 , 2to 1 then to 3,etc it should return fail.

Hi,
Have you written ANY code?
If not, write some code just to check that your hall effect devices can be detected by the UNO?

Can you please post link to data/specs of the hall effect devices?

Tom... :smiley: :+1: :coffee: :australia:

int HallState1; //Variables for the three hall sensors (3,2,1)
int HallState2;
int HallState3;
void setup() {
  // put your setup code here, to run once:
    pinMode(2,INPUT);    // Hall 1
    pinMode(3,INPUT);    // Hall 2
    pinMode(4,INPUT);    // Hall 3
    Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  HallState1 = digitalRead(2);  // read input value from Hall 1
  HallState2  = digitalRead(3);  // read input value from Hall 2
  HallState3  = digitalRead(4);  // read input value from Hall 3
  digitalWrite(8, HallState1);  // LEDs turned on when corresponding sensor is high
  digitalWrite(9, HallState2);
  digitalWrite(10, HallState3);

    Serial.print("H 1: "); // prints 1 when corresponding sensor is high  & 0 when corresponding sensor is low
  Serial.println(HallState1);
  Serial.print("H 2: ");
  Serial.println(HallState2);
  Serial.print("H 3: ");
  Serial.println(HallState3);
  delay(1000);
}

This code is just to test working of it I used can u suggest any other program or circuit to test the sequence of movement of magnet.

If you use #1 as the starting point,
Then there are 4 possibilities.
#1. Nothing else resopnds
#2 dither, the #1 goes off and on and no other sensor responds.
#3 sensor #2 responds
#4 sensor #3 responds

Assuming you don't care about #1 or #2 you can look at the next possible outcome.
Assume sensor #2 responds, there are again 4 possibilities.

Assuming you only care if the next one is sensor #1 or sensor #3.

If sensor #1 is a reset, then the only outcome you need to address is when sensor #3 responds.

To simplify.
We can say there are two outcomes you are expecting
1 2 3 1 2 3 1 2 3
Or
1 3 2 1 3 2 1 3 2

If you reset the results with every time sensor 1 responds, then you have
Reset 2 3 reset 2 3
Or reset 3 2 reset 3 2

This can be done in a Couple of if() statements.

By watching millis() you can say if nothing in 1 seconds then not moving.

This is very simple but it might help you understand and make the next step.