Hall sensor question : have code & how use it

Hi , i have Hall sensor module ,,connected to pin 2 arduino uno , then GND and VCC , also have small magnet ,,if goes near sensor ,,it react ,,and works ,,i can read the serial monitor arduino IDE . each time magnet is close to sensor :

Magnet Present and each time far way : Magnet Gone .. all is fine . also have a sketch .can count each time magnet goes near the sensor , but how can i use the numbers of counts , i need them to measure my wheel speed ,i want use it to my purpose .

my purpose is counting ...and use that counts numbers ( Data ) ...for understand what is the RPM of wheel ,, if the wheel speed ( RPM ) is low ,{do something or some process ( that process don't have any direct relation with this process ) ,this process is only for understand wheel RPM } if my wheel speed is high{don't do something }.

here the code :

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;
}

regard

Hi. Your code needs to measure the time between magnet detections. You can do this using the millis() function. When the magnet is detected, store the value of millis() in a variable (of type unsigned long). Next time the magnet is detected, subtract the previous value from the current value. If the difference is large, the wheel is turning slowly.

Hi, Look up arduino tacho

in the search window in the top right hand corner of this screen.

Tom... :)

ok thanks ,i try write the code ,, im not sure can write that ,,,just try i need example code .then can complete it .im new in arudino

Example http://playground.arduino.cc/Learning/Tachometer

thanks ,i see the website ,
it helps but is not clear for me .
this part of that sketch maybe is useful for my job but i dont know how to use it

void loop()
{
  val=analogRead(0);
  if(val<sens)
    stat=LOW;
   else
    stat=HIGH;
   digitalWrite(13,stat); //as iR light is invisible for us, the led on pin 13 
                          //indicate the state of the circuit.

   if(stat2!=stat){  //counts when the state change, thats from (dark to light) or 
                     //from (light to dark), remmember that IR light is invisible for us.
     contar++;
     stat2=stat;
   }
   if(millis()-last>=milisegundos){
     double rps=((double)contar/nPalas)/2.0*1000.0/milisegundos;
     double rpm=((double)contar/nPalas)/2.0*60000.0/(milisegundos);
     Serial.print((contar/2.0));Serial.print("  RPS ");Serial.print(rps);
     Serial.print(" RPM");Serial.print(rpm);Serial.print("  VAL ");Serial.println(val);
     contar=0;
     last=millis();

it helps but is not clear for me .

You asked for an example and that is what I gave.

Maybe you meant to say "will you write this for me"?

The answer is no. We can help you understand what you don't understand and point you at examples but you have to do some work.

So what do you not understand? Is it the concept of what you are doing or the implementation of this concept. If it is this then explain what you think the concept is and explain exactly what in the implementation you are having trouble with.

i do that .. for high RPM led always on ..means works nice ..it can understand high rpm ...

but for low rpm when led if off .... im not sure is completely done or not ,,i see something random.. for example . when turn the wheel ..sometime works fine ..but some times not

im not sure maybe is fine

i put pin 13 for led .

 volatile byte half_revolutions;
 unsigned int rpm;
 unsigned long timeold;
 void setup()

  {pinMode(13,OUTPUT);

   Serial.begin(115200);
   attachInterrupt(0, magnet_detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)
   half_revolutions = 0;
   rpm = 0;
   timeold = 0;
 }
 void loop()//Measure RPM
 {
   
  
     rpm = 3*1000/(millis() - timeold)*half_revolutions;
     timeold = millis();
     half_revolutions = 0;
     //Serial.println(rpm,DEC);

if (rpm >=2)
   { 
  digitalWrite(13,HIGH);
  delay(1000);
 digitalWrite(13,LOW);



     
   }
 }
 void magnet_detect()//This function is called whenever a magnet/interrupt is detected by the arduino
 {
   half_revolutions++;
   Serial.println("magnet");
{






 }}

[quote author=Grumpy_Mike link=msg=2872929 date=1470592961

Maybe you meant to say "will you write this for me"?] \

yes ..you good understand ...:) my mean was that .. anyway i find another sketch .i think im successful ..but not 100% maybe i make small mistake ...maybe not i try to test

Serial.print in an Interrupt Service Routine (ISR) will cause problems. Set a flag in the ISR, in loop(), if the flag is set, print and clear the flag.

groundfungus: Serial.print in an Interrupt Service Routine (ISR) will cause problems. Set a flag in the ISR, in loop(), if the flag is set, print and clear the flag.

i dont use LCD ..i just want define .HIGH RPM or LOW RPM for High RPM Led = HIGH for Low RPM Led = LOW

after High RPM …LED == on …OK
but

when LOW RPM …LED don’t want off … after stop turning goes off .

 volatile byte half_revolutions;
 unsigned int rpm;
 unsigned long timeold;
 void setup()

  {pinMode(13,OUTPUT);

   
   attachInterrupt(0, magnet_detect, RISING);//Initialize the intterrupt pin (Arduino digital pin 2)
   half_revolutions = 0;
   rpm = 0;
   timeold = 0;
 }
 void loop()//Measure RPM
 {
   
  
     rpm = 3*1000/(millis() - timeold)*half_revolutions;
     timeold = millis();
     half_revolutions = 0;
     //Serial.println(rpm,DEC);

if (rpm >=2)
   { 
  digitalWrite(13,HIGH);
  delay(1000);
 digitalWrite(13,LOW);



     
   }
 }
 void magnet_detect()//This function is called whenever a magnet/interrupt is detected by the arduino
 {
   half_revolutions++;
  
{
 





 }}