defragster
Senior Member+
I made an error with #defines and ended up re-using the same pin for two functions one INPUT and one OUTPUT. This won't work obviously and my simplified code demonstrates this error, in the first two lines. It was subtly mixed in with other stuff I need to clean up - but moving it from three working boards with a unique array of pins to a fourth board the change crept in.
The code below shows anomalous behavior it seems to me that made debugging it not what it first seemed it would be. Second post to follow doing this on LED_BUILTIN pin acts differently
Intro: I set up an OUTPUT pin to LOW. ( Correct ) The same pin is then set out INPUT. ( Error )
Logically: Given the pin is now INPUT I would expect any attempt to write to the pin to be ignored.
Problem: The code below shows the pin will take a digitalWrite(LED_BUILTIN, HIGH); but not a corresponding digitalWrite(LED_BUILTIN, LOW);
Question: Is there any reason to expect or consider this behavior normal, or is it a bug?
Sample: Code below demonstrates the problem. I put in a 10 sec wait between the three steps as the LED_BUILTIN changes to match the pin 3 value as read for confirmation.
The code below shows anomalous behavior it seems to me that made debugging it not what it first seemed it would be. Second post to follow doing this on LED_BUILTIN pin acts differently
Intro: I set up an OUTPUT pin to LOW. ( Correct ) The same pin is then set out INPUT. ( Error )
Logically: Given the pin is now INPUT I would expect any attempt to write to the pin to be ignored.
Problem: The code below shows the pin will take a digitalWrite(LED_BUILTIN, HIGH); but not a corresponding digitalWrite(LED_BUILTIN, LOW);
Question: Is there any reason to expect or consider this behavior normal, or is it a bug?
Sample: Code below demonstrates the problem. I put in a 10 sec wait between the three steps as the LED_BUILTIN changes to match the pin 3 value as read for confirmation.
Code:
#define THREE 3
[B]#define FOUR 3[/B] // This represents the coding error I made
elapsedMillis aTime;
void showstatus( String info );
void setup() {
Serial.begin(9600);
while (!Serial && (millis () <= 5000)) ; // Wait for Serial to come online
Serial.println("\n\n\nHello Out & In Error");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
pinMode(THREE, OUTPUT);
digitalWrite(THREE, LOW);
[B] pinMode(FOUR, INPUT);[/B]
showstatus( " setup() : user err OUTPUT redefined INPUT " );
}
void loop() {
if ( aTime >= 10000 && aTime <10001) {
showstatus( " loop() NO CHANGE" );
delay(2); // prevent re-entry this millisecond
}
if ( aTime >= 20000 && aTime <20001) {
digitalWrite(THREE, HIGH); // set INPUT HIGH - WORKS
showstatus( " loop() Set INPUT HIGH - THIS WORKS ???" );
delay(2); // prevent re-entry this millisecond
}
if ( aTime >= 30000 && aTime <30001) {
digitalWrite(THREE, LOW); // set INPUT LOW - FAILS
showstatus( " loop() Set INPUT LOW - THIS FAILS ???" );
delay(2); // prevent re-entry this millisecond
}
}
void showstatus( String info ) {
Serial.print(info);
Serial.print(" time at: ");
Serial.println(aTime);
digitalWrite(LED_BUILTIN, digitalRead( THREE ));
if ( digitalRead( THREE ) ) Serial.print(" INPUT pin THREE reads HIGH ");
else Serial.print(" INPUT pin THREE reads LOW ");
Serial.println();
}