# Make a Button Switch | MindPlus Coding Kit for Arduino Started Tutorial E05

In the first project, we utilized a button to make LED on when the button is pressed and off when button released. However, in reality, the first button press switches on a light and the subsequent press turns it off -- this is how light switches work in our daily life. If you want to keep LED on without keeping the button pressed, then you should check out this chapter, which turns the button into a real light switch.

1. How to eliminate button jittery?

2. Make a button switch

### Key Points Analysis

What is button jittery?

We would imagine that the switch circuit works like “when button pressed down, the circuit closes” and “button pressed again, the circuit breaks”. However, that is actually not the case.

Buttons are usually made of mechanical elastic switches. When mechanical contacts are broken or closed (usually lasts for around 10ms), a series of vibrations will occur due to elasticity, which causes button switches to not immediately close the circuit upon closing, and also not immediately breaks the circuit upon opening.

Button jittery

So how to eliminate button jittery?

There are two common solutions: by software and by hardware. Here we’ll focus on the simpler one, software solution.

We already know that button jittery cased by elastic inertia lasts for around 10ms. So we can use a delay command to postpone the execution, as well as a conditional, in order to eliminate button jittery.

### Hands-On

Hardware connection

Refer to “Magic Button Switches” for circuit connection.

Programming

1. Write the program

What to do when we want two different results when “press” the button all the same?

In order to continuously switch (no pun intended) between “turning on/ turning off”, we need to apply what we learnt in last chapter, variable, which will be used as an intermediate.

1) First set a variable (if you have questions about how to create a new variable, go back to Project II in Chapter I ) and initialize the variable to 0. Again, this is a looped event.

2) Next, set up the True / False conditional when button is pressed.

3) Now comes the hardest part. We need to find a way to utilize the variable to realize “turning on on first press and turning off on the subsequent press”.

In the conditional, initial value of i is 0, and True / False correspond to 1 and 0. So we set two situations when i is 1 and 0:

4) Then control the output. Output low level for 0, high level for 1, plus the delay command.

2. Reference program

3. Program effect

When the button is pressed, LED turns on; a subsequent press turns it off.

4. Program analysis

You may be confused that why use the variable i?

Let's think about what happens without the variable i. Then we go back to the case where we control the LED simply using the button. When the button is pressed, LED turns on. However the LED only turns on when button pressed down. It doesn’t turn off on a subsequent press.

Therefore, we must introduce the variable i as an intermediate variable in order to realize the function by checking the True / False values.

Two-way switch means two sets of switch can control a light without affecting each other. You can turn on or off the light with either of them. If fact, every light can be wired on a circuit of multiple switches. Two-way, three-way or even multiple-way switches are all possible. Two-way switch means two switches controlling one light, Three-way switch means three switches controlling one light, and so on. We only need to check the conditions of multiple switches.

Common two-way switches include staircase switch, bedroom bed switches, and etc.

### Further Exercise

Now that you have mastered one-way switch, try making a two-way switch.