Introduction

After the tutorial Using a Button, you might think, if we want to turn on the LED in this way, the hand cannot leave button, it is not convenient.How to control the lights as normal , click on the light, then press out? We can improve the program, which can realize the result that will modify the program for the following code, and then upload to the Osoyoo Arduino board.

Preparations

HARDWARE

Connection

Although the bodies of the buttons are square, the pins protrude from opposite sides of the button. This means that the pins will only be far enough apart when they are the correct way around on the breadboard.Build the circuit as below:

Notice:

Generally, the button is directly connected in an LED circuit in order to turn on or off the LED. This connection is relatively simple. However, sometimes the LED will light up automatically without pressing the button, which is caused by various interferences. In order to avoid these external interferences, a pull-down resistor is used, that is, to connect a 1K–10KΩ resistor between the button port and GND. It is used to consume external interferences while connected to GND for as long as the button switch is turned off.

CODE PROGRAM

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.Open the Graphical Programming software Mixly and follow the next operations:

In this program, we use the “setup” block. Compared with the program under it, the “setup” block only executes once during the whole process of running the program.

In the “setup” block, we use the module that defines variables. The name of the variable is state, which is a Boolean variable, that is, its value has two kinds of “high” and “low”.

Click Save aftogramming is done. Select the board type and serial port before uploading. For instause a Uno board, just select Arduino/Genuino Uno: if you use a Mega2560, select Arduino/Genuino Mega or Mega2560. 

Select the serial device of the Arduino board from the COM menu. This is likely to be COM3 or higher (COM1 and COM2 are usually reserved for hardware serial ports). To find out, you can disconnect your Arduino board and re-open the menu; the entry that disappears should be the Arduino board. Reconnect the board and select that serial port.

Next,upload the code. If the uploading fails, check and correct the code according to the prompts.

Finally, the staus will change to ‘Upload success!’.

Running Result

After the program is compiled and uploaded, you can achieve: click the button to turn on the LED, and then press this button, the LED will be turned off.