I am using a DC motor with encoder, and I am trying to calculate how many pulses are recorded in every one second interval. (note that the power source is of 12V for the motor)
Circuit and code:
int encpin=3;
volatile long npc=0; //new pulse count
volatile long opc=0; //original pulse count
volatile long pulsecount;
unsigned long int newtime;
unsigned long int prevtime=0;
unsigned long int time;
void setup()
{
pinMode(encpin, INPUT_PULLUP);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(encpin),function,RISING);
}
void function(){
npc++;
}
void loop()
{
newtime=millis();
time=newtime-prevtime;
pulsecount=npc-opc;
if(time==1000){
Serial.println(pulsecount);
prevtime+=1000;
opc=npc;
}
}
It seems to be rather intuitive that increasing the rpm should increase the number of pulses recorded in one second. However, the results are rather erratic:
RPM PULSECOUNT
26 2365
32 2367
38 2400
44 2375
52 2400
116 2412
142 2384
195 2416
280 2406
350 3008
416 1580
520 1975
624 2370
730 2334
2737 2188
Increasing RPM sometimes increases the pulsecount, but sometimes decreases it....what could be the reason for this behaviour?
time==x
? Seriously??!