# Switch and LED on the same pin, suggestions FYI

**URL:** <https://forum.arduino.cc/t/switch-and-led-on-the-same-pin-suggestions-fyi/423048>\
**Category:** Programming\
**Created:** [December 5, 2016, 6:27am UTC](https://forum.arduino.cc/t/switch-and-led-on-the-same-pin-suggestions-fyi/423048 "2016-12-05T06:27:27Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [December 5, 2016, 5:43pm UTC](https://forum.arduino.cc/t/switch-and-led-on-the-same-pin-suggestions-fyi/423048/6 "2016-12-05T17:43:57Z")

</div>

Thank you for the comments.

@stuart0 suggestions put into practice gives the following results.  
With Red LED Vf=1.7v, R2=220 and VCC=5V. See schematic.

Note: very dim light comes from the LED during switch reading.

 ![2016-12-05_10-37-56.png](https://europe1.discourse-cdn.com/arduino/original/4X/c/1/d/c1d289610c1cd91b06d21c93d70068f89a71ba82.png)

Edit:  
Logic analyzer view  
 ![2016-12-05_11-58-42.png](https://europe1.discourse-cdn.com/arduino/original/4X/d/c/d/dcd217fd589ba9949fb548be983fcf9a53f0b1e4.png)

Minor changes:

```cpp
/*
  Version YY/MM/DD
  1.00    16/01/01  Running code

*/

// **********************************************************************
//G l o b a l  v a r i a b l e s
// **********************************************************************
//inputs/outputs
const byte heartLED  = 13;
const byte secondLED = 12;
const byte SwitchLED = 2; //switch (and LED) is on Arduino pin D02, pushed = LOW

//SRAM variables
byte state = 1;
byte lastSwitchState = 1;

//EEPROM locations

//======================================================================
struct timer
{
  //lastMillis = the time this "timer" was (re)started
  //waitMillis = delay time (mS) we are looking for
  //restart    = do we start "this timer" again and again
  //enableFlag = is "this timer" enabled/allowed to be accessed
  //timeType  = true = millis(), false = micros()
  // **********************
  //For each timer object you need:
  //Example:
  //  timer myTimer = //give the timer a name "myTimer"
  //  {
  //    0, 200UL, true, true, true  //lastMillis, waitMillis, restart, enableFlag, timeType
  //  };
  // You have access to:
  // myTimer.lastMillis, myTimer.waitMillis, myTimer.restart, myTimer.enableFlag, myTimer.timeType, myTimer.CheckTime()
  // **********************

  unsigned long lastMillis;
  unsigned long waitMillis;
  bool          restart;
  bool          enableFlag;
  bool          timeType;

  unsigned long currentTime;

  bool CheckTime() //Delay time expired function "CheckTime()"
  {
    if (timeType == true)
    {
      currentTime = millis();
    }
    else
    {
      currentTime = micros();
    }

    //is the time up for this task?
    if (enableFlag == true && currentTime - lastMillis >= waitMillis)
      {
      //should this start again?
      if (restart)
      {
        //get ready for the next iteration
        lastMillis = currentTime;
      }
      //time was reached
      return true;
    }
    //time was not reached
    return false;

  } //END of CheckTime()

}; //END of structure timer
//======================================================================

// **********************************************************************
//                Create timer objects and initialize
// **********************************************************************
timer heartBeatLED =          //create timer pin13
{
  0, 250UL, true, true, true	//lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};

timer pin2LED =
{
  0, 100UL, true, true, true  //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};

timer checkSwitches =
{
  0, 50UL, true, true, true  //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};

//                        s e t u p ( )
// **********************************************************************
void setup()
{
  // **************************************
  digitalWrite(heartLED, HIGH);  //LED  LOW = ON
  pinMode(heartLED, OUTPUT);

  digitalWrite(secondLED, HIGH); //LED  LOW = ON
  pinMode(secondLED, OUTPUT);

  pinMode(SwitchLED, OUTPUT);    //normal state of this pin
  digitalWrite(SwitchLED, HIGH); //LED  LOW = ON

} //  >>>>>>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<<<<<

//                            l o o p ( )
// **********************************************************************
void loop()
{
  // ***************************
  //HeartBeat LED
  if (heartBeatLED.CheckTime())
  {
    //Toggle heartLED
    digitalWrite(heartLED, !digitalRead(heartLED));
  }

  // ***************************
  if (pin2LED.CheckTime())
  {
    //Toggle the LED on the SwitchLED pin
    digitalWrite(SwitchLED, !state);
    state = !state;
  }

  // ***************************

  //None blocking code goes here

  // ***************************
  //time to check the Switch(s)?
  if (checkSwitches.CheckTime())
  {
    handleSwitchPresses();
  }

} //  >>>>>>>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<<<<<<

//======================================================================
//                        F U N C T I O N S
//======================================================================

//                h a n d l e S w i t c h P r e s s e s( )
// **********************************************************************

void handleSwitchPresses()
{
  //store the condition of the output pin
  byte PinState = digitalRead(SwitchLED);
  
  //read the switch connected to this pin
  pinMode(SwitchLED, INPUT_PULLUP);
  byte switchPosition = digitalRead(SwitchLED);

  //return the pin to OUTPUT
  pinMode(SwitchLED, OUTPUT);
  //restore the pin state
  digitalWrite(SwitchLED, PinState);

  //has the SwitchLED changed state?
  if ( switchPosition != lastSwitchState)
  {
    lastSwitchState = !lastSwitchState;

    if (lastSwitchState == LOW) //do LOW stuff
    {
      //toggle secondLED
      digitalWrite(secondLED, !digitalRead(secondLED));
    }

    else                      //do HIGH stuff
    {
    }

  } //END of SwitchLED changed state

  //Other Switches

} //      E n d  o f  h a n d l e S w i t c h P r e s s e s ( )

// **********************************************************************

//======================================================================
//                        E N D  O F  C O D E
//======================================================================

```

.

---

_[View the full topic](https://forum.arduino.cc/t/switch-and-led-on-the-same-pin-suggestions-fyi/423048)._
