Thank you for the quick reply again!
To answer your last comment first. I am trying to understand how interrupts work on the MEGA 2560. There is a large sketch that I am working with that uses interrupts and I am just trying to learn how they work. I then spotted this discrepancy between how I thought the external interrupt pins worked on the MEGA 2560 and how they actually work.
My response to your first comment
Trans_pin_CL (pin 18) and Trans_pin_CC (pin 19) are set High, otherwise there is no voltage at those pins at all. I want the voltage set high on those pins.
Trans_pin_CR (pin 20) is constantly HIGH. I can't set pin 20 to LOW. The interactive board viewer appears to indicate that those pins on the MEGA 2560 are fixed to 5V with 10kOhm resistors?
The setup works perfectly for pin 18 and pin 19 ( 2 & 3 as well ).
It is pin 20 (and 21) that do not function as I expected.
My goal is to try and understand why pins 20 & 21 do not function like the other pins.
A stretch goal may be to try and get them to function like the other pins.
I have updated my code to reflect your detachinterrupts() comment also.
It's weird, it worked without doing that....
/*
Name: IR_Sensor.ino
Created: 10/26/2020 9:15:22 AM
Author: cilli
*/
#define Trans_pin_CL 18
#define Trans_pin_CC 19
#define Trans_pin_CR 20
#define IRMP A14 // Measure Voltage
#define IRMN A15 // Measure Voltage
volatile int Value = 0;
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(9600);
pinMode(Trans_pin_CL, INPUT);
digitalWrite(Trans_pin_CL, HIGH);
pinMode(Trans_pin_CC, INPUT);
digitalWrite(Trans_pin_CC, HIGH);
pinMode(Trans_pin_CR, INPUT);
digitalWrite(Trans_pin_CR, LOW);
pinMode(IRMP, INPUT);
pinMode(IRMN, INPUT);
EIFR = bit(INTF5); // clear flag for interrupt 5
EIFR = bit(INTF4); // clear flag for interrupt 4
EIFR = bit(INTF3); // clear flag for interrupt 3
EIFR = bit(INTF2); // clear flag for interrupt 2
EIFR = bit(INTF1); // clear flag for interrupt 1
EIFR = bit(INTF0); // clear flag for interrupt 0
attachInterrupt(digitalPinToInterrupt(Trans_pin_CL),
[]() {Value = 1; },
RISING);
attachInterrupt(digitalPinToInterrupt(Trans_pin_CC),
[]() {Value = 2; },
RISING);
attachInterrupt(digitalPinToInterrupt(Trans_pin_CR),
[]() {Value = 3; },
RISING);
}
// the loop function runs over and over again until power down or reset
void loop() {
delay(3 * 1000);
Serial.println(" ");
Serial.print("IR Pos Pin = "); // Display "A3 = "
Serial.print(volts(IRMP)); // Display measured A3 volts
Serial.println(" volts");
Serial.print("IR Neg Pin = "); // Display "A3 = "
Serial.print(volts(IRMN)); // Display measured A3 volts
Serial.println(" volts");
detachInterrupt(digitalPinToInterrupt(Trans_pin_CL));
detachInterrupt(digitalPinToInterrupt(Trans_pin_CC));
detachInterrupt(digitalPinToInterrupt(Trans_pin_CR));
WhichPin(Value);
Serial.println(Value);
delay(1 * 1000);
Value = 0;
attachInterrupt(digitalPinToInterrupt(Trans_pin_CL),
[]() {Value = 1; },
RISING);
attachInterrupt(digitalPinToInterrupt(Trans_pin_CC),
[]() {Value = 2; },
RISING);
attachInterrupt(digitalPinToInterrupt(Trans_pin_CR),
[]() {Value = 3; },
RISING);
}
float volts(int adPin) // Measures volts at adPin
{ // Returns floating point voltage
return float(analogRead(adPin)) * 5.0 / 1024.0;
}
void WhichPin(int Value) {
if (Value == 1) {
Serial.println(" ");
Serial.println("CL Trans Pin Triggered");
Serial.println(" ");
}
else if (Value == 2) {
Serial.println(" ");
Serial.println("CC Trans Pin Triggered");
Serial.println(" ");
}
else if (Value == 3) {
Serial.println(" ");
Serial.println("CR Trans Pin Triggered");
Serial.println(" ");
}
else {
Serial.println(" ");
Serial.println("No Trans Pin Triggered");
Serial.println(" ");
}
}
Again, thanks for taking your own personal time to help.
It is very much appreciated.
And if you don't have time to respond to this post, no offense taken!!
Regards
Cillian