Simplified Sketch:
/*
Demonstration how to use a pin as a output for a LED
and as an input for a switch.
Note:
The LED 'must' be connected to +5V through a 470 ohm (or larger) resistor.
The switch is connected to GND through a 220 ohm resistor.
Version YY/MM/DD
1.00 16/12/11 Running code
*/
//**********************************************************************
//G l o b a l v a r i a b l e s
//**********************************************************************
const byte heartLED = 13; //Heart beat LED, toggles on and off showing if there is blocking code
const byte Pin12LED = 12; //Push ON push OFF, controlled by a switch on pin 6
const byte SwitchLED = 6; //Switch and LED connected this pin, switch pushed = LOW
//LED is toggled showing the dual action of this pin
//SRAM variables
byte state = 1;
byte lastSwitchState = 1;
unsigned long Pin13Millis;
unsigned long Pin6Millis;
unsigned long SwitchMillis;
// s e t u p ( )
//**********************************************************************
void setup()
{
//**************************************
digitalWrite(heartLED, HIGH); //LOW = ON
pinMode(heartLED, OUTPUT);
digitalWrite(Pin12LED, HIGH); //LOW = ON
pinMode(Pin12LED, OUTPUT);
digitalWrite(SwitchLED, HIGH); //LOW = ON
pinMode(SwitchLED, OUTPUT); //normal state of this pin
} // >>>>>>>>>>>>>>>>>> E N D O F s e t u p ( ) <<<<<<<<<<<<<<<<<<<<<
// l o o p ( )
//**********************************************************************
void loop()
{
//***************************
//HeartBeat LED
if (millis() - Pin13Millis >= 100UL)
{
Pin13Millis = millis();
//Toggle heartLED
digitalWrite(heartLED, !digitalRead(heartLED));
}
//***************************
if (millis() - Pin6Millis >= 500ul)
{
Pin6Millis = millis();
//Toggle the LED on the SwitchLED pin
digitalWrite(SwitchLED, !state);
state = !state;
}
//***************************
//None blocking code goes here
//***************************
//time to check the Switch(s)?
if (millis() - SwitchMillis >= 50ul)
{
SwitchMillis = millis();
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()
{
//**** R e a d i n g t h e s w i t c h s h a r e d w i t h L E D ****
//Reading the switch takes ~14us
//save 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);
//**** R e a d i n g t h e s w i t c h s h a r e d w i t h L E D ****
//has the SwitchLED changed state?
if (switchPosition != lastSwitchState)
{
lastSwitchState = switchPosition;
if (switchPosition == LOW) //do LOW stuff
{
//toggle Pin12LED
digitalWrite(Pin12LED, !digitalRead(Pin12LED));
}
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
//======================================================================
See this post for more information:
https://forum.arduino.cc/index.php?topic=445951.msg3336467#msg3336467