1. Cấu trúc chương trình
else {}
4. Vòng lặp
// khai báo biến, hằng...
int ledPin = 13; // select the pin for the LED
// hàm khởi tạo
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
// hàm lặp (hàm chính)
void loop() {
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for 1000 milliseconds:
delay(1000);
// turn the ledPin off:
}
2. Hàm Nhập, Xuất
2.1. digitalRead()
Description: Reads the value from a specified digital pin, either HIGH or LOW.
Syntax: digitalRead(pin)
Parameters: pin: the number of the digital pin you want to read (int)
Returns: HIGH or LOW
Example: Sets pin 13 to the same value as pin 7, declared as an input.
int ledPin = 13; // LED connected to digital pin 13
int inPin = 7; // pushbutton connected to digital pin 7
int val = 0; // variable to store the read value
void setup(){
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop(){
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}
* If the pin isn't connected to anything, digitalRead() can return either HIGH or LOW (and this can change randomly); The analog input pins can be used as digital pins, referred to as A0, A1, etc.
2.1. digitalWrite()
Description: Write a HIGH or a LOW value to a digital pin.
- If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
- If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor. See the digital pins tutorial for more information.
NOTE: If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim. Without explicitly setting pinMode(), digitalWrite() will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor.
Syntax: digitalWrite(pin, value)
Parameters: pin: the pin number
value: HIGH or LOW
Returns: none
Example
int ledPin = 13; // LED connected to digital pin 13
void setup(){
pinMode(ledPin, OUTPUT); // sets the digital pin as output
}
void loop(){
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for a second
digitalWrite(ledPin, LOW); // sets the LED off
delay(1000); // waits for a second
}
Sets pin 13 to HIGH, makes a one-second-long delay, and sets the pin back to LOW.
Note: The analog input pins can be used as digital pins, referred to as A0, A1, etc.
2.3. analogRead()
Description: Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini and Nano, 16 on the Mega), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. The input range and resolution can be changed using analogReference().
It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.
Syntax: analogRead(pin)
Parameters: pin: the number of the analog input pin to read from (A: 0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega)
Returns: int (0 to 1023)
Note: If the analog input pin is not connected to anything, the value returned by analogRead() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.).
Example:
int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read
void setup(){
Serial.begin(9600); // setup serial
}
void loop(){
val = analogRead(analogPin); // read the input pin
Serial.println(val); // debug value
}
2.4. analogWrite()
Description: Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on the Leonardo also run at 980 Hz.
- On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.
- The Arduino Due supports analogWrite() on pins 2 through 13, plus pins DAC0 and DAC1. Unlike the PWM pins, DAC0 and DAC1 are Digital to Analog converters, and act as true analog outputs.
- You do not need to call pinMode() to set the pin as an output before calling analogWrite().
- The analogWrite function has nothing to do with the analog pins or the analogRead function.
Syntax: analogWrite(pin, value)
Parameters: pin: the pin to write to.
value: the duty cycle: between 0 (always off) and 255 (always on).
Returns: nothing
Notes and Known Issues: The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.
Example: Sets the output to the LED proportional to the value read from the potentiometer.
int ledPin = 9; // LED connected to digital pin 9
int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0; // variable to store the read value
void setup(){
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop(){
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
3. Cấu trúc điều khiển
if(...){}else {}
4. Vòng lặp
for(int x=0;x<10 span="" x="">10>
digitalWrite(ledpin[x],LOW);// tắt các đèn
}
No comments:
Post a Comment