[Basics] Arduino Practice - Creating a Traffic Light

주피터 · 2019-10-25 · 8
Making a Traffic Light If you look closely at a traffic light, you can see that the light is made up of many dots, and each of these dots emits light like a light bulb. These are composed of light-emitting diodes (LEDs). How do you make the red and blue lights turn on and off alternately? ⓞ Circuit diagram with a power source, resistor, two light-emitting diodes, and switches connected to each ⓞ Arduino execution ⓞ Arduino configuration The most representative handheld computer is Arduino. When you look at Arduino, there are 6 pins (A0 to A5) that can receive analog signals, and 14 pins (0 to 13) that can be used for digital input or output. A maximum of 40mA of current can flow from the digital output pins. If you design the circuit to allow more current to flow, the components inside Arduino may not be able to withstand it and could fail, so caution must be taken. Arduino has a USB connector to connect with a personal computer, so it can receive sketches (programs) or communicate from a personal computer, and at the same time it can receive 5V of power supply. When not connected to a computer, power can also be supplied through a separate external power connector. Once the sketch is transmitted from the computer, the handheld computer executes the sketch on its own, independent of the personal computer. Therefore, if external power is supplied, it operates on its own without connecting the USB cable. ① Sketch structure A sketch is a type of computer language that we commonly call a program on computers. It is a set of commands that give instructions to Arduino, written in a manner similar to English sentences. It is very similar to languages such as C, C++, and JAVA used in personal computer programming, but much simpler and easier. A sketch requires two important functions: one is the setup() function, and the other is the loop() function. The setup() function is executed only once when the sketch first runs, and the loop() function is executed repeatedly in an infinite loop thereafter. You can include explanatory text in a sketch that is not actually executed but helps the reader understand. Use // for single-line comments and /* */ for multi-line comments. Use curly braces { } to group commands into a block, and attach a semicolon at the end of each command. You can use various variables in a sketch to store numbers. The most commonly used variable is an integer variable, written as int, followed by the variable name. An integer variable occupies 2 bytes, or 16 bits internally, and can hold integers from -32,768 to 32,767. To use the digital input/output terminals of the Arduino board, you must first set the terminal mode, which can be easily done using the pinMode() function. Inside the parentheses of the pinMode() function, you specify the terminal number you want to configure and the INPUT or OUTPUT constant that determines input/output. Since the terminal mode setting only needs to be done once before actually using the terminal, it should be used within the setup() function. After setting the digital input/output terminal to output mode, you can perform actual output using the digitalWrite() function. Inside the parentheses of the digitalWrite() function, write the terminal number and the HIGH or LOW constant. When HIGH, 5V is output; when LOW, 0V is output. To make Arduino do nothing for a certain period of time, you can use the delay() function. When you write a number inside the parentheses of the delay() function, Arduino will pause for the number of milliseconds corresponding to the specified number. Connect a resistor to pin 13 on Arduino, and connect the anode of the red light-emitting diode to it. Also connect a resistor to pin 12, and connect the anode of the blue light-emitting diode to it. Connect the cathodes of both light-emitting diodes to Arduino's ground. Since the current flowing through the two light-emitting diodes must be 20mA or less, it is good to use resistors with values of 250 ohms or more. Then, write a sketch to set pins 13 and 12 to output mode, and output 5V and 0V alternately every 5 seconds. Place the components on the breadboard according to the circuit configuration and connect them using jumper wires. After connecting everything, check the component orientation and terminal numbers once more. Integer variables ledRed and ledGreen were declared and assigned 13 and 12, respectively. By using variables this way, you don't have to constantly remember the numbers, and later when you need to change the terminal numbers, you only need to change them where you first declared the variables, which is convenient. In the setup() function, the pinMode function is called twice to set both ledRed and ledGreen terminals to output mode. Next, in the loop() function, the digitalWrite() function is called twice to turn on ledRed and turn off ledGreen. Then the delay(5000) function is called to wait for 5 seconds. Next, the digitalWrite() function is called twice again to turn off ledRed and turn on ledGreen. The delay(5000) function is called again to wait for 5 seconds. When you click the first checkmark icon button in the Arduino development environment tools, the written sketch is converted to machine language. If there are errors, an error message appears in the window at the bottom, and you should refer to this message to find and correct the wrong parts. If errors persist, this process must be repeated until there are no more errors. This process is called debugging, which means catching bugs. Once there are no more errors, click the upload button next to it to upload the converted machine language code to the Arduino board. ``` int ledRed = 13; int ledGreen = 12; void setup() { pinMode (ledRed,OUTPUT); pinMode (ledGreen,OUTPUT); } void loop() { digitalWrite(ledRed,HIGH); digitalWrite(ledGreen,LOW); delay(5000); digitalWrite(ledRed,LOW); digitalWrite(ledGreen,HIGH); delay(5000); } ```
8
댓글 0
등록된 댓글이 없습니다.