สมมุติว่า ผมพิมไปในช่องของ Serial Monitor ว่า 50 แล้วให้ตัวแปร a เก็บค่านี้ไว้ และ ผมพิม 30 ให้ตัวแปร b เก็บค่านี้ไว้. ผมต้องเขียนยังไงหรือครับ. ขอบคุณครับ.
ตัวอย่างโคดดังนี้ครับ
int incomingByte = 0; // for incoming serial data
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}
ตัวอย่างอ้างอิง https://www.arduino.cc/en/Serial/Read
เก็บค่าไว้ในตัวแปร array โดยใช้ index ชี้ตำแหน่งที่ต้องการเก็บ
ครั้งแรก index จะเป็น 0 เมื่ออ่านค่าจะเก็บที่ตัวแปรตำแหน่ง 0
จากนั้นเพิ่มค่า index อีก 1 ดังนั้นก็จะเป็นไว้ที่ตัวแปรตำแหน่งที่ 1
ก็จะได้ตัวแปร 2 ตัว นำมาเปรียบเที่ยบได้ครับ
ตัวอย่างตามนี้ครับ
https://gist.github.com/atduskgreg/1349176
http://www.instructables.com/answers/How-to-input-NUMBERS-through-Arduino-serialmonito/
https://www.baldengineer.com/arduino-multi-digit-integers.html
ตรงนี้ผมยังไม่มีข้อมูล ต้องลองดัดแปลงโคดเพิ่มเติมครับ