Simple communication between 2 Arduinos

I have one Arduino which emits regular pulses on digital pin 11.

I've connected that pin directly to pin 5 on a second Arduino.

I create an interrupt on pin 5 to fire on rising edge.

But I'm never seeing it fire (my counter variable remains at 0). Both Arduinos are powered by the same power supply and share an earth. Should this work, or am I missing something basic?

Here's the reader code:

#include <Wire.h>

const byte interruptPin = 5;
volatile int counter;

void pulseHandler() {
  counter++;
}

void setup() {
   Serial.begin(57600);
   pinMode(interruptPin, INPUT);
   attachInterrupt(digitalPinToInterrupt(interruptPin), pulseHandler, FALLING); 
   counter = 0;
}

void loop() {
  Serial.print("Counter is "); Serial.println(counter);
  delay(1000);
}

And here's the writer code (sorry it's a bit convoluted, it's intended to simulate an RPM input on a car)

/*
   UNO board.
   Emits pulses for testing of the RPM meter.
   20 ms between pulses (at 3000 rpm) to 90 ms between pulses (at 666 rpm)
   One 0.1 ms pulse each pulse time during 10 s.
   Then next pulse time in sequence until 90 ms.
*/

unsigned long M = 0;    //time

const int     out = 11;       //pulse output pin

const int     rpm  = 200;
int           pulseWidth = 10;    // ms

int           freq;
unsigned long timeBetweenPulses;
unsigned long lastStart = 0;
int           pulseTimer = 0;

void setup()
{
  pinMode(out, OUTPUT);
  // flash LED as well as raising pin 11
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(57600);
  delay(1000);     //delay 1 s to allow time to start serial monitor
  freq = rpm/60;
  timeBetweenPulses = 1000/freq;
  Serial.print  ("LED_BUILTIN is "); Serial.println(LED_BUILTIN);
  Serial.println("Frequency is "); Serial.println(freq);
  Serial.println("timeBetweenPulses is "); Serial.println(timeBetweenPulses);
  lastStart = millis();
}

void loop()
{
  M = millis();     //read millis and save
  if (M - lastStart >= timeBetweenPulses) {
    digitalWrite(out, HIGH);
    digitalWrite(LED_BUILTIN, HIGH);
    pulseTimer = M;
    lastStart = M;
  }
  if (pulseTimer != 0 && M >= pulseTimer + pulseWidth) {
    digitalWrite(out, LOW);
    digitalWrite(LED_BUILTIN, LOW);
    pulseTimer = 0;
  }
}

If the controller is an UNO the builtin LED is on pin 13, not 11. That means that no pulse is sent out on pin 11.

I write to pin 11 as well as the inbuilt LED (and yes, it is a UNO. The receiver is a Nano).

Yes You do. My mistake.
Can You use a multimeter and verify any action on pin 11?
I'll fetch a can of beer and look again.

Is pin 5 an external interrupt on an Uno? No, then you need to set up a pin change interrupt for pin 5 or use one of the external interrupt pins.

groundFungus:
Is pin 5 an external interrupt on an Uno? No, then you need to set up a pin change interrupt for pin 5 or use one of the external interrupt pins.

Thanks! That was beyond my knowledge.

groundFungus:
Is pin 5 an external interrupt on an Uno? No, then you need to set up a pin change interrupt for pin 5 or use one of the external interrupt pins.

Ah. The receiving unit is a Nano. I didn’t realise you were limited to certain pins for interrupts. I’ll investigate further. Many thanks.

!

Nano uses the same processor as the Uno (mega328) so the interrupts are the same. External interrupts are pins 2 and 3.

Since your counter variable is multibyte (an int is 2 bytes) you need an interrupt guard when you read the value of counter. If you do not, it is possible that, while you are reading it, an interrupt can occur and change the value before the read is finished causing erroneous output. So disable interrupts, copy the value of counter and enable interrupts. Then use the copy in the rest of the code.

Example of interrupt guard: (removed the nasty delay and used blink without delay timing)

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 1000;
   if (millis() - timer >= interval)
   {
      timer = millis();
      Serial.print("Counter is ");
      noInterrupts;
      int copyOfCounter = counter;
      interrupts;
      Serial.println(copyOfCounter);
   }
}

Non-blocking timing tutorials:
Several things at a time.
Beginner's guide to millis().
Blink without delay().

groundFungus:
Nano uses the same processor as the Uno (mega328) so the interrupts are the same. External interrupts are pins 2 and 3.

Since your counter variable is multibyte (an int is 2 bytes) you need an interrupt guard when you read the value of counter. If you do not, it is possible that, while you are reading it, an interrupt can occur and change the value before the read is finished causing erroneous output. So disable interrupts, copy the value of counter and enable interrupts. Then use the copy in the rest of the code.

Example of interrupt guard: (removed the nasty delay and used blink without delay timing)

void loop()

{
  static unsigned long timer = 0;
  unsigned long interval = 1000;
  if (millis() - timer >= interval)
  {
      timer = millis();
      Serial.print("Counter is ");
      noInterrupts;
      int copyOfCounter = counter;
      interrupts;
      Serial.println(copyOfCounter);
  }
}




Non-blocking timing tutorials:
[Several things at a time.](https://forum.arduino.cc/index.php?topic=223286.0)
[Beginner's guide to millis().](https://forum.arduino.cc/index.php?topic=503368.0)
[Blink without delay().](https://www.arduino.cc/en/tutorial/BlinkWithoutDelay)

That's true real time programming and a reason to stay away from Interrupts as long as possble. Nothing for the unexperienced beginner.

groundFungus:
Nano uses the same processor as the Uno (mega328) so the interrupts are the same. External interrupts are pins 2 and 3.

Since your counter variable is multibyte (an int is 2 bytes) you need an interrupt guard when you read the value of counter. If you do not, it is possible that, while you are reading it, an interrupt can occur and change the value before the read is finished causing erroneous output. So disable interrupts, copy the value of counter and enable interrupts. Then use the copy in the rest of the code

That’s great info, I’ll be sure to do that.