Reading a switch with 2 ports

Hi all

Im a little uncertain, so I have a simple question. Is it possible to have a OUTPUT port acting as +5v for another INPUT port?

Its because, it would be easy for me NOT to solder a wire to the real vcc (+5v) Therefore i will use one port as +5v by setting it high. And another port reading wether its high or nc (low).

void ReadSwitches()
{
   const int  MotorOn = 43;
   const int  VCC     = 42;
   bool SwitchMotorOn = 0;




 pinMode(MotorOn, INPUT); // 3 way switch 
 pinMode(VCC, OUTPUT);     // Have to be high
 digitalWrite(VCC, HIGH);  // +5v for the switch



  //Read the switch
   SwitchMotorOn =  digitalRead(MotorOn);

   if ( SwitchMotorOn)> 0)  //the switch is on
     {
       Serial.println("--------Motor is running ------------- ");
     }
   else
     {
       Serial.println("--------Motor is OFF ------------- ");
     }


}

Or is there a better way to do this, with 2 ports.

Thanks in advance Tim

Or is this better ?

  const int MotorOn = 43;
  const int GND     = 42;
  bool SwitchMotorOn = 0;

  pinModeMotorOn , INPUT_PULLUP);
  pinMode(GND, OUTPUT);
  digitalWrite(GND, LOW);  // GND for the switch

  
  //Read the switch
  SwitchMotorOn = digitalRead(MotorOn);


  if (SwitchMotorOn == LOW) //the switch is on
     {
       Serial.println("--------Motor is running ------------- ");
     }
   else
     {
       Serial.println("--------Motor is OFF ------------- ");
     }

Thanks in advance Tim

Can you post a diagram of what you are trying to do?

One simple and common way to operate a switch is to set the pin with pinMode(INPUT_PULLUP) and wire the switch so it connects the pin to GND when the switch is pressed.

A pin set as INPUT can detect the HIGH or LOW voltage on another pin set as OUTPUT. Just be careful NOT to have both pins set as OUTPUT because then if one is HIGH and one is LOW there will be a short circuit and the smoke will escape.

…R

Oki thanks, Robin.

Will set the PIN 42 as GND instead of VCC. (so it's LOW and not HIGH)

Br Tim

Tvixen: Will set the PIN 42 as GND instead of VCC. (so it's LOW and not HIGH)

The words and the grammar are good but without the context this does not convey any meaning.

And I suspect you will eventually just confuse yourself if you give the name GND to an I/O pin.

...R