External interrupt Guru's

This is a project I’m working on but, I would like to get a specific answer on said project. I want to stay focused on this aspect of the project in thread.

External interrups

When you use the ISR and and external interrupt for instance

int pin = 13;
int hallcounter;
volatile int hall = LOW;

void setup()
{
pinMode(pin, OUTPUT);
attachInterrupt(0, counter , FALLING);
}

void loop()
{
}
Did I write the attachInterrupt(0, counter , FALLING);? correctly
Where does the ISR go?
I want to count the hall events with hall counter.
Does the external interrupt trigger the ISR?
If it does and I’m using it as a counter and an interrupt happens and goes to increment and the interrupt happens again does it null the first one causing me to miss counts?
After counter gets so high I want the next thing to happen which is stop my motor?
So coult the ISR look like this

If (counter < target);//use counter to go to target number of events
speedo.write(95);// motor controller
else
speedo.write(90);//motor stopper

Yes, your attach statement is correct. The ISR is a function, so it goes outside of the setup and loop functions.

pre-setup: #defines, #includes, variable definitions, etc.

void setup(){
:
:
}

void ISR() {  // void if nothing is returned, or it acts on volatile variables:
:                  // I'd need to look up some examples to confirm
}

void loop(){
:
:
}

Some questions I have

  1. Should I use falling, rising, change?

The hall effect sensor is controlled by the RC speed controller so I'm pretty sure it has its own cr (capacitor resistor circuit built into it) so I don't (think) that I need to you use any kind of debounce function. 2. Do you think my assumption is correct?

The three variables after the attach interrupt kind of confuse me. 3. Can someone clarify this?

attachInterrupt(0, counter , FALLING); I think the 0 is for the external interrupt pin, the counter is the function that it goes to, and the FALLING is when the interrupt is initiated. 4. Do I have this correct?

  1. What are the scenarios that each of these should be used?

  2. Which one should I use?

  3. Why should I use the one you suggest?

RISING, FALLING, CHANGE, LOW.

My program works like this I give the hall counter a desired target say 1000 and that correlates 1 rotation for every hall event. So I give the motor controller a PWM signal to move and on the first rotation the interrupt happens.

  1. Does this mean that the ISR function will cause the processor to stop sending the PWM signal to the Speedcontroller?

  2. Or will it do both at the same time?

When the counter does get to the desired (count) the PWM signal will need to be changed to stop the motor. 10. If the interrupter is always interrupting can the program do this in the back ground or is it going to be interrupting so fast that it won't be able to.? 11. Or do I need to put this stop function in the ISR I think its called detach interrupt? 12. whats the longest the ISR should be to be able to count over 20,000 rotations a minute

(yellow changed to red to make it readable).

  1. Falling.
  2. Could be.
  3. desription looks correct.
  4. see 3.
  5. Signal that is interrupting is normally low, you want interrupt when it goes high; Signal that is interrupting is normally high, you want interrupt when it goes low; You don't care which level, you just want an interrupt when it changes.
  6. falling.
  7. Where is the signal level when nothing is moving? Use the opposite level.
  8. No. PWM is "set & forget", it free runs once started until it is turned off.
  9. See 8.
  10. Takes 4-8microseconds to process the interrupt. Your PWM will be at 490 Hz (2000uS)? 980 Hz (1000uS)? Might be noticable, depends what else is going on.
  11. interrupts are disabled already while in an ISR.
  12. 20,000/min = ~333/second, slower than standard PWM. Keep the ISR as short as possible no matter what.

Thank you so very much for the direct answers. Even though some of them sounded the same there was a little bit of difference in each that clarified it for me I will finish my program tonight and let you know the results.

I can’t get this external interrupt to work on this dang thing.

Here is my current sketch and it seems to work its just not counting.

 #include <Servo.h>

int Speedcontroller = 9;
Servo motor;
int HALT = 89;
int FWD = 100;
int REV = 87;
int target;
volatile int hallInt = LOW;
int rotations;
int hallState = 0;
int lastHallState = 0;

void setup()
{
  motor.attach(9);
  delay(4000);
  attachInterrupt(0, counter, CHANGE);
  Serial.begin(9600);
  Serial.println("Ready ");
  delay(1000);
  Serial.println("Send X location");  
  Serial.println("Ready");
  
}
void loop()
  {
  if (rotations <= target - 70)
  motor.write(FWD);
  else
  {
    motor.write(HALT);
  }
  }

void counter()
{
  if (hallState != lastHallState) {
  if (hallState == HIGH){
  if (rotations < target){
  rotations++; 
 Serial.println(rotations); 
}}}}

Need to know details of the circuit. The Hall sensor in particular. They come in various types, analog, digital, etc. Also need to know how it's connected, voltages, etc. I wouldn't think debouncing is needed, it's not a mechanical device, there's nothing to bounce. Having a datasheet for the sensor would be ideal. Lacking that, does the ESC documentation have details of the sensor?

Not much point in writing code until these details are sorted.

I have the circuit aready. Sorry for not posting it. Is this what you need or do you need more.

-pin 9 PWM out put to speed controller -using servo library(89-0 is rev 0 being the fastest reverse and 90-179 is fwd 179 being full throttle fwd)so 0=full reverse,90=brakes,179=full forward.

-pin 2 external interrupt for hall input not CR circuit because its built into the speed controller(I opened it up and verified)

-gnd pin goes to speed controller ground and hall ground.

styler93gsx: I have the circuit aready. Sorry for not posting it. Is this what you need or do you need more.

So where is it, did I miss it? Did you post a circuit?

That basically doesn't tell me anything more. Need specs or a part number for the Hall sensor itself. Need to know how the connections are brought out of the motor. Need the circuit diagram for the motor/sensor, I don't even care about the Arduino at this point. I have no idea what this "CR circuit" business is. If that means resistor/capacitor circuit, it would normally be referred to as an "RC" circuit. I have no idea why one might be needed with a Hall effect sensor. Is there a link or any doc whatsoever on the motor?

My crystal ball is out for repairs, I don't expect it back until next month sometime. If there are no hard facts available, then a person would have to have the thing in hand, and determine its characteristics via testing.