การใช้งานฟังก์ชัน Math กับบอร์ด Arduino ด้วยโปรแกรม Arduino IDE
ฟังก์ชัน Math กับบอร์ด Arduino
ฟังก์ชัน Math หรือ ฟังก์ชันทางคณิตศาสตร์ในโปรแกรม Arduino IDE จะมีอยู่ด้วยกัน 8 ฟังก์ชัน
คือ abs(), constrain(), map(), max(), min(), pow(), sq(), sqrt()
🔹 ตัวอย่างฟังก์ชัน Math ที่ใช้กับ Arduino
| ฟังก์ชัน | คำอธิบาย | ตัวอย่าง |
|---|---|---|
abs(x) | หาค่าสัมบูรณ์ | abs(-15) ➝ 15 |
sq(x) | หาค่ายกกำลังสอง | sq(5) ➝ 25 |
sqrt(x) | หาค่ารากที่สอง | sqrt(49) ➝ 7.0 |
pow(x, y) | หาค่ายกกำลัง | pow(2, 3) ➝ 8.0 |
min(x, y) | หาค่าที่น้อยที่สุด | min(3, 7) ➝ 3 |
max(x, y) | หาค่าที่มากที่สุด | max(3, 7) ➝ 7 |
constrain(x, a, b) | จำกัดค่าให้อยู่ในช่วง | constrain(120, 0, 100) ➝ 100 |
map(value, fromLow, fromHigh, toLow, toHigh) | แปลงค่าจากช่วงหนึ่งไปอีกช่วง | map(50, 0, 100, 0, 255) ➝ 127 |
1. abs()
ฟังก์ชัน abs() เป็นฟังก์ชันในการใส่ค่าสัมบูรณ์ให้กับตัวแปร ซึ่งตัวแปรที่เป็นค่าลบจะถูกทำให้เป็นค่าบวก
วิธีการใช้งานคือ abs(ตัวแปรที่ต้องการใส่ค่าสัมบูรณ์)
void setup() {
Serial.begin(115200);
while(!Serial);
Serial.flush();
}
void loop() {
int x = -50;
Serial.println("Don't use ans() x = " + String(x));
x = abs(x);
Serial.println("Use ans() x = " + String(x));
Serial.println("");
delay(5000);
}
ประกาศตัวแปร x มีค่าเท่ากับ -50 และจะแสดงค่า x ออกมาที่ Serial monitor
ขณะที่ไม่ใช้งานฟังก์ชัน abs() และใช้งานฟังก์ชัน abs() และจะเห็นว่าเมื่อใช้งานฟังก์ชัน
abs() ค่า x ที่แสดงออกมาจะมีค่าเป็นค่าบวก
ให้ทำการอัปโหลดซอร์สโค้ดตัวอย่างลงบนบอร์ด Arduino
โดยไม่ต้องต่ออุปกรณ์ใดๆ และเปิดหน้าจอ Serial monitor ขึ้นมาโดยการกดปุ่ม Ctrl + Shift + M
ในโปรแกรมจะประกาศตัวแปร x มีค่าเท่ากับ -50 และจะแสดงค่า x ออกมาที่ Serial monitor ขณะที่ไม่ใช้งานฟังก์ชัน abs()
และใช้งานฟังก์ชัน abs() และจะเห็นว่าเมื่อใช้งานฟังก์ชัน abs() ค่า x ที่แสดงออกมาจะมีค่าเป็นค่าบวก
2. constrain()
เป็นฟังก์ชันที่ใช้ในการควบคุมค่าตัวแปรให้อยู่ในกรอบที่กำหนด เช่น ประกาศตัวแปร x แบบสุ่มค่าที่ 0 – 500
แต่ต้องการค่าน้อยสุดที่ 100 และค่ามากสุดที่ 200 สามารถใช้งานฟังก์ชัน constrain
ได้โดยการพิมพ์ constrain(x, 100, 200); หากค่าตัวแปร x น้อยกว่า 100 ค่าที่ได้จากฟังก์ชัน constrain
จะเท่ากับ 100 หากค่าตัวแปร x มากกว่า 200 ค่าที่ได้จากฟังก์ชัน constrain จะเท่ากับ 200
และหากค่าตัวแปร x อยู่ในช่วง 100 – 200 ค่าที่ได้จากฟังก์ชัน constrain จะเท่ากับค่า x นั้นๆ
void setup() {
Serial.begin(115200);
while(!Serial);
Serial.flush();
}
void loop() {
int x = random(0 - 500); //สุ่มค่า 0 - 500 เก็บในตัวแปร x
Serial.println("x = " + String(x));
int y = constrain(x, 100, 200);
Serial.println("y = " + String(y));
Serial.println("");
delay(2000);
}
3. map()
เป็นฟังก์ชันที่ใช้ในการเปรียบเทียบค่าตัวแปรให้ด้วยสมการเส้นตรง
เช่น ต้องการให้ตัวแปร x ที่มีค่า 0 – 100 เปลี่ยนแปลงเป็นค่า 500 – 1,000 สามารถทำได้โดยการพิมพ์
map(x, 0, 100, 500, 1000); หากค่า x = 0 จะถูกแปลงเป็น x = 500 และหากค่า x = 100 จะถูกแปลงเป็น 1,000
void setup() {
Serial.begin(115200);
while(!Serial);
Serial.flush();
}
void loop() {
int x = 0;
x = map(x, 0, 100, 500, 1000);
Serial.println("x = 0 after map function x = " + String(x)); //\n = เว้นบรรทัด 1 ครั้ง \t = tap 1 ครั้ง
x = 100;
x = map(x, 0, 100, 500, 1000);
Serial.println("x = 100 after map function x = " + String(x));
x = random(0 - 100);
Serial.print("x = " + String(x));
x = map(x, 0, 100, 500, 1000);
Serial.println(" after map function x = " + String(x));
delay(5000);
Serial.println("");
}
อย่างไรก็ตามในการใช้งานฟังก์ชัน map จะไม่เหมาะกับการลดขนาดตัวแปรเนื่องจากเป็นตัวแปรประเภทจำนวนเต็มใน
การลดขนาดเช่นจากค่า 0 – 100 ลดขนาดให้เป็น 0 – 5 โปรแกรมจะแปลงค่าเพียงแค่จำนวนเต็ม ทำให้ไม่ทราบจำนวนทศนิยมจะทำให้ความระเอียดของจำนวนที่ได้ลดลง
ในการลดขนาดตัวแปรจำเป็นต้องใช้วิธีการลดขนาดด้วยสมการและประกาศใช้งานตัวแปรเป็น float
4. max()
เป็นฟังก์ชันส่งออกตัวแปรมากกว่าค่าที่กำหนด เช่น
การสุ่มค่า x ตั้งแต่ 0 – 500 กำหนดให้ x มากกว่าจึงจะส่งค่าออกไป หากค่า x ต่ำกว่า 200 ก็จะส่งออกค่า 200 ออกมาแทน
void setup() {
Serial.begin(115200);
while(!Serial);
Serial.flush();
}
void loop(){
int x = random(0 - 500); //สุ่มค่า 0 - 500 เก็บในตัวแปร x
Serial.print("\nx = " + String(x));
x = max(x, 200);
Serial.println(" after max function x = " + String(x));
//วิธีที่ 2 โดยให้ผผลเหมือนกันแต่จะเขียนคำสั่งมากกว่า
x = random(0 - 500);
Serial.print("\nx = " + String(x));
if(x >= 200){
Serial.println(" after max function x = " + String(x));
}
else{
x = 200;
Serial.println(" after max function x = " + String(x));
}
Serial.println("________");
delay(2000);
}
5. min()
เป็นฟังก์ชันที่มีผลตรงกันข้ามกับ max ซึ่งจะแสดงค่าที่น้อยกว่าที่กำหนดไว้
void setup() {
Serial.begin(115200);
while(!Serial);
Serial.flush();
}
void loop(){
int x = random(0 - 500); //สุ่มค่า 0 - 500 เก็บในตัวแปร x
Serial.print("\nx = " + String(x));
x = min(x, 200);
Serial.println(" after max function x = " + String(x));
//วิธีที่ 2 โดยให้ผผลเหมือนกันแต่จะเขียนคำสั่งมากกว่า
x = random(0 - 500);
Serial.print("\nx = " + String(x));
if(x <= 200){
Serial.println(" after max function x = " + String(x));
}
else{
x = 200;
Serial.println(" after max function x = " + String(x));
}
Serial.println("_______");
delay(2000);
}
6. pow()
เป็นฟังก์ชันเลขยกกำลังสามารถใช้งานได้ดดังนี้ pow(เลขฐานเป็นจำนวนเต็ม, เลขชี้กำลัง)
void setup() {
Serial.begin(115200);
while(!Serial);
Serial.flush();
}
void loop(){
int x = 10;
int y = 2;
int power = pow(x, y); //10^2
Serial.println("\n base number = " + String(x));
Serial.println("exponent number = " + String(y));
Serial.println(" 10^2 = " + String(power));
delay(2000);
}
7. sq()
sq() หรือ square เป็นฟังก์ชันเลขยกกำลัง 2 สามารถใช้งานได้ดดังนี้ sq(ค่าตัวเลขที่ต้องการยกกำลัง)
void setup() {
Serial.begin(115200);
while(!Serial);
Serial.flush();
}
void loop(){
float x = 4.5;
float square = sq(x);
Serial.println("\n x = " + String(x));
Serial.println("Square x = " + String(square));
delay(2000);
}
8. sqrt()
เป็นฟังก์ชันถอดรากที่ 2 สามารถใช้งานได้ดดังนี้ sqrt(ตัวเลขที่ต้องการถอดรากที่ 2);
float x = 15;
void setup() {
Serial.begin(115200);
while(!Serial);
Serial.flush();
}
void loop(){
Serial.println("\nx = " + String(x));
Serial.print("square root " + String(x));
float y = sqrt(x);
Serial.println(" = " + String(y));
delay(2000);
x++; // x = x+1
}
.png)







