คำสั่งต่างๆของ arduino
คำสั่งพื้นฐานสำหรับ Arduino ที่ควรรู้
จากที่ไปอบรมมาเรามาเริ่มสิ่งแรกที่เราต้องรู้ก่อนที่จะเริ่มเขียนนะครับ คือคำสั่งพื้นฐาน
แต่หัวหน้าผมบอกว่าจะ อธิบายเพิ่มเติมอีกที ทุกคนรอนะครับ
1.pinMode() ใช้กำหนดทิศทางสัญญาณ (I/O direction) ของขาดิจิทัล
Syntax : pinMode(pin, mode)
ตัวอย่างการใช้งาน
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
}
2.digitalRead() ใช้อ่านค่าจากขาดิจิทัลที่ถูกกำหนดให้เป็นอินพุต
Syntax : digitalRead(pin)
ตัวอย่างการใช้งาน
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
}
3.digitalWrite() ใช้เขียนค่า (LOW/HIGH) ให้ขาดิจิทัลที่ถูกกำหนดให้เป็นเอาต์พุต
Syntax : digitalWrite(pin, value)
ตัวอย่างการใช้งาน
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 }
4.analogWrite() ใช้สัญญาณ PWM เป็นเอาต์พุต
Syntax : analogWrite(pin, value)
ตัวอย่างการใช้งาน
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);
5.analogRead() ใช้อ่านค่าจากขาแอนะล็อก-อินพุต
Syntax : analogRead(pin)
ตัวอย่างการใช้งาน
int analogPin = 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 }
6.analogReference() กำหนดระดับแรงดันอ้างอิงสำหรับการอ่านค่าจากขาแอนะล็อก-อินพุต
Syntax : analogReference(type)
Parameters
type: which type of reference to use (DEFAULT, INTERNAL, INTERNAL1V1, INTERNAL2V56, or EXTERNAL).
7.Delay() รอเวลาให้ผ่านนไปตามระยะเวลาที่กำหนด (มิลลิวินาที) ก่อนที่จะทำขั้นตอนต่อไป
Syntax : delay(ms)
ตัวอย่างการใช้งาน
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 }
8.delayMicroseconds() รอเวลาให้ผ่านไปตามระยะเวลาที่กำหนด (ไมโครวินาที)
Syntax : delayMicroseconds(us)
ตัวอย่างการใช้งาน
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 }
9.randomSeed() กำหนดค่าเริ่มต้นสำหรับการสร้างเลขแบบสุ่มเทียม
ตัวอย่างการใช้งาน
long randNumber;
void setup(){
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop(){
randNumber = random(300);
Serial.println(randNumber);
delay(50);
}
10.random() ให้ค่าเป็นเลขสุ่มเทียม (Pseudo-Random Number)
Syntax :
random(max)
random(min, max)
ตัวอย่างการใช้งา่น
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);
}
11.millis() บอกเวลาที่ผ่านไปในหน่วยเป็นมิลลินาที นับตั้งแต่โปรแกรมเริ่มต้นทำงาน
ตัวอย่างการใช้งาน
unsigned long time; 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); }
12.min() ให้ค่าน้อยที่สุดระหว่างตัวเลขสองค่าที่นำมาเปรียบเทียบกัน
ตัวอย่างการใช้งาน
sensVal = min(sensVal, 100); // assigns sensVal to the smaller of sensVal or 100 // ensuring that it never gets above 100.
13.max() ให้ค่าที่มากที่สุดระหว่างตัวเลขสองค่าที่นำมาเปรียบบเทียบกัน
ตัวอย่างการใช้งาน
sensVal = max(senVal, 20); // assigns sensVal to the larger of sensVal or 20 // (effectively ensuring that it is at least 20)
14.abs() ให้ค่าสัมบูรณ์ของตัวเลข
ตัวอย่างการใช้งาน
abs(a++); // avoid this - yields incorrect results abs(a); a++; // use this instead - keep other math outside the function
ความคิดเห็น
แสดงความคิดเห็น