I am using Arduino Duemilanove and a hall effect sensor to calculate the speed of a motor. When I used sensor out directly to Interrupt0, the rising edges of noise were also detected. So the speed calculation went wrong.
So now sensor is connected to A0.and a threshold is put. Pin No 13 is connected to Interrupt 0(Pin 2). I use the following code.
volatile byte count;
const int analogPin = A0;
const int digitalPin=13;
const int threshold =20;
unsigned int rpm;
unsigned long starttime;
void setup()
{
pinMode(digitalPin,OUTPUT);
digitalWrite(analogPin,HIGH);
digitalWrite(digitalPin,HIGH);
Serial.begin(9600);
attachInterrupt(0,sense,FALLING);
rpm=0;
starttime=0;
count=0;
}
void loop()
{
int sensorValue = analogRead(analogPin);
Serial.println(sensorValue);
if (sensorValue < threshold)
{
digitalWrite(digitalPin,LOW);
if (count>=20)
{
rpm=60*1000*count/(millis()-starttime);
starttime=millis();
count=0;
Serial.println(rpm,DEC);
}
}
else
{
digitalWrite(digitalPin,HIGH);
}
}
void sense()
{
count++;
}
But I get actual speed only in between. The speed changes back . I feel there is some problem with the sampling or rpm updation. Looking forward for help
attachInterrupt
here since this is your code that artificially triggers interrupts by writing todigitalPin
. Hence I think yoursense
function is not called as many times as you would expect. Also, where does the value20
for threshold come from?