I created a program that measures the pulses set out by the encoder (part of the dc motor with encoder inbuilt), and uses the ppr value (pulses per rotation) to calculate the rotations per minute (rpm)
However, the serial monitor continuously shows 0 for the variable "pulsecount", that maintains the total pulses sent.
code:
float ppr=512.0;
int encpin=3;
volatile long pulsecount=0;
float revs;
float rps;
float rpm;
unsigned long time;
void setup()
{
pinMode(encpin,INPUT);
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(encpin),function,RISING); //when pulse is detected
//interrupt triggered,
//function callled
}
void function(){
pulsecount+=1;
}
void loop()
{
revs= pulsecount/ppr ;
time=millis()/1000;
rps=revs/time ;
rpm=revs*60;
Serial.println(pulsecount);
}
function()
inbetweensetup()
andloop()
? If it compiles it might be OK, but I have never seen that... Try to print every value to Serial Monitor and maybe you can see the variable which causes this behavior.INPUT_PULLUP
rather thanINPUT
?