สวัสดีครับ สําหรับเนื้อหาในบทความนี้เราจะอธิบายถึง Function ที่ Arduino เตรียมไว้ให้เราใช้ ว่าแต่ละ Function มีไว้ทําอะไร ใช้งานอย่างไร โดยผมจะหยิบมาเฉพาะที่ใช้กันบ่อยๆ มาดูกันเลยครับ
เป็น Function สําหรับอ่านค่าจาก I/O โดยระบุ Pin ที่ต้องการจะอ่าน ค่าที่ได้จะเป็น HIGH หรือ LOW
digitalRead(pin);
pin: Pin number ที่ต้องการจะอ่าน
HIGH หรือ LOW
Sets pin 13 to the same value as pin 7, declared as an input. กําหนดให้ Pin 13 มีค่าตามที่อ่านได้จาก Pin 7
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
}
กําหนดค่า HIGH หรือ LOW ให้กับ digital pin
โดยเราจะต้องกําหนด Pin เป็น OUTPUT ก่อนโดยใช้ Function pinMode()
หลังจากนั้นเมื่อเรากําหนดค่าให้กับ Pin นั้นเป็น HIGH จะมีกระแสไฟฟ้าออกจาก Pin นั้น 3.3V หรือ 5V แล้วแต่ชนิดของ Arduino board และถ้าเรากําหนดค่าให้กับ Pin นั้นเป็น LOW จะมีกระแสไฟฟ้า ออกจาก Pin นั้น 0V
digitalWrite(pin, value);
pin: the Arduino pin number. value: HIGH or LOW.
ตัวอย่างนี้เป็นโปรแกรมไฟกระพริบโดยจะหน่วงเวลา 1 วินาที
void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
เป็น Function ที่ใช้ในการ Set mode ของ Pin ว่าจะเป็น mode ไหน เช่น INPUT, OUTPUT, or INPUT_PULLUP
pinMode(pin, mode);
pin: Pin number mode: INPUT, OUTPUT, or INPUT_PULLUP
ตัวอย่างนี้เป็นโปรแกรมไฟกระพริบโดยจะหน่วงเวลา 1 วินาที
void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
เป็น Function สําหรับอ่านค่า Analog จาก Pin ที่ต้องการ ซึ่ง Input จะอ่านค่าแรงดันไฟฟ้าจาก Pin ที่กําหนด แล้วแปลงเป็นข้อมูล Digital 10-bit จะได้ความละเอียดอยู่ที่ 1024 ตัวอย่างถ้าเราอ่านข้อมูลจาก Arduino board ชนิดที่ใช้แรงดันไฟฟ้า 5V เราก็จะอ่านได้ที่ความระเอียด 5/1024 = 0.0049 volts (4.9 mV) ต่อหน่วย
ตารางด้านล่างนี้เป็นตารางข้อมูลของ Arduino board แต่ละรุ่น
BOAR | VOLTAGE | PINS | MAX RESOLUTION |
---|---|---|---|
Uno | 5 Volts | A0 to A5 | 10 bits |
Mini, Nano | 5 Volts | A0 to A7 | 10 bits |
Mega, Mega2560, MegaADK | 5 Volts | A0 to A14 | 10 bits |
Micro | 5 Volts | A0 to A11 | 10 bits |
Leonardo | 5 Volts | A0 to A11 | 10 bits |
Zero | 3.3 Volts | A0 to A5 | 12 bits |
Due | 3.3 Volts | A0 to A11 | 12 bits |
MKR Family boards | 3.3 Volts | A0 to A6 | 12 bits |
analogRead(pin);
pin: Pin number
ค่าที่อ่านได้จาก Pin ที่กําหนด มี type เป็น Int
อ่านค่าจาก Pin A3 แล้วแสดง
int analogPin = A3; // 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
}
Function สําหรับกําหนดแรงดันไฟฟ้าสูงสุดที่ต้องการจะอ่านจากจาก Analog Pin
Arduino AVR Boards (Uno, Mega, Leonardo, etc.)
Arduino SAMD Boards (Zero, etc.)
Arduino megaAVR Boards (Uno WiFi Rev2)
Arduino SAM Boards (Due)
analogReference(type)
type: รายละเอียดอยู่ใน Description ด้านบน
เป็น Function สําหรับกําหนดค่า Output Analog โดยจะใช้หลักการ PWM สัญญานที่ออกจาก Pin จะเป็นระดับแรงดันไฟฟ้าตามที่เรากําหนด
ตารางด้านล่างเป็นรายละเอียดของ Arduino board แต่ละรุ่น
BOARD | PWM PINS | PWM FREQUENCY |
---|---|---|
Uno, Nano, Mini | 3, 5, 6, 9, 10, 11 | 490 Hz (pins 5 and 6: 980 Hz) |
Mega | 2 - 13, 44 - 46 | 490 Hz (pins 4 and 13: 980 Hz) |
Leonardo, Micro, Yún | 3, 5, 6, 9, 10, 11, 13 | 490 Hz (pins 3 and 11: 980 Hz) |
Uno WiFi Rev.2 | 3, 5, 6, 9, 10 | 976 Hz |
MKR boards * | 0 - 8, 10, A3 (18), A4 (19) | 732 Hz |
MKR1000 WiFi * | 0 - 8, 10, 11, A3 (18), A4 (19) | 732 Hz |
Zero * | 3 - 13, A0 (14), A1 (15) | 732 Hz |
Due ** | 2-13 | 1000 Hz |
101 | 3, 5, 6, 9 | pins 3 and 9: 490 Hz, pins 5 and 6: 980 Hz |
analogWrite(pin, value);
pin: Pin ที่ต้องการ value: the duty cycle: รับข้อมูลเป็น Int ค่าตั้งแต่ 0-255 โดยที่ 0 คือ Off และ 255 คือ On
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
}
เป็น Function หน่วงเวลาโดนเราสามารถกําหนดระยะเวลาการหน่วงเป็น milliseconds
delay(ms);
ms: milliseconds (เป็นข้อมูลชนิด long)
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
}
เป็น Function หน่วงเวลาโดนเราสามารถกําหนดระยะเวลาการหน่วงเป็น Microseconds
delayMicroseconds(us);
us: เวลาที่ต้องการหน่วงเวลา Microseconds (ข้อมูลชนิด unsigned int)
int outPin = 8; // digital pin 8
void setup() {
pinMode(outPin, OUTPUT); // sets the digital pin as output
}
void loop() {
digitalWrite(outPin, HIGH); // sets the pin on
delayMicroseconds(50); // pauses for 50 microseconds
digitalWrite(outPin, LOW); // sets the pin off
delayMicroseconds(50); // pauses for 50 microseconds
}
หาค่าสัมบูรณ์ (absolute) ของตัวเลขเป็นการทําให้ค่าของตัวแปรเป็นค่าจํานวนเต็มบวก
abs(x);
x: number
|x|
ปัดค่าตัวเลขที่น้อยกว่าหรือมากกว่าให้อยู่ในช่วงที่กําหนด
constrain(x, a, b);
sensVal = constrain(sensVal, 10, 150); // limits range of sensor values to between 10 and 150
แปลงค่าจาก range หนึง เป็นอีก range หนึ่ง เช่น เรามีค่า x ที่อยู่ในช่วง range 0-1024 แต่เราต้องการจะรู้ว่าค่า x มีค่าเท่าไรของช่วง range 0-255 เราสามารถใช้ Function คํานวณออกมาได้ครับ
map(value, fromLow, fromHigh, toLow, toHigh);
ค่าที่ได้เมื่ออยู่ใน range ใหม่
/* Map an analog value to 8 bits (0 to 255) */
void setup() {}
void loop() {
int val = analogRead(0);
val = map(val, 0, 1023, 0, 255);
analogWrite(9, val);
}
long map(long x, long in_min, long in_max, long out_min, long out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
หาค่าสูงสุดของจํานวน 2 จํานวน
max(x, y);
ค่าสูงสุด
sensVal = max(sensVal, 20); // assigns sensVal to the larger of sensVal or 20
// (effectively ensuring that it is at least 20)
หาค่าตํ่าสุดของจํานวน 2 จํานวน
min(x, y)
ค่าตํ่าสุดของสองจํานวน
sensVal = min(sensVal, 100); // assigns sensVal to the smaller of sensVal or 100
// ensuring that it never gets above 100.
คํานวณเลขยกกําลัง
pow(base, exponent);
ผลการคํานวณเลขยกกําลัง
z = pow(x, y);
คํานวณหาค่า square หรือ หาค่ายกกําลัง2
sq(x);
x: จํานวนที่ต้องการจะหาค่ายกกําลัง2
ค่ายกกําลัง2 x
int inputSquared = sq(10);
คํานวณ สแควรูท
sqrt(x);
x: จํานวนที่ต้องการจะหาค่า สแควรูท
สแควรูทของ x
double res = sqrt(25);
คํานวณหาค่า cos จากมุม (in radians) โดยผลลัพธ์จะอยู่ระหว่าง -1 ถึง 1
cos(rad);
rad: radians รับประเภทของข้อมูลเป็น float.
ค่า cos โดยมี type เป็น double
คํานวณหาค่า cos จากมุม (in radians) โดยผลลัพธ์จะอยู่ระหว่าง -1 ถึง 1
sin(rad);
rad: radians รับประเภทของข้อมูลเป็น float.
ค่า sin โดยมี type เป็น double
คํานวณหาค่า tan จากมุม (in radians)
tan(rad);
rad: The angle in radians. Allowed data types: float.
ค่า tan โดยมี type เป็น double
สุมตัวเลข
random(max);
random(min, max);
จํานวนที่ได้จากการสุ่มโดนจะอยู่ในช่วง min ถึง max-1 และมีประเภทของข้อมูลเป็น long
long randNumber;
void setup() {
Serial.begin(9600);
// if analog input pin 0 is unconnected, random analog
// noise will cause the call to randomSeed() to generate
// different seed numbers each time the sketch runs.
// randomSeed() will then shuffle the random function.
randomSeed(analogRead(0));
}
void loop() {
// print a random number from 0 to 299
randNumber = random(300);
Serial.println(randNumber);
// print a random number from 10 to 19
randNumber = random(10, 20);
Serial.println(randNumber);
delay(50);
}
คือการกําหนดค่า seed ให้กับ random โดยค่า seed มีไว้กําหนดชุดของการ random เช่น ถ้าเรากําหนดค่า seed เป็น 1 เมื่อรันโปรแกรมหลายๆครั้ง โปรแกรมจะสุ่มค่าออกมาเป็นชุดเดียวกัน แต่ถ้าเรากําหนด seed เป็นค่าอื่นๆ ข้อมูลที่สุ่มออกมาจะไม่เหมือนกัน และถ้าค่า seed เปลี่ยนทุกครั้งที่รันโปรแกรม ข้อมูลที่สุ่มได้ จะออกมาไม่เหมือนกันเลย
randomSeed(seed)
seed: ค่า seed
long randNumber;
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
randNumber = random(300);
Serial.println(randNumber);
delay(50);
}
หาค่าของ bit ที่ระบุ เช่น
bit(n)
n: bit
ค่าของ bit ที่ระบุ
Clear bit ของ Number ให้เป็น 0 โดยระบุจํานวน Bit ที่ต้องการ Clear ซึ่งจะเริ่ม Clear จาก Bit ขวาสุด
bitClear(x, n)
อ่านค่า Bit ของ Number
bitRead(x, n);
ค่าของ bit ที่อ่านได้
Set ค่า 1 ให้กับ bit ที่ระบุ
bitSet(x, n);
Set ค่าให้กับ bit ที่ระบุ โดยเราสามารถระบุค่าที่ต้องการจะ Set ได้ว่าจะให้ Set 1 หรือ 0
bitWrite(x, n, b)
void setup() {
Serial.begin(9600);
while (!Serial) {} // wait for serial port to connect. Needed for native USB port only
byte x = 0b10000000; // the 0b prefix indicates a binary constant
Serial.println(x, BIN); // 10000000
bitWrite(x, 0, 1); // write 1 to the least significant bit of x
Serial.println(x, BIN); // 10000001
}
void loop() {}
ตัด Byte สูง หรือ byte ที่อยู่ด้านซ้ายออกมา เช่น
highByte(x)
x: ข้อมูลที่ต้องการตัด
ข้อมูลที่ตัดได้
ตัด Byte ตํ่า หรือ byte ที่อยู่ด้านขวาออกมา เช่น
lowByte(x);
x: ข้อมูลที่ต้องการตัด
ข้อมูลที่ตัดได้
นอกจากนี้ยังมี Function อื่นๆอีก สามารถเข้าไปศึกษาเพิ่มเติมได้ที่ arduino.cc/reference/en/
สวัสดีครับ บทความนี้เป็นเรื่องเกี่ยวกับการใช้ Docker ผ่าน Proxy เราจะมาดูถึงวิธีการ Config กันครับ ซึ่งวิธีการ Config proxy ให้กับ Docker นั้นไม่ยาก ทําตามขั้นตอนนี้ได้เลย
กรณีที่เราลง Adobe Premiere Pro CS6 หลังจาก Crack แล้ว เมื่อเปิดโปรแกรมขึ้นมาใช้งาน Preset จะขึ้นไม่ครบ HDV ไม่ขึ้น ดังรูป
Auto login on raspberry pi ถ้าเราใช้ debian บน raspberry pi ในโหมด gui ระบบจะ login user: pi ให้เราโดยอัตโนมัติ แต่ถ้าเราต้องการให้ login ด้วย user อื่นๆ ก็สามารถทำได้โดย แก้ไข ไฟล์ "/etc/lightdm/lightdm.conf" ด้วยคำสั่งนี้