SOLVED - Arduino nano v3- pin change Interrupt not working - SOLVED

Hi!

I am trying to figure out how to use interrupts with my arduino (pin change interrupt on one single pin).
I have an arduino nano v3 with atmega328p connected to an rc receiver on Digital pin D4. So far, i have come up with the following code:

/*
  reads the PPM signal from an rc receiver
  receiver: MULTIPLEX RX-7-DR light M-LINK
  processor: arduino nano v3.0
  wiring: receiver port B connected to GND, 3V3
  receiver port/channel 1 (signal pin) connected to arduino D4  
  using "pin change interrupts";
  measuring time between pin change events
*/


int oldtime = 0;
int timepassed;

#include <SoftwareSerial.h>


void setup() {
  Serial.begin(115200);
  pinMode(4, INPUT);
  
  //enable pin change interrupt 2: 
  //Change on any enabled PCINT[23:16] will cause interrupt
  //PCICR = Pin change interrupt control register
      PCICR |= (1 << PCIE2);      
     
  //enable Interrupt on specific pin
  //PCMSK2 = Pin change Mask register
      PCMSK2 |= (1 << PCINT20); //enable PinChangeInterrupt on PCINT20
      //PCINT20 = pin nr.2 = PD4 = D4
}


ISR(PCINT_vect)  //InterruptService-Routine, called by the Interruptvektor PCINT_vect
{
  //This function is called everytime the state of one of the interruptpins PCINT
  //that were activated above changes
  
  timepassed = micros() - oldtime;
  oldtime = micros();
}


void loop() {
  
  Serial.println(timepassed);
}

So, i have activated the general pin change interrupts in register PCICR, then the specific interrupt on Pin D4 in the register PCMSK2.
Ok, i do not check back which pin actually called the ISR, but know that, at the moment, an interrupt can only come from this single pin (which is the only one connected to electronics, and the only one activated in PCMSK2). So tthe code should somehow be working.

However, i only receive zeros over the serial, so there must be a mistake somewhere.

best regards,

Zero2

Not directly related to your question, but why are you including SoftwareSerial.h?

If you try to create an instance of SoftwareSerial, which also uses pin change interrupts, you'll be screwed.

Why not use an external interrupt pin, if you only need the one?

Edit: timepassed is an int; it should be unsigned long, and it needs to be volatile.

PaulS:
Not directly related to your question, but why are you including SoftwareSerial.h?

If you try to create an instance of SoftwareSerial, which also uses pin change interrupts, you’ll be screwed.

Oh you´re right! Sorry just a bad habit of mine- i just deleted that part.

PaulS:
Why not use an external interrupt pin, if you only need the one?

Well, in the long run i will need much more interrupts, but i cannot start coding on that unless i get the basic functionality with one single interrupt working. This, just trying to keep it simple.

PaulS:
Edit: timepassed is an int; it should be unsigned long, and it needs to be volatile.

Ok, i changed that. So, volatile means that the the program kinda always uses the “latest” (e.g. the current) value stored in the RAM, right?

int oldtime = 0;
volatile unsigned long timepassed;


void setup() {
  Serial.begin(115200);
  pinMode(4, INPUT);
  
  //define what kind of pin change makes interrupt happen
  //EICRA external interrupt control register a
    
  //enable pin change interrupt 2: 
  //Change on any enabled PCINT[23:16] will cause interrupt
  //PCICR = Pin change interrupt control register
      PCICR |= (1 << PCIE2);      
     
  //enable Interrupt on specific pin
  //PCMSK2 = Pin change Mask register
      PCMSK2 |= (1 << PCINT20); //enable PinChangeInterrupt on PCINT20
      //PCINT20 = pin nr.2 = PD4 = D4
}


ISR(PCINT_vect)  //InterruptService-Routine, called by the Interruptvektor PCINT_vect
{
  //This function is called everytime the state of one of the interruptpins PCINT
  //that were activated above changes
  
  timepassed = micros() - oldtime;
  oldtime = micros();
}


void loop() {
  
  Serial.println(timepassed);
}

Hi,

Volatile and a few other things you should be aware of explained in detail and specifically in relation to RC Signals here -

Duane B

rcarduino.blogspot.com

Ok, found the mistake!

It has to be
ISR(PCINT2_vect) instead of ISR(PCINT_vect)

@DuaneB:
I will definitely study those pages!

PaulS, DuaneB: thanks for your help!