Go Down

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

Alimoestar_S_K

Hi,

i'm working on rpm sensing using a proximity sensor. when ever the sensor detects metal bolt on the motor axis it should blink BUILTIN LED. The time between blinks is used to calculate the rpm. thats the theory behind this project. but so far no luck....
any advise?

Code: [Select]
/* interrupt function testing
 *  created 30 may 2018 9:51
 *  by Alimoestar SK
 *  tested and improved till 6 june 2018
 *  work as follows. if a metal object is detected build in led is on
 *  the number led is on is counted and presented as "number of detections"
 *  RPM is measured using the number of detection and the time that has passed
 */


const byte interruptPin = 2;
volatile byte state = LOW;
float n = 0;
float RPM = 0;

unsigned long StartTime = millis();



void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, HIGH);
  /* CHANGE to trigger the interrupt whenever the pin changes value
   *  LOW to trigger the interrupt whenever the pin is low,
   *  RISING to trigger when the pin goes from low to high,
   *  FALLING for when the pin goes from high to low.The Due, Zero and MKR1000 boards allows also:
   *  HIGH to trigger the interrupt whenever the pin is high.
   */
  Serial.begin(9600);
 
}
void loop() {
  digitalWrite(LED_BUILTIN, state);
 
}

void blink() {
  state = !state;
{ if(digitalRead(LED_BUILTIN  == HIGH))
{n++;
  Serial.print("number of detections:");
  Serial.println(n);
}
  unsigned long CurrentTime = millis();
 
    RPM = ((float) n / CurrentTime - StartTime);
    CurrentTime = StartTime;
      Serial.print("RPM:");
      Serial.println(RPM);
     
}}
 

bos1714

#1
Jun 07, 2018, 01:08 pm Last Edit: Jun 07, 2018, 01:09 pm by bos1714
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, HIGH);
  /* CHANGE to trigger the interrupt whenever the pin changes value
   *  LOW to trigger the interrupt whenever the pin is low,
   *  RISING to trigger when the pin goes from low to high,
   *  FALLING for when the pin goes from high to low.The Due, Zero and MKR1000 boards allows also:
   *  HIGH to trigger the interrupt whenever the pin is high.
   */
  Serial.begin(9600);
 
}
Hello there!

The first thing that strikes me as odd is that you have the interrupt pin set as INPUT_PULLUP, which leads me to believe that your sensor pulls the pin low when it sees the metal bolt. Your interrupt is set to run the ISR when the pin is high. Since the pin is set at INPUT_PULLUP, the ISR is going to continuously run when the sensor does not see the bolt, seeing as how the pin is always high (except for the metal bolt). Try changing the interrupt event so that it triggers the ISR during a FALLING edge, instead of HIGH.
Lines? Time isn't made up of lines. Time is made up of circles. That is why clocks are round.

PaulS

Code: [Select]
  attachInterrupt(digitalPinToInterrupt(interruptPin), blink, HIGH);
The valid modes are RISING, FALLING, CHANGE, and LOW. HIGH is nowhere in that list.
The art of getting good answers lies in asking good questions.

bos1714

#3
Jun 07, 2018, 05:47 pm Last Edit: Jun 07, 2018, 05:47 pm by bos1714
The valid modes are RISING, FALLING, CHANGE, and LOW. HIGH is nowhere in that list.
Here is a snippet from the Arduino Reference page for attachInterrupt(). It clearly says that HIGH is on the list. Additionally, if HIGH wasn't allowed, wouldn't the compiler throw an error?

Lines? Time isn't made up of lines. Time is made up of circles. That is why clocks are round.

PaulS

Quote
Additionally, if HIGH wasn't allowed, wouldn't the compiler throw an error?
The compiler would throw an error if you tried to use "SamIsAnIdiot" as the argument. You are free to use values of the correct type, but not that the function expects, and the compiler will let you.
The art of getting good answers lies in asking good questions.

bos1714

I see. You say that HIGH is not a valid argument input, but yet is it on the reference snippet I posted. Any explanations?
Lines? Time isn't made up of lines. Time is made up of circles. That is why clocks are round.

PaulS

I see. You say that HIGH is not a valid argument input, but yet is it on the reference snippet I posted. Any explanations?
Other than that it doesn't make sense? Why would you want to trigger an interrupt continually when a pin was HIGH or LOW?

Interrupts are to handle changes from outside the microcontroller.

(LOW has a valid use in waking from sleep mode)
The art of getting good answers lies in asking good questions.

cattledog

#7
Jun 08, 2018, 06:32 pm Last Edit: Jun 08, 2018, 06:50 pm by cattledog
Quote
Here is a snippet from the Arduino Reference page for attachInterrupt(). It clearly says that HIGH is on the list.
You are correct about this, but there is more to consider. Did you see this line  about the Due, Zero, and MKR1000 boards right before the listing of HIGH

Quote
The Due, Zero and MKR1000 boards allows also:
HIGH: to trigger the interrupt whenever the pin is HIGH
What Arduino do you have?

In the AT328 Arduinos the external interrupt mode HIGH is actually treated the same as CHANGE.

Arduino.h defines HIGH as 1 and LOW as 0. It also defines CHANGE as 1, FALLING as 2 and RISING as 3.

The attachInterrupt() function (defined in WInterrupts.c) just does an entry of that value into the External Interrupt Control Register A (EICRA) for interrupt sense control. The only values which fit into the two control bits of that register are 0 through 3.

Table 12-1. Interrupt 1 Sense Control
ISC11 ISC10 Description
0 0 The low level of INT1 generates an interrupt request.
0 1 Any logical change on INT1 generates an interrupt request.
1 0 The falling edge of INT1 generates an interrupt request.
1 1 The rising edge of INT1 generates an interrupt request.


Please elaborate on what your program is doing. "no luck" is not a very good problem statement.

Go Up