I'm working on a program which listens for certain bytes on serial through an if/then/elseif ladder. I want to also check if a pin (say, A15
) has been given a HIGH state, by another Arduino with shared ground. The slave Arduino will turn a digital pin to HIGH and I want the MEGA2560 to check every time it loops if a HIGH is present from the other board. A "While button not pressed do nothing statement won't work, because that won't let the loop go. I tried doing an if statement, using something to the effect of this:
if (digitalRead(A15)==HIGH) {
//things to do
}
But it seems no matter what I put, be it HIGH
, LOW
, != HIGH
, != LOW
, etc. or either input pinmodes, pullup or not, it always thinks the board is transmitting HIGH. In fact, it only doesn't think it's HIGH when it actually is HIGH. I'm pretty stumped on this. If you ask me, digitalRead is really odd with what's HIGH and what's LOW.
1 Answer 1
Simplify, and show your setup code.
First, do you have a pinMode call in your setup function:
void setup() {
pinMode(A15, INPUT_PULLUP);
Serial.begin(115200);
}
Then use test code like this:
void loop() {
if (digitalRead(A15) == HIGH) {
Serial.println("Pin A15 is HIGH");
} else {
Serial.println("Pin A15 is LOW");
}
}
Connect your A15 pin directly to ground with a jumper and run the sketch above. Open the serial monitor and see what is output. It should be LOW. Then disconnect the jumper and check again. It should now be HIGH. Finally, connect the jumper between A15 and +5V and check the output again. It should still show HIGH.
That lets you troubleshoot just the software piece. Once you get that working, connect pin A15 to the remote Arduino's output pin and try again.
-
I'll try this as soon as I get to the board tomorrow.SYGMAH– SYGMAH2019年04月12日 00:36:42 +00:00Commented Apr 12, 2019 at 0:36
Explore related questions
See similar questions with these tags.
INPUT
inSetup()
,pinMode(A15, INPUT);
? Could you show us the entire sketch you are using?