counting a turn

I got this hall effect sensor, i could not decide how to measure a turn, where magnet goes from 359 degree to 0,

the values of 0 from sensor is around ~400
and for 359 it is ~3600

My logic for this was

void loop(){

Serial.print("  ");
Serial.print("  ");
delay(1000);
if (mpos_first >= 3600 && mpos_second <= 500){

Serial.println("a turn_foward");
}
if (mpos_first <= 500 && mpos_second >=  3600)  // thanks Msquare for saying this is wrong

{

Serial.println("a turn_backwards");

}

~more code
.
.
.
.
.
.
mpos_first=mpos_second;

} // - void loop()

my problem is that i cant check this code, so i need to know if this logic is right and if not, how should i fix it. Tryed to google for examples but did not find.

Can you just re-read what you have written and see if it makes any kind of sense to you, then re post it?

Hall sensors usually only indicate the presence or absence of a magnetic field, not the degree of rotation.

Posting all your code would be useful too. Use the code tags to make it easier to read. Use the # icon on the editors toolbar

Your code might work under the correct conditions of your hardware - ie we do not turn to fast (your delay(1000) limits us to less than 1deg/second) or too slow. I presume a magnet is attached to shaft that sweeps it past the sensor.

if (mpos_first >= 3600 && mpos_second <= 500){

correctly test a “rising edge”, but it has to be very sharp (ie rise from 500 to 3600 in one second). I presume it will never be “slow”, where the “mpos_second” might be 2000 and mpos_first is 3600.

Your second test makes no sense to me - if you wanted to test the “opposite” of the the first test it should be

if (mpos_first <= 500 && mpos_second >= 3600)

On a side note: Your if statments have a syntax error - you do not want the “;” inside the if-brackets. Seriously - why should we bother looking at your code logic if you have not even taken the effort to check for syntax? And what is this:

my problem is that i cant check this code yet

The sensor is based on Hall effect technology and detects the position from a rotating magnetic field.

And my bad i had done few mistakes in the code, i was in a hurry when i was translating the code from one language to english. I did not want to copy the code (because it is 150+ lines long and mostly in estonia with lots of comments), so i shorten it up and made mistakes while doing it. :blush: ...

I tested out that sensor really has a "sharp edge" from 359 degree to 0 degree.

But how do I check it then when values ??do not jump so sharply? Do i use array for it?( i just want to know that, for future projects)

Lets say that the maximum value is 3600 (359 degrees) and the minimum value of 400 (0 degrees), but for some reason sensor gives out intermediate values?? too, then how do i check the transition from 359 degrees to zero? I tried to google for examples but i did not find.

AWOL: Hall sensors usually only indicate the presence or absence of a magnetic field, not the degree of rotation.

Hall sensors detect field strength perpendicular to one axis of the sensor. They are directional.

matukaz:
But how do I check it then when values ??do not jump so sharply? Do i use array for it?( i just want to know that, for future projects)

Lets say that the maximum value is 3600 (359 degrees) and the minimum value of 400 (0 degrees), but for some reason sensor gives out intermediate values?? too

It is an analog input you are measuring, not a digital angular position, so it is expected that intermediate values are seen when you turn the shaft slowly enough. What is the expected range you need to count on, ie what is the slowest and fastest speed you must be accuarate~~, and how often does direction change~~? In otherwords, tell us a little of what you are measuring.

Thinking belatedly about it - you can not detect direction with one sensor. Say you turn to 359 and then reverse - the sensor can not tell if the “falling” edge is due to moving away from the “peak value” clock- or anti-clockwise (unless the signal is highly asymetric and the speed is only slow changing).

The way it is now, you can make a algorithm that watches for a “peak” - irrespective of speed - and then count one. Direction you have to assume or get from another input.

Arrays consume precious RAM and we only need to keep track of a high and a low. In pseudo code:

If Value > biggest seen - update biggest seen.
If value < smallest seen - update smallest seen
If biggest seen >3000 and smallest seen < 500