The material we need is:
-Arduino Uno or similar.
- A PC with the Arduino environment correctly installed and configured.
-A plate breadboard, an LED.
-A button.
-Two resistances of 330 ohms.
-Some cables protobard.
Often electronics need to know if a light is on or off, if someone pressed a button or if a door has been left open or closed.
This type of signals ON / OFF, YES / NO, TRUE / FALSE, 0/1 are called digital, and we can handle them with pins 0 to 13 and therefore speak Arduino digital pins.
Many of the sensors and actuators we see in the real world are digital.
Arduino digital pins can be used as all or nothing outputs to turn on an LED. In the same way we can read values, all or nothing from the outside world.
In this session we will see that Arduino digital pins can be used for both input and output. Let's read a button or external button and let an LED on or off depending on the button is pressed or not.
We mount a circuit with an LED and connected resistance to digital pin 10 Arduino, as we saw in the previous and also one second circuuito with S1 button connected to pin 6 with a resitencia as shown in the following diagram sessions.
While we do not press S1 Arduino pin 6 it is connected to 5V through resistor R3 forcing a high voltage reading (HIGH). Instead when we press S1 close the circuit Ground pin 6 to thus read low, LOW voltage. In both cases we have a defined voltage value.
If we did not put the resistor R3, pressing S1 would read correctly LOW on pin 6. But when you stop pressing S1 pin 6 would be in a floating state, which is neither HIGH nor LOW but undetermined. As this is unacceptable in digital circuits we force a high reading with R3.
And here is the outline for breadboard circuit:
Let's start doing a program that makes the LED lights up when you press the button and turns off when you let go. To do this we will ask to configure the Arduino digital pin 10 (D10) as an output to drive the LED, and digital pin 6 (D6) as input to read the button.
Usually in simple programs simply put the pin number in the instructions. But as the program is complicated this tends to cause more difficult bugs.
So it is customary to define variables with pin numbers we use, so we can modify them playing in one place (and not having to search through the program). Let's write this a little more elegantly:
int LED = 10;
int button = 6;
void setup ()
{
pinMode (LED, OUTPUT); // LED output
pinMode (button, INPUT); // Button as input
}
We saw enough to turn the use digitalWrite (LED, HIGH) LED. To read a button you can do something similar: digitalRead (button). Let's see how it could be our loop
void loop()
{
int valor = digitalRead(boton) ; // We read the value of Button in value
digitalWrite( LED, valor) ;
}
How could we do otherwise, the LED is turned on and off by pressing else? It would suffice to write LED opposite of what we read on the button.
There is an operator that does that exactly the negation operator "! ". If given value x is HIGH, then! X is LOW and vice versa.
In fact these operations are so frequent that C ++ incorporates a type called bool or Boolean TRUE only accepts two values (true) and FALSE and are fully equivalent to 1/0, and HIGH / LOW
This new program would be something like this:
void loop ()
{
int value = digitalRead (button); // Read the value of Button in value
digitalWrite (LED, value!); // Write value LED
}
We have defined as a bool value, because we can use the value of high voltage and low value TRUE and FALSE.
If the button is not pressed the D6 read TRUE and therefore will LED to FALSE. Otherwise it will turn on the LED.
In fact we could write a curious variant of blinking LED using the negation operator:
void loop ()
{
bool = digitalRead (LED);
digitalWrite (LED, value!);
delay (1000);
}
The first line reads the status LED and invests in the second line, then write this in LED. And while we beat a record, we can write the blinking led in only two lines:
void loop ()
{
digitalWrite (LED, digitalRead (LED)!);
delay (1000);
}
(The instructions within parentheses are executed before those outside of them. So the digitalRead runs before the digitaWrite.)
No hay comentarios:
Publicar un comentario