Hello all
My friends and I are working on a project with an LM393 speed sensor and Arduino Uno rev3. We have an encoder disc that we want to run through the sensor, the arduino recognizes two different RPMs and calculates an acceleration. Essentially, (V2-V1)/(t2-t1). We can't seem to get the code to correctly output an acceleration. The output of RPM is correct but the acceleration is a constant value no matter what runs through the sensor.
Anyone have any insight or similar code to work off of?
int encoder_pin = 2; // The pin the encoder is connected
unsigned int rpmA; // rpmA reading
unsigned int rpmB; // rpmB reading
unsigned int acceleration; // acceleration reading
volatile byte pulses; // number of pulse
unsigned long timeoldA;
unsigned long timeoldB;
// The number of pulses per revolution
// depends on your index disc!!
unsigned int pulsesperturn = 20;
void counter()
{
//Update count
pulses++;
}
void setup()
{
Serial.begin(9600);
//Use statusPin to flash along with interrupts
pinMode(encoder_pin, INPUT);
//Interrupt 0 is digital pin 2, so that is where the IR detector is connected
//Triggers on FALLING (change from HIGH to LOW)
attachInterrupt(0, counter, FALLING);
// Initialize
pulses = 0;
acceleration = 0;
rpmA = 0;
timeoldA = 0;
rpmB = 0;
timeoldB = 0;
}
void loop()
{
if (millis() - timeoldA >= 100); /*Uptade every one second, this will be equal to reading rotations per minute (rpm).*/
if (millis() - timeoldB >= 110)
{
//Don't process interrupts during calculations
detachInterrupt(0);
//Note that this would be 60*1000/(millis() - timeold)*pulses if the interrupt
//happened once per revolution
rpmA = ((60000 / pulsesperturn )/ (millis() - timeoldA)* pulses);
timeoldA = millis();
rpmB = ((60000 / pulsesperturn )/ (millis() - timeoldB)* pulses);
timeoldB = millis();
pulses = 0;
acceleration = (rpmB - rpmA) / (timeoldB - timeoldA);
//Write it out to serial port
Serial.print("rpmA = ");
Serial.println(rpmA,DEC);
//Serial.print("rpmB = ");
//Serial.println(rpmB,DEC);
Serial.print("accel = ");
Serial.println(acceleration,DEC);
//Restart the interrupt processing
attachInterrupt(0, counter, FALLING);
}
}