hi,
I am trying to use an intterupt function to get a rpm readout from my DC motor. The problem i am having is that it reactes up to 5 times sometimes per slot on the rotating disc. i don't know how i can prevent this from hapening.
that when i set it to rising for example, it should only go in the function 1 time when the slot passes by. But it goes in this functies for 5-6 times. so the time is really short and i have no rpm readout.
i tested your code and it still enters the void 5-6 times in 1ms. is there a way to let the arduino ignore the other all the other times it enters the void except for the first?
when i use your method, i get around 5000rpm. this same result as i got when i measured the amount of times the disk made a full rotation in 1 second. i todd this wasn't correct so i dismissed it. is there an reason why the measurement above shows so different results?
i know that i have bounce on my signal, i just don't know how to fix this for an ir sensor. this is a school project so the components are given by them and i don't see any serial numbers on them. i don't have a schematic ready yet and i will provide them once there ready. the purpose of this is that a need a rpm measurement on startup. i need to type in a rpm that i want and then the DC motor needs to get to that rpm in exactly 10 seconds, regardless of the rpm i set it to. and while the motor is starting, i need to see the rpm i gave up and the rpm it is running at. and i have everything except for the rpm reading.
i think i will need to do this in software for the teachers to be happy. so if a take my calculations out and wait for stable reading, my values that store in "old_millis" and "current_millis" won't be correct or can i make them that they only react to the last change?
The method would be similar to the bounce without delay example.
In this case, you won't be able to prevent the multiple interrupts from happening (during the noise or bounce), but you could respond to the first interrupt, then wait for a time period to expire before responding to the next valid interrupt. Need to monitor elapsed time (micros()) from the first interrupt.
Here's an example that would need modification. Could give some ideas for your project, as it's similar. Good luck!
const byte wings = 4; // no of wings of rotating object, for disc object use 1 with white tape on one side
volatile unsigned long us, prevPulseUs, pulseUs, prevPulseUsCopy, pulseUsCopy;
unsigned long pulsePeriod, rpm;
void isr() { //interrupt service routine
us = micros();
if ((us - pulseUs) > 2000) { //limited to 500Hz max
prevPulseUs = pulseUs;
pulseUs = us;
}
}
bool timeOut(unsigned long ms) { //ms timer
volatile unsigned long prevMs, now;
now = millis();
if ((now - prevMs) >= ms) {
prevMs = now;
return true;
}
return false;
}
void setup() {
pinMode(3, OUTPUT); // pwm test: disconnect sensor, then add jumper from pin 2 to 3
analogWrite(3, 127); // pwm @ 490 Hz = 29400 rpm / wings
attachInterrupt(digitalPinToInterrupt(2), isr, RISING); //attaching the interrupt
}
void loop() {
if (timeOut(1000)) {
cli(); //stop interrupts
prevPulseUsCopy = prevPulseUs;
pulseUsCopy = pulseUs;
sei(); //allow interrupts
pulsePeriod = pulseUsCopy - prevPulseUsCopy;
if (!pulseUsCopy || !prevPulseUsCopy) pulsePeriod = 10000000; // startup 0 rpm
rpm = 1000000 / pulsePeriod * 60 / wings; //calculates rpm
// print results
}
}