digitalWrite and/or digitalRead to Input and/or Output pins

I have a general question regarding I/O pins.
If I set up a digital pin as an Input, can I subsequently Write to it, i.e., change its boolean state? Same for an Output pin; can I read its state at any particular time? Is there a preferred way to handle these issues?
I know that his is basic stuff to most, but I am still getting started with this world.
Thanks for any nonjudgmental reply. :smiley:

nkuck:
I have a general question regarding I/O pins.
If I set up a digital pin as an Input, can I subsequently Write to it, i.e., change its boolean state?

Calling digitalWrite() will turn on/off the internal pull up. If you want to drive the pin you have to make it an output which you can do on the fly. Although it's common to define pins as input or output in the setup() it's not static and you can change it at any time.

nkuck:
Same for an Output pin; can I read its state at any particular time?

No problem.

nkuck:
Is there a preferred way to handle these issues?

If you go from input to output and the state matters the moment it becomes an output call digitalWrite() first and then call pinMode(). Other then that, no.

Thanks for the informative response. I could not find a definitive answer online.

Here is what I am trying to accomplish with little/no success. Trying to check the state of a pin and flip it.

void incorrectPIN() { // do this if incorrect PIN entered
  Serial.println("@ incorrect PIN ");
  delay(2000);
  digitalRead(Alarm_OutPin);
  if (Alarm_OutPin == LOW) {
    pinMode(Alarm_OutPin, INPUT);digitalWrite(Alarm_OutPin, HIGH);
  }
  // if the LIGHT is off turn it on and vice-versa:
    digitalRead(Light_OutPin);
    if (Light_OutPin == LOW) {
    pinMode(Light_OutPin, INPUT);digitalWrite(Light_OutPin, HIGH);
    PIR_SensorPin = LOW;
    started = true;
  alarm();
}
}

What am I doing wrong? It reads just fine to me!
By the way, the Alarm_OutPin and Light_OutPin was initially declared as Outputs, while the PIR_SensorPin was an Input.

Fist thing I notice:

digitalRead(Alarm_OutPin);
  if (Alarm_OutPin == LOW) {

You read a pin and throw the reading away after which you compare the pin NUMBER with LOW...

And please post all your code next time (or at least a compilable simplification). Snippets may go to http://snippets-r-us.com/

  // if the LIGHT is off turn it on and vice-versa:
digitalWrite(Light_OutPin, !digitalRead(Light_OutPin));

Nice. Thanks.