I have a very simple test sketch in which I\'m trying to set a pin to HIGH
and then read its state with digitalRead
. Here is my sketch.
<
I wrote a routine for flashing four different LEDs for different purposes but I also wanted to retain their initial state before I flash them, as their steady state also tells me something is occurring in my code, so here is my Flash code, you call it by sending the pin number, the number of times to flash it and how long to flash it for.
inline void Flash (byte pinNum, byte times, byte flashSpeed)
{
bool state; // Local var for State of pin
state = digitalRead(pinNum); // Read the current state as set elsewhere
int x; // Local var for repeat flash
for (x = 0; x < times; x++)
{
digitalWrite(pinNum, HIGH); // Turn on LED
delay(flashSpeed);
digitalWrite(pinNum, LOW); // Turn off LED
delay(flashSpeed * 2); // leave off twice as long as on
} // due to persistence of Vision
digitalWrite(pinNum, state); // Restore the original state of the LED
}
Your sketch should be
void setup()
{
Serial.begin(9600);
}
void loop()
{
delay(1000);
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
delay(1000);
// pinMode(3, INPUT); // get rid of this line
Serial.println(digitalRead(3));
}
That's all. Then it reads the pin's state which in your case is "HIGH". If you set the pinMode to input it will read the input depending on what is connected. If you are writing "HIGH" to an input pin the internal pullup will be activated. It does not matter if you write HIGH before setting it to input mode or after setting it to input mode. Unless of course you are driving a load that is to high for the output pin (e.g. a switch to ground). Then this would probably kill the pin.
If you have written a low and set the pin to low it might float which may lead to any kind of unpredictable behaviour.
Didn't like any of the previous answers, because they would not be cross-arduino-platform compatible. You need to access through the pin reference tables. The following expression does the trick.
bool value = (0!=(*portOutputRegister( digitalPinToPort(pin) ) & digitalPinToBitMask(pin)));
Let me break that down for better understanding
digitalPinToPort(pin)
look up you the gpio bank [port] that the pin is assigned to on your selected hardware
portOutputRegister(...)
gives you a pointer to the port containing the value you are looking for. * dereferences the pointer and gives the full value of all 8 pins assigned to that port. It is not yet uniquely useful, but the bit you are looking for is in there.
&digitalPinToBitMask(pin)
selects only the bit you are interested in for the pin, all other bits will be zero.
0!= tests to see if the resulting expression is zero, or something else. If it is zero, then your output is zero on that pin. Otherwise the output is one.
In this case you just want to access the data register itself.
PORTB and PORTD registers contain the pin data you are looking for. I finally got access to an Arduino to figure it out. You want to use bitRead(PORTD, pin)
.
Serial.println(bitRead(PORTD,3)); //Reads bit 3 of register PORTD which contains the current state (high/low) of pin 3.
Reference Bit Read Operation for more information.
ALways keep in mind. If you are trying to Configure anything ,Just keep it in setup file. Since setup file get executed once , If you are setting In loop . it execute continuous.and try to keep it remain it start state.That is active low state