Go Down

Topic: rpm sensing with proximity sensor (Read 2504 times) previous topic - next topic

Alimoestar_S_K

Work out, on paper, EXACTLY which pin you are reading from. It is NOT the pin you want to read from.


the pin i am reading from is pin 2 on the arduino uno R3, which has interrupts for pin 2 and 3.


There is NO need to read the state of an output pin. Keep track of the state you set it to.

so in other words leave out the write state to build in led and try something else?
will a flag function work?

PaulS

Quote
the pin i am reading from is pin 2 on the arduino uno R3, which has interrupts for pin 2 and 3.
With this code,
Code: [Select]
  if (digitalRead(LED_BUILTIN  == FALLING))
You are comparing LED_BUILTIN to FALLING, which returns true or false. Which of those values do you think is 2? Neither one is. That code is reading from pin true or pin false. Hardly a reasonable thing to do.

Even if you fix that to
Code: [Select]
  if (digitalRead(LED_BUILTIN)  == FALLING)
so that you are reading from the LED_BUILTIN pin, you are comparing HIGH or LOW to FALLING. Never will match...
The art of getting good answers lies in asking good questions.

Alimoestar_S_K

#17
Sep 12, 2018, 03:29 pm Last Edit: Sep 12, 2018, 03:32 pm by Alimoestar_S_K
dear all,

after consideration of all your comments and some extra reading and testing, i came up with the following code:

Code: [Select]


/* RPM sensing using a arduino uno
    and inductive proximity sensor
    Alimoestar S.K.
    original code may 2018
    revised 20 aug 2018
    revised 27 aug 2018
    revised 1 sep 2018

*/

//---------------------------- declaration -------------------------------
const byte interruptPin = 2;
int n = 0;
volatile long RPM = 0;
volatile long RPMsum = 0;             // sum of 20 RPM measurements
volatile long RPMav = 0;              // average of 20 RPM measurements
float F = 0.000;                      // frequency
volatile boolean flag = 0;
unsigned long DeltaT;                 // time between two interupts
unsigned long previoustime = 0;



//---------------------------- setup --------------------------------------
void setup()
{
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(interruptPin), sensor, FALLING);
}

//---------------------------- ISR sensor --------------------------------
void sensor()
{ flag = 1;
}

//----------------------------- loop --------------------------------------
void loop()
{ //-----------------------------------------------------------------------rpm measurement
  if (flag)
  { noInterrupts();                                            // disables interrupt, because the folloing is time sensitive
    n++;
    unsigned long currenttime = micros();                      // measure the current time
    DeltaT = (currenttime - previoustime);                     // calculate the time difference
    previoustime = currenttime;                                // set previoustime = currenttime


    if (DeltaT >= 32000) {                                     // deltaT 32000 => rpm= +-1800 (debouncing)
      RPM = float(60000000UL / DeltaT);                        // calculate the rpm (One (1) signal per rotation)
     
      Serial.print("delta T =\t");
      Serial.print(DeltaT);
      Serial.print("\t rpm =\t");
      Serial.println(RPM );
     
      //}

      //---------------------------------------------- frequency calculation
      F = (RPMav / 2) * 60.00;
      flag = 0;
      interrupts();

    }



    // Average RPM value of 200 measurements
    RPMsum = RPMsum + RPM; // sum of 200 measurements

    if (n == 20) {
      RPMav = (RPMsum / 20); // averge of 200 measurement
      //---------------------------------------------- print to serial screen
      Serial.print("    RPM = ");
      Serial.print(RPM );
      Serial.print("    RPMav = ");
      Serial.print(RPMav );
      Serial.print("     Freq = ");
      Serial.println(F );
      RPMsum = 0;
      n = 0;

    }
  }

}//-------------------------------- end loop -----------------------------------------


//--------------------------------- end code -----------------------------------------



this seemed to work at first, but after some time gave some nonsense reading.
any thoughts or coments???

PaulS

Quote
but after some time gave some nonsense reading.
any thoughts or coments
Yes. You ABSOLUTELY MUST understand what you can, and can not do, with interrupts disabled.

The ONLY thing you should be doing, while interrupts are disabled, is copying and resetting the values of the variables that the interrupt service routine(s) use.

NOT A SINGLE THING ELSE.

The art of getting good answers lies in asking good questions.

mrphysh

This is similar to my project.  These comments make little sense.  Watch this little video;  If you can understand this simple sketch, the solution will become obvious.  (use a Hall detector, not the photo module in the video.  I have since learned that RPM is usually done with a Hall effect detector)

https://youtu.be/ifZo87lrlfE

Go Up