Share tips you have come across

Got a request for the code that goes with post #334, (Using a LED and switch on the same I/O pin).

Forgot to add it to that post :frowning: so here it is now:

/*
  Demonstration how to use a pin as an output to a LED
  and at the same time, as an input from a switch.

  Note:
  The LED(s) 'must' be connected to +5V (CA) through a series resistor.
  The switch is connected to GND through a 240 ohm resistor.

  Typical setup for SMD LEDs:
  +5V White  LED - 4.7K - I/O PIN - 240R - Switch - GND
  +5V Red    LED - 2.2K - I/O PIN - 240R - Switch - GND
  +5V Green  LED - 1.2K - I/O PIN - 240R - Switch - GND
  +5V Blue   LED - 4.7K - I/O PIN - 240R - Switch - GND
  +5V Yellow LED - 2.2K - I/O PIN - 240R - Switch - GND

  LarryD
  Version YY/MM/DD
  1.00    17/06/01   Running code
*/

//Assume 6 LED/Switch combinations are connected to pins as below
const byte HeartBeatLED  = 13; //This LED will be toggled when the switch on this pin is pressed
const byte YellowLED     = 12; //This LED will be toggled when the switch on this pin is pressed
const byte BlueLED       = 11; //This LED will be toggled when the switch on this pin is pressed
const byte GreenLED      = 10; //This LED will be toggled when the switch on this pin is pressed
const byte RedLED        = 9;  //This LED will be toggled when the switch on this pin is pressed
const byte WhiteLED      = 8;  //This LED will be toggled when the switch on this pin is pressed

byte lastHeartBeatSwitchState; //The state this switch was in
byte lastYellowSwitchState;
byte lastBlueSwitchState;
byte lastGreenSwitchState;
byte lastRedSwitchState;
byte lastWhiteSwitchState;

//======================================================================
//An example of Timer 'structure' coding
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 and initialize timer objects
//**********************************************************************

timer checkSwitches =         //create a timer to check the switches
{
  0, 50UL, true, true, true   //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};



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

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

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

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

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

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

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


//                             l o o p ( )
//**********************************************************************
void loop()
{

  //*************************************
  //Is it 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()
{
  //Read our six switches

  byte CurrentSwitchState;

  //***************************
  //Has the switch changed position for this pin?
  CurrentSwitchState = ReadSwitchLED(HeartBeatLED);
  if (lastHeartBeatSwitchState != CurrentSwitchState)
  {
    lastHeartBeatSwitchState = CurrentSwitchState;
    if (CurrentSwitchState == LOW)
    {
      digitalWrite(HeartBeatLED, !digitalRead(HeartBeatLED));
    }
  }

  //***************************
  //Has the switch changed position for this pin?
  CurrentSwitchState = ReadSwitchLED(YellowLED);
  if (lastYellowSwitchState != CurrentSwitchState)
  {
    //update to the new switch state
    lastYellowSwitchState = CurrentSwitchState;

    //when the switch goes from not pushed (HIGH) to pushed (LOW) then do something
    if (CurrentSwitchState == LOW)
    {
      //example: toggle the LED on this pin
      digitalWrite(YellowLED, !digitalRead(YellowLED));
    }
  }

  //***************************
  //Has the switch changed position for this pin?
  CurrentSwitchState = ReadSwitchLED(BlueLED);
  if (lastBlueSwitchState != CurrentSwitchState)
  {
    lastBlueSwitchState = CurrentSwitchState;
    if (CurrentSwitchState == LOW)
    {
      digitalWrite(BlueLED, !digitalRead(BlueLED));
    }
  }

  //***************************
  //Has the switch changed position for this pin?
  CurrentSwitchState = ReadSwitchLED(GreenLED);
  if (lastGreenSwitchState != CurrentSwitchState)
  {
    lastGreenSwitchState = CurrentSwitchState;
    if (CurrentSwitchState == LOW)
    {
      digitalWrite(GreenLED, !digitalRead(GreenLED));
    }
  }

  //***************************
  //Has the switch changed position for this pin?
  CurrentSwitchState = ReadSwitchLED(RedLED);
  if (lastRedSwitchState != CurrentSwitchState)
  {
    lastRedSwitchState = CurrentSwitchState;
    if (CurrentSwitchState == LOW)
    {
      digitalWrite(RedLED, !digitalRead(RedLED));
    }
  }

  //***************************
  //Has the switch changed position for this pin?
  CurrentSwitchState = ReadSwitchLED(WhiteLED);
  if (lastWhiteSwitchState != CurrentSwitchState)
  {
    lastWhiteSwitchState = CurrentSwitchState;
    if (CurrentSwitchState == LOW)
    {
      digitalWrite(WhiteLED, !digitalRead(WhiteLED));
    }
  }

} //      E N D   O F   h a n d l e S w i t c h P r e s s e s ( )


//**********************************************************************
//                    R e a d S w i t c h L E D ( )
//**********************************************************************
//Save the current state of the LED on this pin.
//Read the state of the switch on this pin.
//Return the LED state back to this pin.
//Takes about 14us for this process.

byte ReadSwitchLED(byte thisPin)
{
  //Save the current state of this output pin
  byte PinState = digitalRead(thisPin);

  //read the switch connected to this pin
  pinMode(thisPin, INPUT_PULLUP);
  byte SwitchState = digitalRead(thisPin);

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

  return SwitchState;

} //            E N D   O F  R e a d S w i t c h L E D ( )


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