SPEED RADIAN WITH HALL SENSOR

speed hall

const int hallPin = 2; // the number of the hall effect sensor pin
int hallState = 0; // variable for reading the hall sensorstatus
int lasthallState = 0;
int pulses;
unsigned long timeold;
int laju;
int turn;
void setup() {
Serial.begin(9600);
pinMode(hallPin, INPUT);
attachInterrupt(0, counter, FALLING);
pulses = 0;
timeold = 0;
}

void loop(){
// read the state of the hall effect sensor:
hallState = digitalRead(hallPin);

counter();
if (millis() – timeold >= 100) {
detachInterrupt(0);
laju=(pulses)*235.5; //number pulses in 100ms with 16 spots magnet
turn=(pulses)*0.625;
Serial.print(pulses);
Serial.print(” pulses/100ms “);
Serial.print(laju);
Serial.print(” rpm”);
Serial.print(” “);
Serial.print(turn);
Serial.println(” turn/second”);
timeold = millis();
pulses = 0;
attachInterrupt(0, counter, FALLING);

}
}
void counter()
{
hallState = digitalRead(hallPin);
if (hallState != lasthallState) {
if (hallState == LOW) {
pulses++;
}
lasthallState = hallState;
}

}