Hello all, I just got a new Due board and was trying out some simple programs and ran into an odd issue. I'm somewhat new to Arduino, but I've done a lot of work with PICs and general C programming, so maybe I'm just approaching this the wrong way.
I am trying to read the state of a digital pin (specifically the LED pin) when it's in OUTPUT mode, however, it always returns zero. See the code below for an example:
#define LED_PIN 13
unsigned long led_counter = 0;
void setup()
{
pinMode(LED_PIN, OUTPUT);
Serial.begin(19200);
while (!Serial);
}
void loop()
{
if(led_counter%2 == 0)
{
digitalWrite(LED_PIN,HIGH);
}
else
{
digitalWrite(LED_PIN,LOW);
}
Serial.print(digitalRead(LED_PIN));
delay(1000);
led_counter++;
}
The serial monitor just shows a string of "0"s, when it should return alternating "0" and "1". So no matter what the actual output of the pin is digitalRead() seems to return LOW. The LED is actually blinking, so I know the pin state is changing.
After googling around it seems doing a digitalRead() of an output pin should theoretically work. However, I didn't find any cases using a Due, so I wondered if maybe that was the issue. Maybe the different I/O mapping in the ARM core has something to do with it?
Any thoughts or suggestions would be much appreciated! Thanks guys! Liam
P.S. Just to preempt the question: "Why would you want to read an output value you just set?". The I/O register is already storing the value of the pin, so I should just be able to do something like this:
digitalWrite(~digitalRead(LED_PIN));
Also, it just makes sense that digitalRead() should return the value of the pin regardless of the tristate.