(ข) กำรเปลี่ยนแปลงของแรงดันไฟฟำ้ เน่ืองจำกทศิ ทำงกำรควบคุม Joystick เปลี่ยนไป การเช่อื มต่ออปุ กรณ์ Page | 48
โปรแกรม #define X A0 #define Y A1 #define BUTTON 2 int xPosition = 0; int yPosition = 0; int buttonState = 0; void setup() { Serial.begin(9600); pinMode(X, INPUT); pinMode(Y, INPUT); pinMode(BUTTON, INPUT_PULLUP); } void loop() { xPosition = analogRead(X); yPosition = analogRead(Y); buttonState = digitalRead(BUTTON); Serial.print(\"X: \"); Serial.print(xPosition); Serial.print(\" | Y: \"); Serial.print(yPosition); Serial.print(\" | Button: \"); Serial.println(buttonState); delay(250); } Page | 49
การทดลองที่ 14 Serial Communication อุปกรณ์ 1) LED 3 หลอด (สีแดง เหลอื ง และเขียว) 2) ตัวตำ้ นทำน 220 โอหม์ 3 ตัว กำรทดลองนี้เปน็ กำรควบคุมเปดิ -ปิด LED จำนวน 3 หลอด ผ่ำน Serial Monitor โดยผ้ใู ช้สำมำรถเลอื ก เปิด-ปิด LED ทลี ะหลอด หรือเปิด-ปิดทุกหลอดพรอ้ มกนั (รหัสเปิด-ปดิ คอื r, y, g, o, และ x) การเช่ือมต่ออุปกรณ์ Page | 50
โปรแกรม #define R_LED 7 #define Y_LED 6 #define G_LED 5 int r_status = 0; int y_status = 0; int g_status = 0; void setup() { Serial.begin(9600); pinMode(R_LED, OUTPUT); pinMode(Y_LED, OUTPUT); pinMode(G_LED, OUTPUT); } void loop() { while (Serial.available() > 0) { char serialChar = Serial.read(); switch(serialChar) { case 'o': case 'O': Serial.println(\"All LEDs On!\"); r_status = 1; y_status = 1; g_status = 1; digitalWrite(R_LED, HIGH); digitalWrite(Y_LED, HIGH); digitalWrite(G_LED, HIGH); break; case 'r': case 'R': Serial.print(\"Red LED \"); r_status = toggle(r_status, R_LED); break; case 'y': case 'Y': Serial.print(\"Yellow LED \"); y_status = toggle(y_status, Y_LED); break; case 'g': case 'G': Serial.print(\"Green LED \"); g_status = toggle(g_status, G_LED); break; case 'x': case 'X': Serial.println(\"All LEDs Off!\"); r_status = 0; y_status = 0; g_status = 0; digitalWrite(R_LED, LOW); digitalWrite(Y_LED, LOW); Page | 51
digitalWrite(G_LED, LOW); break; } } } int toggle (int x_status, int X_LED) { x_status = !x_status; if (x_status) { digitalWrite(X_LED, HIGH); Serial.println(\"On!\"); } else { digitalWrite(X_LED, LOW); Serial.println(\"Off!\"); } return x_status; } ตวั อยำ่ งผลลพั ธ์กำรสัง่ เปิด-ปิด LED ผำ่ น Serial Monitor Page | 52
การทดลองท่ี 15 Ethernet Shield อปุ กรณ์ 1) Ethernet Shield 1 บอร์ด 2) สำย LAN 1 เส้น 3) LED 1 หลอด 4) ตัวต้ำนทำน 330 โอหม์ 1 ตัว การเชอื่ มตอ่ อุปกรณ์ ให้ตอ่ วงจร LED เหมือนกำรทดลองที่ 2 จำกน้นั เสยี บบอรด์ Ethernet Shield เขำ้ กบั บอร์ด Arduino ดงั รูป ตำมด้วยกำรตอ่ สำย LAN เข้ำท่ี Ethernet Shield และอุปกรณเ์ ครือขำ่ ย สำหรับกำรทดลองประกอบด้วย 2 โปรแกรม ไดแ้ ก่ ตรวจสอบ IP Address ของบอร์ด Ethernet Shield และกำรจำลองให้ Ethernet Shield เปน็ Web Server เพ่ือสั่งเปิด-ปิด LED จำก Web Browser (จำกอปุ กรณ์อน่ื ๆ ท่อี ยู่ในเครือข่ำยเดยี วกัน) Page | 53
โปรแกรม 1 #include <SPI.h> #include <Ethernet.h> byte mac [] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; byte server [] = {173, 194, 126, 119}; // www.google.co.th EthernetClient client; void setup() { Serial.begin(9600); if (Ethernet.begin(mac) == 0) { Serial.println(\"Failed to configure Ethernet using DHCP\"); while(true); } delay(1000); Serial.print(\"IP address: \"); IPAddress myIPAddress = Ethernet.localIP(); Serial.print(myIPAddress); if (client.connect(server, 80) > 0) { Serial.println(\" connected\"); client.println(\"GET /search?q=arduino HTTP/1.0\"); client.println(); } else { Serial.println(\"connection failed\"); } } void loop() { if (client.available()) { char c = client.read(); } if (!client.connected()) { Serial.println(); Serial.println(\"disconnecting.\"); client.stop(); for (;;); } } Page | 54
โปรแกรม 2 #include <SPI.h> #include <Ethernet.h> #define LED 8 boolean reading = false; byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; static char baseurl [] = \"http://192.168.0.101/\"; IPAddress ip(192, 168, 0, 101); EthernetServer server = EthernetServer(80); //port 80 void setup() { pinMode(LED, OUTPUT); Ethernet.begin(mac, ip); server.begin(); } void loop() { EthernetClient client = server.available(); if (client) { boolean currentLineIsBlank = true; boolean sentHeader = false; while (client.connected()) { if (client.available()) { if(!sentHeader) { client.println(\"HTTP/1.1 200 OK\"); client.println(\"Content-Type: text/html\"); client.println(); client.println(\"LED ON/OFF\"); sentHeader = true; } char c = client.read(); if (reading && c == ' ') { reading = false; } if (c == '?') { reading = true; } if (reading) { Serial.print(c); if (c == '0') { digitalWrite(LED, LOW); break; } if (c == '1') { Page | 55
digitalWrite(LED, HIGH); break; } } if (c == '\\n' && currentLineIsBlank) { break; } if (c == '\\n') { currentLineIsBlank = true; } else { if (c != '\\r') { currentLineIsBlank = false; } } } } delay(1); client.stop(); } } ตวั อยำ่ งกำรเปิด-ปิด LED http://192.168.0.101/?1 http://192.168.0.101/?0 Page | 56
แหลง่ ขอ้ มูลเก่ียวกับ Arduino แหลง่ ข้อมลู เกีย่ วกบั ฮำร์ดแวร์และซอฟต์แวร์ https://www.arduino.cc https://www.adafruit.com https://www.sparkfun.com กำรเขยี นโปรแกรมบน Arduino (ภำษำองั กฤษ) http://www.instructables.com/class/Arduino-Class https://learn.adafruit.com/category/learn-arduino https://www.allaboutcircuits.com/latest/arduino https://programmingelectronics.com/arduino-tutorials-all http://www.circuitbasics.com/arduino กำรเขยี นโปรแกรมบน Arduino (ภำษำไทย) http://www.myarduino.net/article https://www.gravitechthai.com/guru.php https://www.arduitronics.com/article กำรเขียนโปรแกรมภำษำ C http://www.cprogramming.com https://www.tutorialspoint.com/cprogramming Page | 57
บนั ทกึ ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………… Page | 58
Search