OzMaz
June 2, 2022, 1:44pm
1
Hi,
More issues.
This program doesn't work at all, but I am mystified why digitalPinToInterrupt returned a negative value.
The program returns 26, -1. Is that right?
#define I_PIN_KEY_PRESSED 26
volatile boolean KeyPressed = false;
static void KeyPressedISR(void) {
KeyPressed = true;
digitalWrite(LED_BUILTIN, HIGH);
}
void setup() {
pinMode(I_PIN_KEY_PRESSED, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
attachInterrupt(digitalPinToInterrupt(I_PIN_KEY_PRESSED), KeyPressedISR, RISING);
Serial.begin(9600);
Serial.println();
Serial.print(I_PIN_KEY_PRESSED);
Serial.print(", ");
Serial.print(digitalPinToInterrupt(I_PIN_KEY_PRESSED));
}
void loop() {
//Serial.println("");
//Serial.print(KeyPressed);
//KeyPressed = false;
delay(2000);
}
Did you mean
Serial.print("I_PIN_KEY_PRESSED");
Serial.print(", ");
Serial.print(digitalRead(I_PIN_KEY_PRESSED));`
cncnotes:
Did you mean
No, the OP literally asks why
digitalPinToInterrupt(I_PIN_KEY_PRESSED)
is -1.
Which is what prompted @anon73444976 to ask for what board.
26 is not a valid pin in this case, it seems, and a retrun value of -1 is the way you find that out sometimes.
a7
There is rarely a need to use an interrupt for a key press if your code is written properly. See the following tutorial:
StateChangeDetection
Good question. Indeed depending on the board, only some specific pins can be used for interruptions.
OzMaz
June 2, 2022, 3:06pm
7
The board I'm using is a Arduino Mega.
What are you trying to achieve with your program?
May be an X-Y problem, which is why I asked
OzMaz
June 2, 2022, 11:57pm
11
I really caused this problem myself, so I'm putting my hand up.
I was testing the hardware using a Mega intending to shift everything to a Due and made the simple but wrong assumption the two were the same, not so.
All of the digital pins on a Due can be configured to interrupt. Not so on a Mega where there are only 6 interrupt pins and are dedicated to a specific pin.
I've learnt something: Some things are not the same!
Tested the board using pin 26 as an interrupt and it works as expected.
Now to move on and get the address switches and LED output's going.
Thanks for the help.
system
Closed
November 29, 2022, 11:58pm
12
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.