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(){
 
  

      readvalue = read_adc(1);
      Serial.print("  ");
      Serial.print(readvalue,DEC);
      Serial.print("  ");
      magnetpos=readvalue;
      delay(1000);
      mpos_first=readvalue;
  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:

ReadAnalog()
If Value > biggest seen - update biggest seen.
If value < smallest seen - update smallest seen
If biggest seen >3000 and smallest seen < 500 
  add one to count,
  reset biggest seen and smallest seen values to 1500

This will do the count when we go past the 359 mark or the 0.5 mark or the 357 mark - depending on the starting condition and direction, but will count one for each revolution. (I think.…)

Right you are when it comes to full turns, MSquare. It'd probably be best to have two Halls oriented 90 degrees to each other.

Btw, it also takes two optical sensors to do the same thing.

Would it be faster to measure the sensors directly via analog (100 usec) or possibly as part of a circuit timing charge or bleed of a digital input (loop and count)? I guess that would have to sacrifice resolution for speed as to prepare the pin (HIGH or LOW) then read the pin and break on condition or loop up to 1023 times might take the 100 usec anyway but one could set it up so the read only takes 360 or 180 loop-through at most with a possibly shorter pre-set time for faster, lower resolution reads. Why? So the shaft can turn faster and still be read in less than 90 degree of turn.

OTOH with two latching Hall sensors placed outside the turning magnet circle, 90 degrees apart it would be easy to get RPM data. Resolution would be poor but the magnet could turn awfully fast and still read just fine.

A hybrid that could get the best of both would only take 2 latching sensors (on digital pins) and 1 linear sensor (on analog pin), right? At 1000+ RPM, is precise instant to instant direction going to matter? If so then get the mirrors and laser!