Functii Basic Arduino
Functii Basic Arduino
Let me start by saying that pinMode() is a wonderful function. If you recall, functions can take
arguments. The pinMode() function takes two arguments - it wants to know which pin you are
going to assign a mode to, and what mode you want that pin to be. The pin number is easy, 0-13
for any of the digital pins, and A0 through A5 for any of the analog pins.
The mode is an easy designation also - you are going to want the pin to be an INPUT - good for reading a
sensor. Or an OUTPUT - good for powering an LED.
The function digitalWrite() is used to apply either HIGH or LOW voltage to a pin. HIGH will apply
5 volts to the pin you designate and LOW will apply 0 volts. If you apply 5 volts to a pin that is
connected through an LED to ground, then your LED will light up. There is a voltage difference
between the pin and ground, thus a current is able to flow through the LED. If you apply 0 volts to
the same pin the LED will not light up, because no current is being "pushed" through the circuit the voltage at ground and at the pin are both zero.
digitalWrite() takes two arguments, the first is what pin you will be applying voltage to and the other is
what level of voltage, either HIGH or LOW as we have discussed above.
The delay() function takes one argument - the number of milliseconds you want the program to
delay. In this case we want 1000 milliseconds, which makes one second.
Serial.begin() is part of a family of functions referred to as a library. The name of the library is
the Serial library. The Serial library helps you establish communication between your computer
and the Arduino board. If you ever go to marriage counseling, you will know that communication
involves sending and receiving. Data can flow both ways. If we want to establish this
communication, we use the begin() function from the Serial library.
Serial.begin(9600);
The begin() function takes one argument - the baud rate. What is the baud rate you ask? It is the rate at
which data will flow between your computer and your Arduino. And I will give you a short answer - for
most Arduino sketches you will run, a baud rate of 9600 is what you will provide as an argument.
The digitalRead() function returns an integer - either a 1 or a 0. If it is a 1, than the voltage at the
pin is high, if the value is 0, the voltage at the pin is low. What pin you ask? Well the pin you
pass as an argument in the digitalRead() function
function analogRead() does? It reads the value at the analog pin that you tell it to - in this case it
is the analog pin A0, where we have the center pin of our potentiometer connected to. The
voltage at pin A0 will be mapped to a number between 0 and 1023, and this value will be
assigned to the variable sensorValue.
If you recall from above, the actual voltage at pin A0 will be between 0 and 5 volts, depending on where
your potentiometer is adjusted - this value gets mapped to the range 0 - 1023 with the help of the analogto-digital converter. So we have a variable that has recorded the value at our potentiometer - what next?
Well let's look at the value! To do that, we need to print it from the Arduino to our computer - and you
guessed it, we will use the Serial library function println() to do just that...
analogWrite(). I experienced much confusion with analogWrite(), because I suspected that it had
to do with the analog pins on the Arduino. The function, however, has nothing to do with the
analog pins.
There are 5-6 pins on most Arduino boards marked with an '~' next to the pin number - these pins can be
invoked to rapidly change the power being applied at the pin - this is a technique called pulse width
modulation (PWM).
exemplu de cod:
int Pin = 6;
int button = 2;
int x=0;
void setup()
{
pinMode(Pin, OUTPUT);
// declares LED as an output
pinMode(button, INPUT);
// declares push button as an input
Serial.begin(9600);
}
void loop()
{
while (digitalRead(button) == HIGH && x<=255) // checks if the button is on and
// if the variable 'x' is less
than 255
{
analogWrite(Pin,x);
// the LED will fade as long as the
delay(20);
// statements in the 'while' are true
x=x+5;
}
x=0;
analogWrite(Pin, 0);
}
void setup(){
Serial.begin(9600);
}
void loop(){
Serial.print("Time: ");
time = millis();
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(1000);
}
Tip: Note that the parameter for millis is an unsigned long, errors may be generated
if a programmer tries to do math with other datatypes such as ints.