Cover image

Arduino ตอน5 Function ที่ใช้ใน Arduino

14 Jun 2019

Share to:

สวัสดีครับ สําหรับเนื้อหาในบทความนี้เราจะอธิบายถึง Function ที่ Arduino เตรียมไว้ให้เราใช้ ว่าแต่ละ Function มีไว้ทําอะไร ใช้งานอย่างไร โดยผมจะหยิบมาเฉพาะที่ใช้กันบ่อยๆ มาดูกันเลยครับ

สารบัญ

digitalRead()

Description

เป็น Function สําหรับอ่านค่าจาก I/O โดยระบุ Pin ที่ต้องการจะอ่าน ค่าที่ได้จะเป็น HIGH หรือ LOW

Syntax

digitalRead(pin);

Parameters

pin: Pin number ที่ต้องการจะอ่าน

Returns

HIGH หรือ LOW

Example Code

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
}

digitalWrite()

Description

กําหนดค่า HIGH หรือ LOW ให้กับ digital pin

โดยเราจะต้องกําหนด Pin เป็น OUTPUT ก่อนโดยใช้ Function pinMode()

หลังจากนั้นเมื่อเรากําหนดค่าให้กับ Pin นั้นเป็น HIGH จะมีกระแสไฟฟ้าออกจาก Pin นั้น 3.3V หรือ 5V แล้วแต่ชนิดของ Arduino board และถ้าเรากําหนดค่าให้กับ Pin นั้นเป็น LOW จะมีกระแสไฟฟ้า ออกจาก Pin นั้น 0V

Syntax

digitalWrite(pin, value);

Parameters

pin: the Arduino pin number. value: HIGH or LOW.

Returns

Example Code

ตัวอย่างนี้เป็นโปรแกรมไฟกระพริบโดยจะหน่วงเวลา 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
}

pinMode()

Description

เป็น Function ที่ใช้ในการ Set mode ของ Pin ว่าจะเป็น mode ไหน เช่น INPUT, OUTPUT, or INPUT_PULLUP

Syntax

pinMode(pin, mode);

Parameters

pin: Pin number mode: INPUT, OUTPUT, or INPUT_PULLUP

Returns

Example Code

ตัวอย่างนี้เป็นโปรแกรมไฟกระพริบโดยจะหน่วงเวลา 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
}

analogRead()

Description

เป็น Function สําหรับอ่านค่า Analog จาก Pin ที่ต้องการ ซึ่ง Input จะอ่านค่าแรงดันไฟฟ้าจาก Pin ที่กําหนด แล้วแปลงเป็นข้อมูล Digital 10-bit จะได้ความละเอียดอยู่ที่ 1024 ตัวอย่างถ้าเราอ่านข้อมูลจาก Arduino board ชนิดที่ใช้แรงดันไฟฟ้า 5V เราก็จะอ่านได้ที่ความระเอียด 5/1024 = 0.0049 volts (4.9 mV) ต่อหน่วย

ตารางด้านล่างนี้เป็นตารางข้อมูลของ Arduino board แต่ละรุ่น

BOARVOLTAGEPINSMAX RESOLUTION
Uno5 VoltsA0 to A510 bits
Mini, Nano5 VoltsA0 to A710 bits
Mega, Mega2560, MegaADK5 VoltsA0 to A1410 bits
Micro5 VoltsA0 to A1110 bits
Leonardo5 VoltsA0 to A1110 bits
Zero3.3 VoltsA0 to A512 bits
Due3.3 VoltsA0 to A1112 bits
MKR Family boards3.3 VoltsA0 to A612 bits

Syntax

analogRead(pin);

Parameters

pin: Pin number

Returns

ค่าที่อ่านได้จาก Pin ที่กําหนด มี type เป็น Int

Example Code

อ่านค่าจาก 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
}

analogReference()

Description

Function สําหรับกําหนดแรงดันไฟฟ้าสูงสุดที่ต้องการจะอ่านจากจาก Analog Pin

Arduino AVR Boards (Uno, Mega, Leonardo, etc.)

  • DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)
  • INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328P and 2.56 volts on the ATmega32U4 and ATmega8 (not available on the Arduino Mega)
  • INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only)
  • INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)
  • EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference.

Arduino SAMD Boards (Zero, etc.)

  • AR_DEFAULT: the default analog reference of 3.3V
  • AR_INTERNAL: a built-in 2.23V reference
  • AR_INTERNAL1V0: a built-in 1.0V reference
  • AR_INTERNAL1V65: a built-in 1.65V reference
  • AR_INTERNAL2V23: a built-in 2.23V reference
  • AR_EXTERNAL: the voltage applied to the AREF pin is used as the reference

Arduino megaAVR Boards (Uno WiFi Rev2)

  • DEFAULT: a built-in 0.55V reference
  • INTERNAL: a built-in 0.55V reference
  • VDD: Vdd of the ATmega4809. 5V on the Uno WiFi Rev2
  • INTERNAL0V55: a built-in 0.55V reference
  • INTERNAL1V1: a built-in 1.1V reference
  • INTERNAL1V5: a built-in 1.5V reference
  • INTERNAL2V5: a built-in 2.5V reference
  • INTERNAL4V3: a built-in 4.3V reference
  • EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference

Arduino SAM Boards (Due)

  • AR_DEFAULT: the default analog reference of 3.3V. This is the only supported option for the Due.

Syntax

analogReference(type)

Parameters

type: รายละเอียดอยู่ใน Description ด้านบน

Returns


analogWrite()

Description

เป็น Function สําหรับกําหนดค่า Output Analog โดยจะใช้หลักการ PWM สัญญานที่ออกจาก Pin จะเป็นระดับแรงดันไฟฟ้าตามที่เรากําหนด

ตารางด้านล่างเป็นรายละเอียดของ Arduino board แต่ละรุ่น

BOARDPWM PINSPWM FREQUENCY
Uno, Nano, Mini3, 5, 6, 9, 10, 11490 Hz (pins 5 and 6: 980 Hz)
Mega2 - 13, 44 - 46490 Hz (pins 4 and 13: 980 Hz)
Leonardo, Micro, Yún3, 5, 6, 9, 10, 11, 13490 Hz (pins 3 and 11: 980 Hz)
Uno WiFi Rev.23, 5, 6, 9, 10976 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-131000 Hz
1013, 5, 6, 9pins 3 and 9: 490 Hz, pins 5 and 6: 980 Hz

Syntax

analogWrite(pin, value);

Parameters

pin: Pin ที่ต้องการ value: the duty cycle: รับข้อมูลเป็น Int ค่าตั้งแต่ 0-255 โดยที่ 0 คือ Off และ 255 คือ On

Returns

Example Code

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
}

delay()

Description

เป็น Function หน่วงเวลาโดนเราสามารถกําหนดระยะเวลาการหน่วงเป็น milliseconds

Syntax

delay(ms);

Parameters

ms: milliseconds (เป็นข้อมูลชนิด long)

Returns

Example Code

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
}

delayMicroseconds()

Description

เป็น Function หน่วงเวลาโดนเราสามารถกําหนดระยะเวลาการหน่วงเป็น Microseconds

Syntax

delayMicroseconds(us);

Parameters

us: เวลาที่ต้องการหน่วงเวลา Microseconds (ข้อมูลชนิด unsigned int)

Returns

Example Code

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
}

abs()

Description

หาค่าสัมบูรณ์ (absolute) ของตัวเลขเป็นการทําให้ค่าของตัวแปรเป็นค่าจํานวนเต็มบวก

Syntax

abs(x);

Parameters

x: number

Returns

|x|


constrain()

Description

ปัดค่าตัวเลขที่น้อยกว่าหรือมากกว่าให้อยู่ในช่วงที่กําหนด

Syntax

constrain(x, a, b);

Parameters

  • x: ตัวเลขที่ต้องการปัดค่าให้อยู่ในช่วงที่กําหนดสามารถเป็นข้อมูลชนิดใดก็ได้
  • a: ค่าตํ่าสุดของช่วงที่กําหนด
  • b: ค่าสูงสุดของช่วงที่กําหนด

Returns

  • x เมื่อ x มีค่าอยู่ระหว่าง a และ b
  • a เมื่อ x มีค่าน้อยกว่า a
  • b เมื่อ x มีค่ามากกว่า b

Example Code

sensVal = constrain(sensVal, 10, 150);  // limits range of sensor values to between 10 and 150

map()

Description

แปลงค่าจาก range หนึง เป็นอีก range หนึ่ง เช่น เรามีค่า x ที่อยู่ในช่วง range 0-1024 แต่เราต้องการจะรู้ว่าค่า x มีค่าเท่าไรของช่วง range 0-255 เราสามารถใช้ Function คํานวณออกมาได้ครับ

Syntax

map(value, fromLow, fromHigh, toLow, toHigh);

Parameters

  • value: ค่าที่อยู่ในช่วง range ตั้งต้น
  • fromLow: ค่าตํ่าสุดของ range ตั้งต้น
  • fromHigh: ค่าสูงสุดของ range ตั้งต้น
  • toLow: ค่าตํ่าสุดของ range ใหม่
  • toHigh: ค่าสูงสุดของ range ใหม่

Returns

ค่าที่ได้เมื่ออยู่ใน range ใหม่

Example Code

/* 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);
}

การคํานวนของ Function map()

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;
}

max()

Description

หาค่าสูงสุดของจํานวน 2 จํานวน

Syntax

max(x, y);

Parameters

  • x: จํานวนแรก
  • y: จํานวนที่สอง

Returns

ค่าสูงสุด

Example Code

sensVal = max(sensVal, 20); // assigns sensVal to the larger of sensVal or 20
                            // (effectively ensuring that it is at least 20)

min()

Description

หาค่าตํ่าสุดของจํานวน 2 จํานวน

Syntax

min(x, y)

Parameters

  • x: จํานวนแรก
  • y: จํานวนที่สอง

Returns

ค่าตํ่าสุดของสองจํานวน

Example Code

sensVal = min(sensVal, 100);  // assigns sensVal to the smaller of sensVal or 100
                              // ensuring that it never gets above 100.

pow()

Description

คํานวณเลขยกกําลัง

Syntax

pow(base, exponent);

Parameters

  • base: ฐาน
  • exponent: เลขยกกําลัง

Returns

ผลการคํานวณเลขยกกําลัง

Example Code

z = pow(x, y);

sq()

Description

คํานวณหาค่า square หรือ หาค่ายกกําลัง2

Syntax

sq(x);

Parameters

x: จํานวนที่ต้องการจะหาค่ายกกําลัง2

Returns

ค่ายกกําลัง2 x

Example

int inputSquared = sq(10);

sqrt()

Description

คํานวณ สแควรูท

Syntax

sqrt(x);

Parameters

x: จํานวนที่ต้องการจะหาค่า สแควรูท

Returns

สแควรูทของ x

Example

double res = sqrt(25);

cos()

Description

คํานวณหาค่า cos จากมุม (in radians) โดยผลลัพธ์จะอยู่ระหว่าง -1 ถึง 1

Syntax

cos(rad);

Parameters

rad: radians รับประเภทของข้อมูลเป็น float.

Returns

ค่า cos โดยมี type เป็น double


sin()

Description

คํานวณหาค่า cos จากมุม (in radians) โดยผลลัพธ์จะอยู่ระหว่าง -1 ถึง 1

Syntax

sin(rad);

Parameters

rad: radians รับประเภทของข้อมูลเป็น float.

Returns

ค่า sin โดยมี type เป็น double


tan()

Description

คํานวณหาค่า tan จากมุม (in radians)

Syntax

tan(rad);

Parameters

rad: The angle in radians. Allowed data types: float.

Returns

ค่า tan โดยมี type เป็น double


random()

Description

สุมตัวเลข

Syntax

random(max);
random(min, max);

Parameters

  • min: ค่าตําสุดของผลลัพธ์ที่จะ Random
  • max: ค่าสูงสุดของผลลัพธ์ที่จะ Random

Returns

จํานวนที่ได้จากการสุ่มโดนจะอยู่ในช่วง min ถึง max-1 และมีประเภทของข้อมูลเป็น long

Example Code

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);
}

randomSeed()

Description

คือการกําหนดค่า seed ให้กับ random โดยค่า seed มีไว้กําหนดชุดของการ random เช่น ถ้าเรากําหนดค่า seed เป็น 1 เมื่อรันโปรแกรมหลายๆครั้ง โปรแกรมจะสุ่มค่าออกมาเป็นชุดเดียวกัน แต่ถ้าเรากําหนด seed เป็นค่าอื่นๆ ข้อมูลที่สุ่มออกมาจะไม่เหมือนกัน และถ้าค่า seed เปลี่ยนทุกครั้งที่รันโปรแกรม ข้อมูลที่สุ่มได้ จะออกมาไม่เหมือนกันเลย

Syntax

randomSeed(seed)

Parameters

seed: ค่า seed

Returns

Example Code

long randNumber;

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(0));
}

void loop() {
  randNumber = random(300);
  Serial.println(randNumber);
  delay(50);
}

bit()

Description

หาค่าของ bit ที่ระบุ เช่น

  • 0 bit คือ 1 (1)
  • 1 bit คือ 2 (10)
  • 2 bit คือ 4 (100)
  • 3 bit คือ 8 (1000)
  • 4 bit คือ 16 (10000)
  • 5 bit คือ 32 (100000)
  • 6 bit คือ 64 (1000000)
  • 7 bit คือ 128 (10000000)
  • 8 bit คือ 256 (100000000)
  • 9 bit คือ 512 (1000000000)
  • 10 bit คือ 1024 (10000000000)

Syntax

bit(n)

Parameters

n: bit

Returns

ค่าของ bit ที่ระบุ


bitClear()

Description

Clear bit ของ Number ให้เป็น 0 โดยระบุจํานวน Bit ที่ต้องการ Clear ซึ่งจะเริ่ม Clear จาก Bit ขวาสุด

Syntax

bitClear(x, n)

Parameters

  • x: Number ต้องการ Clear
  • n: จํานวน Bit ที่ต้องการ Clear ซึ่งจะเริ่ม Clear จาก Bit ขวาสุด

Returns


bitRead()

Description

อ่านค่า Bit ของ Number

Syntax

bitRead(x, n);

Parameters

  • x: Number ที่ต้องการอ่าน
  • n: Bit ที่ต้องการอ่าน เริ่มต้นจาก 0 และนับจากทางขวา

Returns

ค่าของ bit ที่อ่านได้


bitSet()

Description

Set ค่า 1 ให้กับ bit ที่ระบุ

Syntax

bitSet(x, n);

Parameters

  • x: Number ที่ต้องการ
  • n: ตําแหน่งของ bit ที่ก้องการ set โดยเริ่มต้นจาก 0 และนับจากทางขวา

Returns


bitWrite()

Description

Set ค่าให้กับ bit ที่ระบุ โดยเราสามารถระบุค่าที่ต้องการจะ Set ได้ว่าจะให้ Set 1 หรือ 0

Syntax

bitWrite(x, n, b)

Parameters

  • x: Number ที่ต้องการ
  • n: ตําแหน่งของ bit ที่ก้องการ set โดยเริ่มต้นจาก 0 และนับจากทางขวา
  • b: ค่าที่ต้องการจะ Set (0 or 1).

Returns

Example Code

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() {}

highByte()

Description

ตัด Byte สูง หรือ byte ที่อยู่ด้านซ้ายออกมา เช่น

  • 11000000 00011000 จะได้ 11000000
  • 11000111 00011000 จะได้ 11000111
  • 11001000 00011000 จะได้ 11001000

Syntax

highByte(x)

Parameters

x: ข้อมูลที่ต้องการตัด

Returns

ข้อมูลที่ตัดได้


lowByte()

Description

ตัด Byte ตํ่า หรือ byte ที่อยู่ด้านขวาออกมา เช่น

  • 11000000 00011011 จะได้ 00011011
  • 11000111 00011000 จะได้ 00011000
  • 11001000 11111000 จะได้ 11111000

Syntax

lowByte(x);

Parameters

x: ข้อมูลที่ต้องการตัด

Returns

ข้อมูลที่ตัดได้

นอกจากนี้ยังมี Function อื่นๆอีก สามารถเข้าไปศึกษาเพิ่มเติมได้ที่ arduino.cc/reference/en/

Suggestion blogs

เหรียญ 1 บาท พระราชพิธีสถาปนาสมเด็จพระเจ้าลูกยาเธอเจ้าฟ้าวชิราลงกรณ ขึ้นทรงดำรงตำแหน่งสมเด็จพระบรมโอรสาธิราชเจ้าฟ้ามหาวชิราลงกรณ สยามมกุฎราชกุมาร

พระราชพิธีสถาปนาสมเด็จพระเจ้าลูกยาเธอเจ้าฟ้าวชิราลงกรณ ขึ้นทรงดำรงตำแหน่งสมเด็จพระบรมโอรสาธิราชเจ้าฟ้ามหาวชิราลงกรณ สยามมกุฎราชกุมาร พ.ศ.2515ข้อมูลผลิตเป็นที่ระลึกเนื่องในพระราชพิธีสถาปนาสมเด็จพระเจ้าลูกยาเธอเจ้าฟ้าวชิราลงกรณ ขึ้นทรงดำรงตำแหน่งสมเด็จพระบรมโอรสาธิราชเจ้าฟ้ามหาวชิราลงกรณ สยามมกุฎราชกุมาร เมื่อวันที่ 28 ธันวาคม พ.ศ. 2515

HTTPS (SSL) wordpress config

วิธีการทำให้ wordpress support https (ssl) มีขั้นตอนดังนี้ เข้าไปที่ Admin-->Setting-->General Setting เปลี่ยน WordPress Address (URL) และ Site Address (URL) เป็น https

การทํางานของ HTTPS, SSL

สวัสดีครับ บทความนี้เป็นเรื่องการทํางานของ https หรือ ssl ซึ่งปัจจุบันเราก็คุ้นเคยกันดีอยู่แล้วเวลาเราใช้งาน Internet ตรง Url บางเว็บเป็น http:// บางเว็บก็จะเป็น https:// นี่แหละครับที่เราจะพูกถึงกัน


Copyright © 2019 - 2024 thiti.dev |  v1.19.0 |  Privacy policy |  status | 

            วงแหวนเว็บ