I am wondering if it is possible to trigger an ISR routine with a digitalWrite i.e., by software rather than hardware (if that makes sense?).
I have provided some code below which I am trying to get to work.
The Arduino I am using is a Mega 2560 where the interrupt pins are 2, 3, 18, 19, 20, 21.
#define InterruptPin_1 19
#define InterruptPin_2 20
#define InterruptPin_3 21
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(115200);
Serial.println(" ");
Serial.println(" Start ");
pinMode(InterruptPin_1, INPUT_PULLUP);
pinMode(InterruptPin_2, INPUT_PULLUP);
pinMode(InterruptPin_1, INPUT_PULLUP);
// Write High First
digitalWrite(InterruptPin_1, HIGH);
digitalWrite(InterruptPin_2, HIGH);
digitalWrite(InterruptPin_3, HIGH);
attachInterrupt(digitalPinToInterrupt(InterruptPin_1), []() {
Serial.println("First Interrupt Pin Triggered");
}, RISING);// Attach First Interrupt
attachInterrupt(digitalPinToInterrupt(InterruptPin_2), []() {
Serial.println("Second Interrupt Triggered");
}, RISING);// Attach Second Interrupt
attachInterrupt(digitalPinToInterrupt(InterruptPin_3), []() {
Serial.println("Third Interrupt Triggered");
}, RISING);// Attach Third Interrupt
Serial.println("Interrupts Attached");
delay(5000);
digitalWrite(InterruptPin_1, LOW);
delay(1000);
digitalWrite(InterruptPin_1, HIGH);
// "First Interrupt Pin Triggered" should be printed
delay(5000);
digitalWrite(InterruptPin_2, LOW);
delay(1000);
digitalWrite(InterruptPin_2, HIGH);
// "Second Interrupt Pin Triggered" should be printed
delay(5000);
digitalWrite(InterruptPin_3, LOW);
delay(1000);
digitalWrite(InterruptPin_3, HIGH);
// "Third Interrupt Pin Triggered" should be printed
Serial.println("Digital Writes Completed");
}
void loop() {
}
I think we understand what you are trying to do, but why you are trying to do it does not make sense, so please explain because you may have a misconception about something fundamental.
As mentioned by @Whandall , when a pin is in INPUT mode, writing HIGH or LOW enables or disables the internal pull-up resistor on the pin. This is not guaranteed to change the voltage on the pin, unless you have a suitable external pull-down resistor (eg. ~500K).
@Whandall
The changes suggested got it working for me.
Also, I was unaware about the potential issue of the Serial.println in an ISR. It doesn't seem to be a problem in this case but good to know for proper projects.
@PaulRB
I am basically just trying to simulate a poke that is a IR - phototransistor pair which I do not have access to at the moment.
The Phototransistor Pin is an input pin, and the IR LED is an output pin. When the light is blocked, the voltage rises on the transistor pin and triggers an ISR.
#define InterruptPin_1 19
#define InterruptPin_2 20
#define InterruptPin_3 21
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(115200);
Serial.println(" ");
Serial.println(" Start ");
pinMode(InterruptPin_1, OUTPUT);// Change to OUTPUT $
pinMode(InterruptPin_2, OUTPUT);// Change to OUTPUT $
pinMode(InterruptPin_3, OUTPUT);// Change to OUTPUT $
// Write High First
digitalWrite(InterruptPin_1, LOW); // Change to Low $
digitalWrite(InterruptPin_2, LOW); // Change to Low $
digitalWrite(InterruptPin_3, LOW); // Change to Low $
attachInterrupt(digitalPinToInterrupt(InterruptPin_1), []() {
Serial.println("First Interrupt Pin Triggered");
}, RISING);// Attach First Interrupt
attachInterrupt(digitalPinToInterrupt(InterruptPin_2), []() {
Serial.println("Second Interrupt Triggered");
}, RISING);// Attach Second Interrupt
attachInterrupt(digitalPinToInterrupt(InterruptPin_3), []() {
Serial.println("Third Interrupt Triggered");
}, RISING);// Attach Third Interrupt
Serial.println("Interrupts Attached");
delay(5000);
digitalWrite(InterruptPin_1, LOW);
delay(1000);
digitalWrite(InterruptPin_1, HIGH);
// "First Interrupt Pin Triggered" should be printed
delay(5000);
digitalWrite(InterruptPin_2, LOW);
delay(1000);
digitalWrite(InterruptPin_2, HIGH);
// "Second Interrupt Pin Triggered" should be printed
delay(5000);
digitalWrite(InterruptPin_3, LOW);
delay(1000);
digitalWrite(InterruptPin_3, HIGH);
// "Third Interrupt Pin Triggered" should be printed
Serial.println("Digital Writes Completed");
}
// the loop function runs over and over again until power down or reset
void loop() {
}