Display the state of an output pin on the console

Hi guys, its been years. I have push button connected to pin A0 and when it is depressed it is supposed to send a signed to digital pin2 and set it to High.

The code seems to be working well, however I want to verify by seeing 0 zeros and 1 ones, the state of pin 2.

When I type in Serial.println(SIGOUT); I get the pin number #2.
can someone please take a look at the code and help out please?

//Constants and Variables used in code below.
int SIGOUT = 2;  //Variable to signal out to Relay
int ledPin = 13; //On-board LED
int Pin2State = digitalRead (SIGOUT);
const int Button = A0; // Pin used for input as a High / Low

int buttonState = 0;   //Begining button state value


// Program Start.


void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(Button,INPUT_PULLUP);         // A0 Set as digital input for button
    pinMode(SIGOUT,OUTPUT);               //Sets Pin as output
    Serial.begin(9600);                   //setup serial
}

void loop() {

digitalRead(Pin2State);
Serial.println(Pin2State);


// read the state of the pushbutton value:
  buttonState = digitalRead(Button);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    digitalWrite(SIGOUT, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
    digitalWrite(SIGOUT, LOW);
    }

}

post a picture of your serial monitor and explainn again, what u want exactly! Write down the output of ur serial monitorr and make a picture of ur wiring. Then for sure people can follow up with ur problem. Best regards.

When I type in Serial.println(SIGOUT); I get the pin number #2.

the value of SIGOUT is 2

don’t you want to

Serial.println (digitalRead (SIGOUT));

Not quite sure why you are reading from an OUTPUT pin but you can do that if you want..

Pin2State = digitalRead(SIGOUT);
Serial.println(Pin2State);

And also if you are simply mirroring the button to the outputs you do not need an if...

buttonState = digitalRead(Button);
digitalWrite(ledPin, buttonState);
digitalWrite(SIGOUT, buttonState);
int Pin2State = digitalRead (SIGOUT);

You should not read hardware before you know that it's initialised. And that is only once setup() starts
2)
So Pin2State is either 0 or 1.

So the above should be

int Pin2State
...
...

void setup()
{
  ...
  ...
  Pin2State = digitalRead(SIGOUT);
}

...
...
void loop() {

digitalRead(Pin2State);
  Serial.println(Pin2State);

Pin2State is either 0 or 1 (based on the digitalRead mentioned above); so you read either pin 0 or pin 1. And next throw the result away.

Thank you guys, all good info. I was able to get it to work wit OUTPUTPIN=digitalRead(SIGOUT);
This gives me a 1 if High or 0 if low which is what I wanted for QC purposes.

Just to eliminate some confusion on my part,
when you have

int VARNAME = 2; // That means that VARNAME equals "2" whereas if I say..
int VARNAME; // I am just creating an "empty" Integer placeholder?

Also, what goes in the void setup ? why not out everything in loop?
Thank you

int VARNAME = 2; // That means that VARNAME equals "2" whereas if I say..
int VARNAME; // I am just creating an "empty" Integer placeholder?

If the declaration is in global scope, the value of VARNAME is 0.

If the declaration is in local scope the value of VARNAME can be anything (what ever happens to be in that memory location(s). Uninitialized local variables can bite you if misused.

FYI, all capital object names, by convention, means that it is a constant (not variable) and declared like:

const int VARNAME = 2; 
//or
#define VARNAME 2

int VARNAME = 2; // Declares a new variable VARNAME AND assigns it the value 2
int VARNAME; // Declares a new variable VARNAME
VARNAME = 2; // Assigns a (previously declared) variable VARNAME the value 2

If you find yourself with multiple “int VARNAME” with the same name in the same program you’ve almost certainly not understood. This is the programming equivalent of calling both your children “Bob”. Perfectly legal, and you can do it if you want, but you’d be crazy to try as sooner or later you’ll get confused about which “Bob” you mean.

Also, what goes in the void setup ? why not out everything in loop?

Quite obviously, I would have thought, “loop” loops. It gets called over and over.
There are some things you want done only once when the program initialises. Set the pin modes and configure the serial port for example. Those things naturally belong in setup.