Ouput of acceleration from Encoder disc

My friends and I are slightly inexperienced with Arduino coding and are currently coding a program that gives an output of acceleration and RPM from an encoder disc with 20 slits and an LM393 speed sensor. So far we have the code correctly output the RPM of the disc (tested and verified). We would like the output to also provide the acceleration with every given RPM but don’t know where to go from here.

This is our code so far

int encoder_pin = 2; // The pin the encoder is connected
unsigned int rpm; // rpm reading
volatile byte pulses; // number of pulses
unsigned long timeold;
// 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;
rpm = 0;
timeold = 0;

}

void loop()
{
if (millis() - timeold >= 100){ /Uptade every one 1/10 second, this will be equal to reading rotations per minute (rpm)./

//Don’t process interrupts during calculations
detachInterrupt(0);
//Note that this would be 601000/(millis() - timeold)pulses if the interrupt
//happened once per revolution
rpm = ((60
1000 / pulsesperturn )/ (millis() - timeold)
pulses)/1.7;
timeold = millis();
pulses = 0;

//Write it out to serial port
Serial.print("rpm = ");
Serial.println(rpm,DEC);
//Restart the interrupt processing
attachInterrupt(0, counter, FALLING);

acceleration = (
Serial.println(acceleration, DEC);
}
}

Any feedback or advice would help!

SpeedSensor.ino.ino (1.3 KB)

Please read the instructing topics like "How to use the Forum", "How to attach code".
Many helpers can't download code this way and You get no help from them.

I'm sorry, as I am new to the forum. I believe this is better for my code:

int encoder_pin = 2;  // The pin the encoder is connected           
unsigned int rpm;     // rpm reading
volatile byte pulses;  // number of pulses
unsigned long timeold;
// 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;
   rpm = 0;
   timeold = 0;

 }

 void loop()
 {
   if (millis() - timeold >= 100){  /*Uptade every one 1/10 second, this will be equal to reading rotations per minute (rpm).*/
 
  //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
   rpm = ((60*1000 / pulsesperturn )/ (millis() - timeold)* pulses)/1.7;
   timeold = millis();
   pulses = 0;
   
   //Write it out to serial port
   Serial.print("rpm = ");
   Serial.println(rpm,DEC);
   //Restart the interrupt processing
   attachInterrupt(0, counter, FALLING);

    acceleration = (
   Serial.println(acceleration, DEC);
   }
  }

welcome to the forums. Please read the sticky post at the top of the forum and learn how to properly post your code using code tags. It helps people help you.

This code will get you there as soon as you insert the formula for acceleration (did you want angular acceleration?) As it stands now, it just reports 0.00

const int encoder_pin = 2;  // The pin the encoder is connected
volatile unsigned int pulses;  // number of pulses
unsigned long timeold;

// The number of pulses per revolution
// depends on your index disc!!
const 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);
 // Initialize
 pulses = 0;
 timeold = 0;

 //Interrupt 0 is digital pin 2, so that is where the IR detector is connected
 //Triggers on FALLING (change from HIGH to LOW)
 attachInterrupt(digitalPinToInterrupt(encoder_pin), counter, FALLING);
}

void loop()
{
 unsigned long currentTime = millis();

 unsigned long duration;
 unsigned int rpm;     // rpm reading
 unsigned int currentPulseCount;

 float acceleration;

 if (millis() - timeold >= 100) {
   // Uptade every one 1/10 second, this will be equal to reading rotations per minute (rpm)

   //Don't process interrupts during calculations
   //detachInterrupt(0);
   noInterrupts();
   currentPulseCount = pulses;
   pulses = 0;
   interrupts();

   //Note that this would be 60*1000/(millis() - timeold)*pulses if the interrupt
   //happened once per revolution
   duration = currentTime - timeold;
   rpm = ((60 * 1000 / pulsesperturn ) / duration * pulses) / 1.7;
   timeold = currentTime;

   //Write it out to serial port
   Serial.print("rpm = ");
   Serial.println(rpm, DEC);
   //Restart the interrupt processing
   //attachInterrupt(0, counter, FALLING);

   acceleration = 0.0;   // insert formula for acceleration here
   Serial.println(acceleration, 2);  // 2 decimal places
 }
}

Good! That's the way to do it.

Your specification regarding acceleration of the RPM needs to be specified more. Accleration is (V2 - V1)/(t2 - t1).
The sample interval, t2 - t1 needs to be decided.
It doesn't need to be precise, just read RPM and time as Close as possible and store them.