# Playground Switch tutorial not clear

Mellis, I was going to point someone to the [u]switch tutorial[/u] in the playground , but I found that tutorial really confusing.

The first problem is that the wiring diagram is confusing. Its unclear which pins the wires go to on the arduino and it's not very clear what that resistor is connected to.

And it looks from the diagram like the pin would be normally high, pulled low when pressed but the text says the opposite happens.

Another issue may just be with choice of example. I think it would be difficult for the target audience to figure out how to do a non toggling version from the example code. A simple debounce of on and off would probably be used at least as much as a toggle.

Also, the switch tutorial would be a great place to show people how to use a pin's internal pull-up resistor. A button circuit for the AVR can be as simple as simply connecting a pin through a button to ground, but most tutorials I see make a big deal about using an external resistor in the circuit, which tends to complicate things and doubles the number of required components.

• Ben

Also, the switch tutorial would be a great place to show people how to use a pin's internal pull-up resistor. A button circuit for the AVR can be as simple as simply connecting a pin through a button to ground, but most tutorials I see make a big deal about using an external resistor in the circuit, which tends to complicate things and doubles the number of required components.

• Ben

Yes, but dealing with pull-ups is one extra concept that would need to be grasped by someone just starting out. Having the physical resistor in a basic tutorial helps people new to electronics visualize what is happening. I suggest pull-ups are covered after the basic switch with resistor is explained.

How about a 2 or more part tutorial via links. Progressive learning is easy when there are links on where to go next to expand on the current topic.

I'd currently use below code for switch tutorial, step-by-step coding and clear.

``````/*
Toggle LED (13 active-high) with Push Button switch (2 active-low)
*/

int swPin = 2;
int ledPin = 13;
int ledState = 0; // intial state = 0

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH); // enable internal pull-up (Ext. R may removed)
}

void loop()
{
if ((digitalRead(swPin))==LOW) // switch is pressed?
{
if (ledState==0)  // toggle state and led acion
{
digitalWrite(ledPin,HIGH);
ledState=1;
}
else
{
digitalWrite(ledPin,LOW);
ledState=0;
}

while ((digitalRead(swPin))==LOW) // wait for switch depressed
{ delay(10); }
}
}
``````

or another code without waiting

``````/*
Toggle LED (13 active-high) with Push Button switch (2 active-low)
*/

int swPin = 10;
int ledPin = 13;
int ledState = 0; // intial state = 0
int swPressed = 0; // sw hold state =0
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH); // enable internal pull-up (Ext. R may removed)
}

void loop()
{
if ((digitalRead(swPin))==LOW) // switch is pressed?
{
if (swPressed==0)
{
swPressed=1;
delay(10);
if (ledState==0)  // toggle state and led acion
{
digitalWrite(ledPin,HIGH);
ledState=1;
}
else
{
digitalWrite(ledPin,LOW);
ledState=0;
}
}
}
else
{
swPressed=0;
}
}
``````