Hello everyone!
I'm making a school project (Control of an Inverted Pendulum), for this project i'm using an Arduino Mega (ATmega2560) with a pololu gearmotor that gives me 64CPR. So I know the Arduino Mega works at 16MHz, and after doing all the math for the controller, I get a sample time of 100ms and a prescaler of 32.
After researching on Internet I found a TimerOne library that I thought it would help me. The encoder of the dc motor is a two-channel hall effect sensor, so I want to count the pulses when it goes from low to high using an external interrupt from pins 2 & 3 to get the RPMs. The problem is that when I run the program, the result is always different. Can you please help me understand a little more about the Arduino Timers or how can I set up the correct timer.
Here's my code
#include<TimerOne.h>
volatile byte pulses;
float RPM;
void setup() {
Timer1.initialize(100000); //ms
Timer1.attachInterrupt(ISR_Callback);
Serial.begin(115200);
}
void ISR_Callback() {
attachInterrupt(0,ChannelA,RISING);
attachInterrupt(1,ChannelB,RISING);
}
void ChannelA() {
pulses++;
}
void ChannelB() {
pulses++;
}
void loop() {
noInterrupts();
RPM=(pulsos/64)/(0.1/60); // (pulses/CPR)/(Sample time/1 Minute)
interrupts();
Serial.print("RPM = ");
Serial.println(RPM);
}
Thank you
