Rotary H1 Module gives strange values

On some random occasions this module gives me incorrect values. I am using the following code:

Dim prevCount, counts As Integer

Do
	counts = rotaryH1.GetCount()
	If prevCount <> counts Then
		Debug.Print("Prev: " & prevCount & " current: " & counts & " result: " & (prevCount - counts))
		prevCount = counts
	End If
Loop

Here are two captures:

[quote]Prev: 49 current: 57 result: -8
Prev: 57 current: 59 result: -2
Prev: 59 current: 62 result: -3
Prev: 62 current: 63 result: -1
Prev: 63 current: 61 result: 2
Prev: 61 current: 43 result: 18
Prev: 43 current: 30 result: 13
Prev: 30 current: 22 result: 8
Prev: 22 current: 11 result: 11
Prev: 11 current: -32764 result: 32775
Prev: -32764 current: -4 result: -32760
Prev: -4 current: 0 result: -4
Prev: 0 current: 12 result: -12
Prev: 12 current: 21 result: -9
Prev: 21 current: 23 result: -2
Prev: 23 current: 30 result: -7

Prev: 36 current: 33 result: 3
Prev: 33 current: 34 result: -1
Prev: 34 current: 38 result: -4
Prev: 38 current: 41 result: -3
Prev: 41 current: 39 result: 2
Prev: 39 current: 33 result: 6
Prev: 33 current: 32 result: 1
Prev: 32 current: 22 result: 10
Prev: 22 current: 19 result: 3
Prev: 19 current: 16 result: 3
Prev: 16 current: 6 result: 10
Prev: 6 current: 2 result: 4
Prev: 2 current: -32765 result: 32767
Prev: -32765 current: -9 result: -32756
Prev: -9 current: -14 result: 5
Prev: -14 current: -13 result: -1
Prev: -13 current: -10 result: -3[/quote]

Value of 32767 does look suspicious. Can anyone provide any explanation why I am getting this?

Btw, same happens without using Debug.Print.

The module is running in 16bit mode so once it gets to 32768 it goes to 0

1 Like

Looks like an overflow issue. I would get the driver source code and check there.

Yes, it only happens when .GetCount is around 0. It does not happen if .GetCount is, for example, at 1000.

You need to handle when the count gets to max and min

@ Bill Gates - But that is not max or min, that is in the middle. I think Gadgeteer driver should not give this type of result.
I have looked at its driver and I am pretty sure that if statement is the cause of this issue.

public int GetCount()
{
	int count = this.Read2(Commands.LS7366_READ | Commands.LS7366_CNTR);

	if ((this.GetStatus() & 0x01) > 0)
	{
		count = ~count;
		count &= 0x7FFF;
		count *= -1;
	}

	return count;
}

I must be blind - well it is monday ::slight_smile:

Where is the issue?

Get count returns the current count from the LSI chip which is a INT16

So turning it anti clockwise once you get to zero it will be -32768

No.

[quote]
Prev: 0 current: 1 result: -1
Prev: 1 current: 2 result: -1
Prev: 2 current: 3 result: -1
Prev: 3 current: 4 result: -1
Prev: 4 current: 3 result: 1
Prev: 3 current: 2 result: 1
Prev: 2 current: 1 result: 1
Prev: 1 current: 0 result: 1
Prev: 0 current: -1 result: 1
Prev: -3 current: -4 result: 1
Prev: -4 current: -5 result: 1
Prev: -5 current: -4 result: -1
Prev: -4 current: -3 result: -1
Prev: -3 current: -2 result: -1
Prev: -2 current: -1 result: -1
Prev: -1 current: 0 result: -1
Prev: 0 current: -1 result: 1
Prev: -1 current: 0 result: -1
Prev: 0 current: 1 result: -1
Prev: 1 current: 2 result: -1
Prev: 2 current: 3 result: -1[/quote]

It only happens if I turn more than once in one go. If I turn it one-by-one it works fine.

Dunno then - call the experts :smiley:

@ John, ring ring… :whistle:

@ iamin - If you change the function to the one below, does it work as expected for you?


public int GetCount()
{
	return this.Read2(Commands.LS7366_READ | Commands.LS7366_CNTR);
}

@ John - Yes, it does the trick.
While you at, maybe you could add ResetCount method that is already included in the Pulse Count module?

@ iamin - It’s been added.

1 Like