I am trying to change an analogue pin (#define PIN_HOLD A0) on the fly but running into an issue. The pin is initially set to an output in setup() and held high - no problem. In a function I then change it to an input and perform and A/D read - again no problem and I get the expected value.
I then try to set it back to an output and take it high again but the pin just stays as an input.
If I comment out the analogRead line the pin will happily switch from input to output repeatedly, it just won't play ball after performing an A/D read.
Any ideas very welcome?
pinMode(PIN_HOLD, INPUT); //Make the power hold pin an input
delay(20); // probably not needed
AD_Val = analogRead(PIN_HOLD); //Perform an A/D read to check for switch activity
delay(20);
pinMode(PIN_HOLD, OUTPUT); //turn the pin back to an output to maintain the power
digitalWrite(PIN_HOLD, HIGH);
I have used the same technique on the Arduino Zero/SAMD21 and it works fine.
i believe pinMode () only sets the direction when using digitWrite/Read().
not sure why you would want to set the pin to OUTPUT if it's an input. there's a reason pins are by default set to INPUT because you don't want 2 output drivers fighting one another and possibly damaging one or the other
The port is well protected by a 300k resistor so no harm can be done to it when in output mode.
Everything I am doing relates to the same single port so port to port conflicts can't happen.
looks like you're attempting to read an analog voltage presumably being driven by some external device and want to reconfigure the pins you're reading the voltage from as an OUTPUT.
driving the pin as an OUTPUT may damage the device you're reading from or that device many damage the pin on the Arduino.
don't understand why you would need to configure the pin as an OUTPUT
My oscilloscope shows that the analogue signal is still present via a 300k resistor network and hasn't been forced high by the digitalWrite command following the A/D read.
monitoring the power button in order to instigate a shutdown
Using a single port for 2 functions is a very useful technique to reduce IO requirements and this has always worked fine on other processors - just not on the nRF52840 SoC.
I have tried various permutations of setting the port high/low/input/output before and after doing the A/D read but nothing cures this issue. It's looking like I will need to use an additional port such that one does the A/D read and the other holds the line high in between the A/D reads