I am trying to use pin change interrupt with my Arduino Uno R4 Minima following this tutorial, and in particular using this code found on the website:
/*
Pin Change Interrupt Test
pin-change-test.ino
Demonstrates use of Pin Change Interrupt
Input on D7, LED on D13
DroneBot Workshop 2022
https://dronebotworkshop.com
*/
// LED and switch
const byte ledPin = 13;
const byte buttonPin = 7;
// Boolean to represent toggle state
volatile bool togglestate = false;
void setup() {
// Set LED as output
pinMode(ledPin, OUTPUT);
// Set switch as input with pullup
pinMode(buttonPin, INPUT_PULLUP);
// Enable PCIE2 Bit3 = 1 (Port D)
PCICR |= B00000100;
// Select PCINT23 Bit7 = 1 (Pin D7)
PCMSK2 |= B10000000;
}
void loop() {
// No code in Loop
}
ISR (PCINT2_vect)
{
// Interrupt for Port D
// Invert toggle state
togglestate = !togglestate;
// Indicate state on LED
digitalWrite(ledPin, togglestate);
}
It, naturally, does not even compile because the Arduino Uno R4 Minima has a Renesas RA4M1 microcontroller instead of whatever the tutorial is using. In particular, the variable names PCICR, PCMSK2, PCINT2_vect are different for the Renesas.
My problem is that I do not understand what is written there and what I should be looking for, as the vocabulary of the tutorial is different from the vocabulary of the documentation full of acronyms.
My question is therefore: does anyone knows of a dictionary that provides this information, or tell me how to decipher the documentation so that I find these variables names I am looking for to copy/paste in the code?
Is Pin Change Interrupt even possible with Arduino Uno R4 Minima?
(Note that I know about attachInterrupt(), but it does not fit my end goal as I would need 4 pins to interrupt instead of the only two existing in the Arduino Uno R4 Minima.)
You might look at the attachInterrupt functions. Example:
// LED and switch
const byte buttonPin = 2;
void setup() {
// Set LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Set switch as input with pullup
pinMode(buttonPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(buttonPin), &my_isr, CHANGE);
}
void loop() {
// No code in Loop
}
void my_isr()
{
digitalWrite(LED_BUILTIN, digitalRead(buttonPin));
}
When pin 2 changes it will set the built in LED to it's state...
Thank you, but as I mention at the end of my post, I specifically cannot use attachInterrupt because, in the end, I would need 4 pins able to support the function, while only 2 are available on an Arduino Uno R4 Minima.
This is not what this document is saying.
I tried to use pin 12 and pin 13 (so with attachInterrupt(digitalPinToInterrupt(12), &my_isr, CHANGE); ) in your code, without success, whereas attachInterrupt(digitalPinToInterrupt(2), &my_isr, CHANGE); would work.
Naturally, I would plug the button into pin 12 and 2 respectively. Am I missing something?
I made a test using 4 buttons on pins 1, 2, 3 & 4 and it is working fine for me, I made a function that changes the status of the built-in LED using any of these four buttons.
This is my sketch:
// LED and switch
const byte buttonPin1 = 0;
const byte buttonPin2 = 1;
const byte buttonPin3 = 2;
const byte buttonPin4 = 3;
bool status = 1;
void setup() {
// Set LED as output
pinMode(LED_BUILTIN, OUTPUT);
// Set switch as input with pullup
Serial.begin(9600);
pinMode(buttonPin1, INPUT_PULLDOWN);
pinMode(buttonPin2, INPUT_PULLDOWN);
pinMode(buttonPin3, INPUT_PULLDOWN);
pinMode(buttonPin4, INPUT_PULLDOWN);
attachInterrupt(digitalPinToInterrupt(buttonPin1), &my_isr, HIGH);
attachInterrupt(digitalPinToInterrupt(buttonPin2), &my_isr, HIGH);
attachInterrupt(digitalPinToInterrupt(buttonPin3), &my_isr, HIGH);
attachInterrupt(digitalPinToInterrupt(buttonPin4), &my_isr, HIGH);
}
void loop() {
digitalWrite(LED_BUILTIN, status);
Serial.print("Button 1: ");
Serial.println(digitalRead(buttonPin1));
Serial.print("Button 2: ");
Serial.println(digitalRead(buttonPin2));
Serial.print("Button 3: ");
Serial.println(digitalRead(buttonPin3));
Serial.print("Button 4: ");
Serial.println(digitalRead(buttonPin4));
Serial.print("Status: ");
Serial.println(status);
delay(300);
}
void my_isr() {
status = !status;
}
In my code, all the buttons "activate" the same interruption functions when the state is HIGH and it works properly for me.