Good day, Habr. As planned, I continue the cycle of articles that will help you in acquaintance with Arduino. Also, in each subsequent article (including this one) you can find answers to the most important questions that appear in the comments. For those who have not read the first part, here .
At the moment, most LEDs do so in order to simplify the process of determining the polarity. The LED has two legs, one of which will be longer than the other. The long leg is a plus (anode). A short leg is a minus (cathode). But what to do if the findings of the LED of the same size? Sometimes the cathode output is marked with a dot or a small cut on the body. You can also find out the polarity by careful consideration of the crystal. Plus has a much smaller size inside the lens compared to minus. The minus contact, in turn, resembles the flag on which the crystal is placed. You can also determine the polarity of the power supply. To do this, you need a current source (with a voltage of 3 to 6 volts), a resistor (with a resistance of 220 - 470 Ohms) and the LED itself. First connect one leg of the LED to the resistor. Then touch the power supply contacts with the LED. Touching the anode to the plus, and the cathode to the minus, the LED will glow (if it is working).
In the last article we learned how to connect and turn on the LED and also flicker them. And what if we need to control this LED. Or we want to make a semblance of a lamp. Here we will help the button or switch. But today we will look at the button. They can actually be useful in different projects. Let's get started
To connect the button you need:
After connecting the circuit, let's deal with the program itself. After connecting the circuit, let's deal with the program itself. In this example, we will control the LED on the 13 pin. That is, you can not connect an additional LED so there is a built-in. But the connection of the LED can be read in 1 part Arduino for beginners.
int Led = 13; int Button = 2; // , void setup(){ pinMode(Led, OUTPUT); pinMode(Button, INPUT); } void loop(){ if(digitalRead(Button) == HIGH){ // digitalWrite(Led, HIGH); // } else{ // () digitalWrite(Led, LOW); // } }
This example is quite good. But, and if we need to perform an action not by holding, but by pressing. Then we will change our code a bit. We will add two more variables, some flags that will follow some processes. And the code looks like this:
bool But = true; // bool Light = false; // int Led = 13; int Button = 2; // , void setup(){ pinMode(Led, OUTPUT); pinMode(Button, INPUT); } void loop(){ bool Now = digitalRead(Button); // if(But && !Now) { delay(10); // Now = digitalRead(Button); if(!Now) { Light = !Light; digitalWrite(Led, Light); } } But = Now; }
In this code, you might notice Light = !Light
. I'll explain now. We read whether the button has been omitted. Next, a small delay determines whether it was an error and if the button was actually pressed, we invert the LED signal. That is, if the LED is on, it will stop burning. And vice versa.
Very good, when everything is perfect. But unfortunately everything is so perfect. And with the buttons the same problem. The mechanical contacts of the button cannot close / open instantly. This is called “contact bounce”. How to understand this. The input of the microcontroller pulses. But instead of just getting 1 or 0 because of this phenomenon, a whole bunch of impulses come to the input. This can create false signals. And that is why we make a small delay in our program. At the speed of this is not particularly affect, but will provide us from the error.
On this we have the end of the second part. Thanks for attention.
Source: https://habr.com/ru/post/358186/