# Thread: Switch concept and growing array ?

1. ## Switch concept and growing array ?

Hello all,

After some coding I got an output of two variables. Usually, the output is as follows:

X: 0, 0, 1, 1, 0
Y: 0, 1, 1, 0, 0

or

X: 0, 1, 1, 0, 0
Y: 0, 0, 1, 1, 0

In short, the important change is when both are 0, 0 and 1, 1 together and either is 0, 1 or 1, 0. At this points the decision are about to be made. For example, when 0, 0 it is either open or close, indeed, if it is open, then either follows the 0, 1 or 1, 0. It never follows the 1, 1. After , 1, 0 or 0, 1 follows 1, 1 and after follows the opposite of what follows before the 1:1. So, if first is 1:0, ( 1:1 ) then it will be 0, 1. It always end or start with the 0, 0.

In short someone could code this as a simple pattern, for example, of few words, like: a(0, 0), b(1, 0), c(1, 1), d(0, 1), and again a(0, 0). So the pattern could easily be deciphered as : a, b, c, d, a or a, d, c, b, a. However, those deciphers repeat, for example, there could be 10 or 2... of a or b, or c, and so on. The number is occurring random, however, in most cases it boils down to 100 for the whole decipher... but nor necessary .

I was thinking to go around this with a several ifs and a growing array aka lists, etc. however, I have found several questions. For example, is the decipher a and c even necessary ? For example, is it not enough to know when the b and d occur to know what occurred in the given decipher. For example, the plan is to make a particular decisions on b and d! But, as you can see there is several of those that occur...

So, thinking to use.
Code:
` If(b){ if I am in switch, make a decision and break }. Make the same for the if(d) ...`
Would that work, would that be a good idea to go around or is there something else that would be more appropriate to tackle this? Indeed, please comment as well on problems that I do not yet see.

best.

PS indeed, the state of variables x,y are changing random, but quickly and the dynamic never stops, its endless.

2. Your question is just meaningless babble to me.

3. The thing you are talking about is pretty common in lots of areas... Motor encoders, Gray counters, QPAM modulation, rotary encoders..

You have a sequence of pairs.

(0,0)->(0,1)->(1,1)->(1,0)->....
or
(0,0)->(1,0)->(1,1)->(0,1)->....

There are many cunning ways to deal with it - you just need to know what the current pair is, and what the last pair is.

However, here's an even easier way. Just XOR the second bit with the first bit.. that turns the sequence into:

(0,0^0)->(0,1^0)->(1,1^1)->(1,0^1)->....
(0,0^0)->(1,0^1)->(1,1^1)->(0,1^0)->....

And these simplify down to :

(0,0)->(0,1)->(1,0)->(1,1)->....
(0,0)->(1,1)->(1,0)->(0,1)->....

And if you look closely you may see that it is a binary up / down counter:

00->01->10->11->....
00->11->10->01->....

4. Hello Hamster_nz,
thanks for your input, I think I understand what you are saying, however, I am not sure, how should that help to decipher the pairs?
Best.

5. Hey John,
sorry, I will soon post a more readable c code for you to engage
Best.

6. Hello both,

I have put some bits of code together, it is a simple if sentence structure.

Code:
```// 1) Starting the sequence for "left gesture" ?
if (Data2 > HT && Data3 < HT) {
printf(" left step 1) \n ");
printf("Data2: %d Data3: %d\r\n", Data2, Data3);
// 2) continue the sequence with looking for the center gesture
if(Data2 > HT && Data3 > HT) {
printf(" left center step 2) \n) ");
// we have reached the center of gesture
// 3) continue with looking for a conclusion, namely left gesture?
if (Data2 < HT && Data3 > HT) {
printf(" left 1:1 step 3) \n");
// we have found the concluding gesture, namely left in this case
// 4) continue with the final gesture in sequence to deliver the actuation
if (Data2 < HT && Data3 < HT) {
printf(" left 0:0 step 4) \n");
// we have come to the end of the left gesture sequence!
//print out !
printf("\r LEFT %d myCount: %d\r\n", left, right, myCount);

// ON/OFF the leds!
// turn on the yellow led

// turn off the white led

}
}
}
}```

could this be a way to go?

Best.

7. ... and this is another demo...

Code:
```       /*
* my 1st demo
*/

if (true) {
// the light is off
my_switch_1 = false;

// left?
if (Data2 > HT && Data3 < HT ) {

if (Data2 > HT ) {

my_led_left = true;
my_led_right = false;

left = true;
right = false;

start = false;
end = false;
myCount++;

printf("\r LEFT  my_led_left: %d my_led_right: %d left: %d right:  %d myCount: %d\r\n", my_led_left, my_led_right, left, right, myCount);

// turn on the leds
if (left ) {

// turn on the yellow led

// turn off the white led

}
}
}

// right
if (Data3 > HT && Data3 > HT) {

if (Data2 < HT ) {

my_led_left = false;
my_led_right = true;

left = false;
right = true;

start = false;
end = false;

myCount++;
printf("\r RIGHT  my_led_left: %d my_led_right: %d left: %d right:  %d myCount: %d\r\n", my_led_left, my_led_right, left, right, myCount);

if (right) {
// turn on the white led
// turn off thw yellow led
// break;
}
}
}

// end of my 1st demo
}```

This is one rather works, however, there is a problem between switching the leds, for example, for example the idea is to if the "left" repeats twice than for example the light should go off, same idea for the right concept. I am thinking to use the state machine, like enumerator... to follow this. What do you think, so far, the off was not implemented, not really sure how to go about. Any ideas?

Best.

8. > PS indeed, the state of variables x,y are changing random, but quickly and the dynamic never stops, its endless.
...
> Then you start talking about gestures, which are rather non-random.

Neither sample of code is meaningful.
> if (Data2 > HT && Data3 < HT )
How are these declared?
How are they changed?
Data and HT are meaningless names that tell us nothing.

It all seems like The XY Problem

9. Hello Salem and thank you for your input.
The data2 and data3 are so called output that is derived on basis of gestures.
So, they are given above, as you said, they are random, however, usually, they follow the pattern as given above.
HT is a variable that is not important in this case, so, it could as well be if Data2 is true, it just compares that value of Data2 or Data3... lets say that HT = 0.9, so, if Data2 = 1 is above HT, else below. I guess this is what you needed to know.

In general, there is a state machine that produces the number as given above and the idea is to explicitly capture what is give.
So, I was thinking to put everything that is above HT into an array and at the end check that array and accordingly make a decisions. However, as we both know that array could be big, hence it would be beneficial to check the pattern without storage, if possible. However, in most cases the length of the pattern is around 50 , so, the size of the array could as well be 100, and if it is longer patter it would just ignore what ever is there ...

Anyway, if you look at the last demo that I have posted, this is a way how to do that with a simple logic check, however, as you can see in the output data, during that logic the data as well flips before it ends. For example, have a look at the above reply from Hamster_nz:

You have a sequence of pairs.

(0,0)->(0,1)->(1,1)->(1,0)->....
or
(0,0)->(1,0)->(1,1)->(0,1)->....

But there is a so-called input/output pair that could be leveraged, like 00, this usually occurs either at the beginning or end of each decipher and in the middle there is usually (1, 1)

So, my question is how to simply deal with this 4-bit problem? Of course I can always try to put the values in an array and skim at the end, and then make a decision, however, could not that be already done while I am obtaining data first data, indeed, some data may not be useful, however, would not the function of some kind of ignore help here and eventually capture only the needed data to make a decision?

Best.