[ Basics ] Arduino - Making a Desk Lamp
주피터 · 2019-10-25 · 9
Making a Stand Lamp
[Image: HF89G04V32BLEXT24H03.png]
Turn the light on and adjust brightness as needed—making it slightly brighter or very bright, depending on requirements
[Image: VU7P52P05H84SUS1PDIY.png]
There are many types of switches. Slide switches, commonly used in MP3 players, multi-way switches frequently used in joysticks or digital camera control buttons, micro switches—switches come in diverse varieties and types.
The switch we use here is called a push button, which is a very simple device with +, −, and 2 poles.
The upper part of the switch is covered with plastic, and when you open this plastic, you can see two metal pieces separated by a spring.
When you press the button, the two metal pieces connect with each other and electricity flows. When you release your finger, the two metal pieces separate again and electricity stops flowing.
So when you want to turn on the light, you press the switch to connect the two metal pieces, which closes the circuit and turns on the stand lamp.
This switch has four legs total—two on each side. When connecting to a circuit, you only need to connect two legs. You insert it into the breadboard with the side where the legs are lined up horizontally facing forward.
One leg connects to the positive pole and the other to the negative pole. Then you can create a circuit that turns on when the switch is pressed and turns off when released.
[Image: 9DJV12LH3CM62NBMEZMS.png]
You connect it to one of Arduino's digital terminals, but there is one problem. When the switch is pressed, the circuit is connected so it reads exactly 1 and outputs 5V as voltage. What value does it read when not pressed? In such cases, when the value is not determined, it is called a floating state, and we solve this problem using a pull-down resistor.
[Image: EE42NNKJ6IUGD6DCDGXV.png]
When the switch is pressed, the circuit is completely connected and we can say 5V is output precisely. However, when the switch is not pressed and is open, it is unclear what voltage flows.
So to obtain a voltage as close to 0V as possible when the circuit is not connected, we attach a relatively large resistor of about 10kΩ to the ground terminal of the switch circuit.
The resistor used in this case is called a pull-down resistor. The voltage is lowered by the resistor so that 0V flows. Thus when the switch is pressed, 5V flows, and when it is not pressed, 0V flows.
[Image: DEG8OZBX1U4SZRFWS62M.png]
To know the switch signal in Arduino, you can use the digitalRead() function. The digitalRead() function takes a terminal number as an argument and returns the voltage at the connected terminal as 0 or 1 values.
For example, if the switch is pressed and 3V or more comes in, it returns HIGH (1), and if 0V comes in, it returns LOW (0).
Here, HIGH and LOW are called constants—values pre-defined as 1 and 0 in Arduino. Although they are text strings, they are always used as the numbers 1 and 0.
[Image: BKXTX8L2M57HV4C6YLSG.png]
To adjust brightness in steps, you need to remember how many times the switch has been pressed.
We use something called a variable.
Arduino has a memory space where you can store data and retrieve it for use. You decide whether the type of data you want to store is a number or a string, and you give that space a name—that is called a variable. Then you can store or retrieve data there anytime.
For example, let's look at the sentence int state = 0;. state is the variable name.
Think of a space called state being created in memory. int means the value stored in the variable state is of integer type, and the initial value 0 will be stored in the variable state. A semicolon must always be attached at the end of the statement.
[Image: YFUPGZCYOTMUY64WIUJR.png]
To adjust the stand brightness in steps, you first need to define the steps. You must pre-define how the state changes depending on the number of times the switch is pressed.
As you can see in the above, we divide the total 4 steps and define the stages according to how many times the switch is pressed.
Step 1 is when the switch is pressed once, and 1 LED turns on.
Step 2 is when the switch is pressed twice, and 2 LEDs turn on.
Step 3 is when the switch is pressed three times, and 3 LEDs turn on.
Finally, Step 4 is when the switch is pressed four times, a state where all LEDs turn off.
[Image: 6XU27F1RJEFVOPJIWR90.png]
For Arduino to operate differently by step, you need a conditional statement.
A conditional statement is a command that makes the computer make a decision, and the if statement is a typical example.
After the reserved word if, you must write a 'question' inside the parentheses.
If the answer is correct or the result of the question is true, the code block immediately below is executed.
In conditional statements, use '==' instead of '='. '=' means assignment, and '==' means equality.
Looking at the conditional statement example below, in the case of if (pin == 1), if the value of the variable pin in the parentheses equals 1, code block A below is executed. And in the case of else if (pin == 2), it does not meet the above if condition. If pin == 2, code block B is executed. Finally, if neither of these two conditions applies, block C is executed.
[Image: 8GASWQU6WHOONPS2D0HC.png]
Circuit with switch connected to Arduino and circuit with 3 LEDs connected
This circuit is a combination of two circuits—a switch input circuit and an LED output circuit. Looking at the switch input circuit, one leg of the switch connects to Arduino's 5V terminal, and the other leg connects to a 10K ohm resistor, which connects to Arduino's pin 8 input terminal, creating the switch input circuit. The LED output circuit has 3 diodes connected to pins 3, 5, and 7, connected together with resistors.
Once the circuit is fully configured, you need to write the sketch. First, read the switch input value and if the switch is pressed, set the step value. Then write the sketch to turn the LEDs on or off according to the step.
[Image: TPEQ6E8SP3PTEXIO6NIK.png]
Place the components on the breadboard according to the circuit configuration and connect them using jumper wires.
After all connections are complete, verify the component orientation and terminal numbers once again.
[Image: MSST954UCLUMC51VFEWO.png]
In the setup() function initially, set three output terminals to output mode and one input terminal to input mode. Also, check how many times the switch has been pressed to save the step.
And we wait for about 250 microseconds, which is very brief—this is to properly detect the moment the switch is pressed.
If step 1, turn on LED 1. If step 2, turn on LED 2. If step 3, turn on LED 3. If step 4, turn off all LEDs. This process repeats infinitely.
[Image: 5X7ZD1RVE51K24EQZGI9.png]
First, we pre-defined variables and constants. We declared a variable called State to store how many times the switch has been pressed, and defined three terminals for LEDs as constants named DIODE1, DIODE2, DIODE3. We also designated the terminal name for receiving switch input as SWITCHINPUT, and declared 4 constants—STEP1, STEP2, STEP3, STEP4—to specify the switch press count in 4 steps.
[Image: X6QHOO6OSLZR93SN0XGH.png]
In the setup() function, we set the initial environment before operation occurs. Below, we designated the terminals for LEDs—DIODE1, DIODE2, DIODE3—as OUTPUT mode. We also designated SWITCHINPUT, declared for switch input, as input mode.
[Image: 851TR3VNP9PD1ZNLK503.png]
In the loop() function, it indicates that actual operation repeats infinitely. First, we call the digitalRead() function to check if the switch is pressed, and we divide the number of switch presses by 4 and use the remainder value—this is to manage only 4 steps. When the switch is pressed 1 time, we turn on DIODE 1, and when pressed 2 times, we turn on DIODE 2.
[Image: CA8N8WQ5VQBEFJRVWN2C.png]
When the switch is pressed the 3rd time, DIODE 3 turns on, and when pressed the 4th time, all diodes turn off.
[Image: HCO8O118U8KMYFTOPQIW.png]
Light switches found everywhere in homes, remote control buttons that control power or change channels, TVs, elevators, fans, computers—almost all electronic devices where we can select some operation have switches built in.
```
int state = 0;
const int DIODE1 = 3;
const int DIODE2 = 5;
const int DIODE3 = 7;
const int SWITCHINPUT = 8;
const int STEP1 = 1;
const int STEP2 = 2;
const int STEP3 = 3;
const int STEP4 = 0;
void setup()
{
pinMode( DIODE1, OUTPUT );
pinMode( DIODE2, OUTPUT );
pinMode( DIODE3, OUTPUT );
pinMode( SWITCHINPUT, INPUT );
}
void loop()
{
if( digitalRead(SWITCHINPUT) == HIGH )
{
state = (state + 1)%4;
delay(250);
}
if ( state == STEP1){
digitalWrite( DIODE1, HIGH );
}else if ( state == STEP2 ){
digitalWrite( DIODE2, HIGH );
}else if ( state == STEP3 ){
digitalWrite( DIODE3, HIGH );
}else if ( state == STEP4 ){
digitalWrite( DIODE1, LOW );
digitalWrite( DIODE2, LOW );
digitalWrite( DIODE3, LOW );
}
}
```
9
댓글 1개
- 주피터댓글