Accurately read the state of a square wave signal - Hall sensor

I am trying to control the number of revolutions of a motor with an Arduino (Uno R3). The motor has a hall sensor which produces a square wave - 2 pulses per revolution at about 70 Hz.

I want to read these pulses and use them to control the motor. Ie if i have read 30 pulses, the motor must switch off and await further instruction.

I have tried various ways of doing this, but my results don’t match my oscilloscope (Picoscope 2204A). Sometimes I am within about 5 pulses and other times only about 40-50. My range of pulses is from 30-100 so these are fairly significant numbers.

I have attached 3 different ways I have tried to solve the problem (as well as the time and DS1307 RTC libraries and the motor data sheet). The only difference between each method is in the section called “My code…” in the main loop.

My results also seem to vary as I change the motor current (ie changing motor rpm). I don’t know if I am reading the pin too slowly or ,heaven forbid, this is beyond the capability of the arduino.

Any help would be greatly appreciated! Thank you!

Forum_code_3.ino (23.2 KB)

Forum_code_2.ino (22.2 KB)

Forum_code_1.ino (22.5 KB)

Time.zip (25.2 KB)

DS1307RTC.zip (5.02 KB)

PE3C-62-R-24H.30.33-REV.C.pdf (165 KB)

I have been looking at the Encoder library which I think could possibly work, although I haven't figured it out yet.

Please use the </> option to include code.
Thanks
Dwight

Are you waiting for both positive and negative changes
to see edges?

Is this a simple DC motor? ie you switch it on, wait for the required number of rotations then switch it off?

If you find your pulse counts are lower the faster your motor is going then its likely you are missing some pulses.

Have you considered using interrupts to count the pulses from the Hall effect sensor rather than pulseIn()? You can set up the arduino to call an interupt service routine every time a pin changes state- usually this routine simply increments a counter or sets a flag. This happens regardless of what else your program is doing at that time making it much less likely that you miss a pulse. The main program then loops around checking the counter until it gets to a certain value. See http://playground.arduino.cc/Code/Interrupts and Gammon Forum : Electronics : Microprocessors : Interrupts for more info and examples.