Here is a circuit that shows LEDs and switches being used on the same pins.
/*
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
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 = 41; //Heart beat LED, toggles on and off showing if there is blocking code
const byte YellowLED = 40; //This LED will be toggled when the switch on this pin is pressed
const byte BlueLED = 37; //This LED will be toggled when the switch on this pin is pressed
const byte GreenLED = 36; //This LED will be toggled when the switch on this pin is pressed
const byte RedLED = 35; //This LED will be toggled when the switch on this pin is pressed
const byte WhiteLED = 34; //This LED will be toggled when the switch on this pin is pressed
byte lastYellowSwitchState; //The state this switch was in
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 heartBeatLED = //create timer for the heartBeatLED
{
0, 100UL, true, true, true //lastMillis, waitMillis, restart, enableFlag, true=millis/false=micros
};
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()
{
//***************************
//HeartBeat LED
if (heartBeatLED.CheckTime())
{
//Toggle HeartBeatLED
digitalWrite(HeartBeatLED, !digitalRead(HeartBeatLED));
}
//***************************
// Non blocking code goes here
//***************************
//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 five switches
byte CurrentSwitchState;
//***************************
//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));
}
}
//***************************
CurrentSwitchState = ReadSwitchLED(BlueLED);
if (lastBlueSwitchState != CurrentSwitchState)
{
lastBlueSwitchState = CurrentSwitchState;
if (CurrentSwitchState == LOW)
{
digitalWrite(BlueLED, !digitalRead(BlueLED));
}
}
//***************************
CurrentSwitchState = ReadSwitchLED(GreenLED);
if (lastGreenSwitchState != CurrentSwitchState)
{
lastGreenSwitchState = CurrentSwitchState;
if (CurrentSwitchState == LOW)
{
digitalWrite(GreenLED, !digitalRead(GreenLED));
}
}
//***************************
CurrentSwitchState = ReadSwitchLED(RedLED);
if (lastRedSwitchState != CurrentSwitchState)
{
lastRedSwitchState = CurrentSwitchState;
if (CurrentSwitchState == LOW)
{
digitalWrite(RedLED, !digitalRead(RedLED));
}
}
//***************************
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 ( )
//**********************************************************************
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
//======================================================================