I am Using STM 32 F446RE with Arduino IDE for programming. Encoder will provide me 10000pulese per revolution and motor speed is 10rpm(max). I want to find out the exact velocity from that. I tried coding and ran into some error and not able to solve it. Please have look into the code and help me out.
Also I have got couple of questions:
Why interrupt is important for Encoder sensor?
What is the need for pull up resistor?
//#include <RotaryEncoder.h>;
// Initialization
const int pinA = D0;
const int pinB = D2;
int counterA = 0;
int counterB = 0;
int digA = 0;
int digB = 0;
int prevCountA = 0;
int prevCountB = 0;
unsigned long time;
unsigned long currenttime;
const unsigned long period = 100; //the value is a number of milliseconds
float angle = 0;
float rpm = 0;
// the setup routine runs once when you press reset:
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pinA, INPUT_PULLUP);
pinMode(pinB, INPUT_PULLUP);
}
// the loop routine runs over and over again forever:
void loop()
{
// call encoder
time = millis();
digA = digitalRead(pinA);
digB = digitalRead(pinB);
call_encoder();
calc_vel();
// delay(1); // delay in between reads for stability
}
void call_encoder()
{
if (prevCountA != digA)
{
counterA = counterA+1;
}
if (prevCountB != digB)
{
counterB = counterB+1;
}
Serial.print("A");
Serial.print(counterA);
Serial.println();
Serial.print("B");
Serial.print(counterB);
Serial.println();
// Serial.print(digA);
// Serial.println();
//
// Serial.print(digB);
// Serial.println();
prevCountA = digA;
prevCountB = digB;
}
void calc_vel()
{
// currenttime = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (time >= period) //test whether the period has elapsed
{
angle = (counterA/200)*360;
rpm = angle/360;
Serial.print("rpm");
Serial.print(rpm);
Serial.println();
time = 0;
}
}