calculating revolution per sec using hall effect sensor for POV display

hello everyone; i am making a cylinder POV display for my school project and i need to work out the revolution per second of the cylinder. i plan to use the serial port to display the speed using a computer. i am using a A3144 Hall effect sensor connected to the Micorduino's A1 (analogue pin 1) and have tried many variation of sketches and keep failing to get the program to work. but i do know the harware works because i tested it with this sketch i found, and it works fine

int sensorPin = 2;
int counter = 0;
boolean sensorState = false;

void setup() 
{
  // setup serial - diagnostics - port
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);
}

void loop() 
{
  if(magnetPresent(sensorPin) && !sensorState)
  {
    sensorState = true;
    printMessage("Magnet Present");
  }
  else if(!magnetPresent(sensorPin) && sensorState)
  {
    sensorState = false;
    printMessage("Magnet Gone");
  }
}

void printMessage(String message){
  counter++;
  Serial.print(counter);
  Serial.print(" ");
  Serial.println(message);
//  delay(1000);
}

boolean magnetPresent(int pin){
  return digitalRead(pin) == LOW;
}

here is one of the sketches that i wrote and did not work:

int rpm = 0;//not really RPM but revolution per second
int i = 0;
long previousTime = 0;      
long oneSec = 1000;   

void setup()
{
  Serial.begin(9600);
  pinMode(A1, INPUT); //I did not bother declaring the A1 sensor pin
  digitalWrite(A1, HIGH);

}

void loop()
{
  
  unsigned long currentTime = millis();
  
  if (digitalRead(A1)== LOW)
    {
     ++i;// I also tried i = i + 1 and i++
    }
  
  if(currentTime - previousTime > oneSec) {
    previousTime = currentTime;
    Serial.print("RPM: ");
    Serial.println(rpm);
    rpm = 0;
    
  }
}

i forgot what results i got for this sketch(sorry for the lack of detail), but when i did get results from other sketches what printed out was not the 2 or 3 i was looking for but numbers that were long and 5-6th place e.g. 16550

this is a sketch i retyped again when writing up this post. it is different form the previous sketch in logic, meaning i have not tested it yet. but from other 'similar' sketch i keep getting 0 for the speed and it seems like there is a problem with the calculation.

const byte sensor = A1;
long time1 = 0;
long time2;
long RevPerSec;

void setup(){
Serial.begin(9600);
pinMode(sensor, INPUT);
digitalWrite(sensor, HIGH);
}

void loop(){
  if (digitalRead(sensor)== LOW)
{
time2 = millis();
RevPerSec = 1UL / (time 2 - time 1);//not sure what format 1 should be in
Serial.print(RevPerSec, long);
time1 = time2;//set time1 to the current time at that instance
}
}

please tell me all the horrible mistakes i made. i appreciate the help form people(if i get any) and apologies for the lack of proper test results. It would be nice if someone can tell me a better way to figure out the revolution per second for the POV display.

not your only problem but what are you doing here?

pinMode(sensor, INPUT);
digitalWrite(sensor, HIGH);
}

void loop(){
  if (digitalRead(sensor)== LOW)

you set the pin high and wait for it to go low?

ditch this:

digitalWrite(sensor, HIGH);

I’d use an interrupt for this…

To be honest i don't know what i am doing, it appeared in someone else' sketch:

int sensorPin = 2;
int counter = 0;
boolean sensorState = false;

void setup() 
{
  // setup serial - diagnostics - port
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);
}

void loop() 
{
  if(magnetPresent(sensorPin) && !sensorState)
  {
    sensorState = true;
    printMessage("Magnet Present");
  }
  else if(!magnetPresent(sensorPin) && sensorState)
  {
    sensorState = false;
    printMessage("Magnet Gone");
  }
}

void printMessage(String message){
  counter++;
  Serial.print(counter);
  Serial.print(" ");
  Serial.println(message);
//  delay(1000);
}

boolean magnetPresent(int pin){
  return digitalRead(pin) == LOW;
}

it also set the pin high and it works ,so ..i not sure. Thanks anyway

not your only problem

what are the other problems?

I'd use an interrupt for this

how would i implement that? maybe just point me in a right direction if you can't be bothered explaining. thanks for replying

Here is a link to a basic tutorial on state change detection and counting the changes.

http://arduino.cc/en/Tutorial/ButtonStateChange

You should be able to modify that example where you are using digitalRead of your Hall Sensor instead of reading the button. You will need to add an elapsed time function so that you can get the counts over some time period to calculate rps.

Depending upon the speed of your POV display you should be able to make it work with polling. 30 rps will generate a pulse every 33.3ms which is slow in the world of the processor, but the issue will be the time that the magnet is in front of the Hall sensor and your loop time.

If you do go down the road of interrupts (and most of the code you will see on tachometers and rpm counters does go that route) here is a basic reference.

http://playground.arduino.cc/Main/ReadingRPM

Google and forum search on Arduino tachometer and Arduino rpm meter will also bring up lots of examples.

you set the pin high and wait for it to go low?

That's not even close to what that code is doing.

That code is turning on the internal pullup resistor, which is a good thing. It means that an external resistor is not needed.

The pin state goes LOW when the switch is pressed (or when a magnet nearby causes the hall effect sensor to act like a pressed switch).

int rpm = 0;//not really RPM but revolution per second

There is nothing even remotely magical about the name. If the value in the variable is revolutions per second, then call it rps and ditch the stupid comment.

To be honest i don't know what i am doing, it appeared in someone else' sketch:

So I hope your will say this in your project write up.

There is absolutely zero reason for calculating the revolutions per second for this project. All you do is detect the index pulse and then send the data. The length of the data and the rotation speed will determine how much the data is spread out. Adjust the delay between each data until it matches.

This is how I did it wit a wand and a Raspberry Pi but the idea is exactly the same.

http://www.thebox.myzen.co.uk/Raspberry/Magic_Wand.html

thanks for all the help, i wanted to count the rev per second for diagnostic testing of the display, not because i need to to make the POV display work.

Is there any resource that can help me to program the POV display with SPI protocol to control the shift register? Because most program i find controls the LEDs directly with the Arduino I/O pins. and also resources that explain the how to use data array for POV display

Thanks for any constructive responses(and none constructive ones)