Hello there, I am working on a project of mine where I need to calculate the RPM of a DC motor as the title of this post says.
I am not sure if I do this correctly but let me first give you the specification of my setup. I am using:
Arduino Mega
Simple Encoders from Ebay https://cdn.shopify.com/s/files/1/0020/8027/6524/products/Par-de-encoder-rotativo-incremental-para-calcular-distancia-y-velocidad-800x800_x700.png?v=1524155292
My Encoders have 20 slots (holes). No I will paste my code which I actually found somewhere in the internet and modified.
int val; //calibration value
long last=0; //used in timing - holds thelast milis count
int prevLEncValue; //used in calibration
int lEncValue; //used in calibration
int counter=0; //used to count light/dark iterations
int sens=75; //calibration value
int slots=20; //# slots in disc
int milisecs=500; //time of sample
// some code here...
void loop() {
moveForward(95);
//manualControl();
//autonomousControl();
lEncValue = digitalRead(32); // Read the value of the left encoder
if(prevLEncValue != lEncValue) {
counter++;
}
prevLEncValue = lEncValue; // Update the previous left encoder value
if(millis()-last>=milisecs) { // has time passed?
double rps=((double)counter/slots)/2.0*1000.0/milisecs;
double rpm=((double)counter/slots)/2.0*60000.0/(milisecs);
counter=0; //reset counter
last = millis(); //remember this moment
Serial.println(rpm);
}
delay(10);
}
Notice that i pasted only the section this post is about, because my full code is about 700LOC. Thanks for any help... MY question is actually if this is correct or my approach is wrong?