ควบคุมรถบังคับด้วย App Bluetooth RC Controller วันนี้เรามาเตรียมอุปกรณ์กันก่อนนะครับ
อุปกรณ์มีดังนี้
- Arduino Uno R3 จำนวน 1 บอร์ด http://bit.ly/3bN1WWU
- Motor Drive / Motor Drive Shield L298P http://bit.ly/39UGPAf
- Bluetooth HC-06 http://bit.ly/2SV7QfH
- Smart car chassis http://bit.ly/39K4mnb
- สายไฟ http://bit.ly/37Eytec
- Buzzer http://bit.ly/2SC6FmF
การต่อวงจร
ตัวอย่างโปรแกรม
#include <SoftwareSerial.h>SoftwareSerial BTSerial(9, 10); // RX | TX // สำหรับรับค่าจาก bluetooth// give it a name:int Motor_Left_forward = 2; // pin สำหรับกำหนดทิศทางการหมุนมอเตอร์ด้านซ้ายint Motor_Left_reward = 4; // pin สำหรับกำหนดทิศทางการหมุนมอเตอร์ด้านซ้ายint Speed_Motor_Left = 3; // pin สำหรับควบคุมความเร็วมอเตอร์ซ้ายint Motor_Right_forward = 7; // pin สำหรับกำหนดทิศทางการหมุนมอเตอร์ด้านขวาint Motor_Right_reward = 6; // pin สำหรับกำหนดทิศทางการหมุนมอเตอร์ด้านขวาint Speed_Motor_Right = 5; // pin สำหรับควบคุมความเร็วมอเตอร์ขวาint Horn = 11; // pin สำหรับแตรint Speed = 0; // ตัวแปรสำหรับเก็บค่าความเร็วvoid setup() {Serial.begin(9600); // ความเร็วในการสื่อสารสำหรับคอมฯBTSerial.begin(9600); // ความเร็วในการสื่อสารสำหรับ บลูทูธpinMode(Motor_Left_forward, OUTPUT); // กำหนด pin ให้ทำงานแบบ OutputpinMode(Motor_Left_reward, OUTPUT); // กำหนด pin ให้ทำงานแบบ OutputpinMode(Speed_Motor_Left, OUTPUT); // กำหนด pin ให้ทำงานแบบ OutputpinMode(Motor_Right_forward, OUTPUT); // กำหนด pin ให้ทำงานแบบ OutputpinMode(Motor_Right_reward, OUTPUT); // กำหนด pin ให้ทำงานแบบ OutputpinMode(Speed_Motor_Right, OUTPUT); // กำหนด pin ให้ทำงานแบบ OutputpinMode(Horn, OUTPUT); // กำหนด pin ให้ทำงานแบบ Output}// the loop routine runs over and over again forever:void loop(){// รับค่าจาก bluetoothif (BTSerial.available()>0) // ตรวจสอบว่า bluetooth มีการส่งค่ามาหรือไม่{char Control = BTSerial.read(); // อ่านค่าจาก bluetooth มาเก็บไว้ที่ ตัวแปร Control// Serial.println(Control); // แสดงค่า จาก bluetooth (ตัวแปร Control) ทาง Serial Monitorif(Control == 'F') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'F'Forward(); // หุ่นยนต์เดินหน้าif(Control == 'B') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'B'Reward(); // หุ่นยนต์ถอยหลังif(Control == 'I') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'I'Turn_right(); // หุ่นยนต์เลี้ยวขวาif(Control == 'G') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'G'Turn_left(); // หุ่นยนต์เลี้ยวซ้ายif(Control == 'R') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'R'Spin_right(); // หุนยนต์หมุนขวาif(Control == 'L') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'L'Spin_left(); // หุ่นยนต์หมุนซ้ายif(Control == 'H') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'H'Reward_turn_left(); // หุ่นยนต์ถอยหลังและเลี้ยวซ้ายif(Control == 'J') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'J'Reward_turn_right(); // หุ่นยนต์ถอยหลังและเลี้ยวขวาif(Control == 'S') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'S'Stop(); // หุ่นยนต์หยุดเดินif(Control == 'V') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'V'digitalWrite(Horn, HIGH); // แตรดังif(Control == 'v') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'v'digitalWrite(Horn, LOW); // แตรหยุดดังif(Control == '1' || Control == '2' || Control == '3'|| Control == '4'|| Control == '5'|| Control == '6'|| Control == '7'|| Control == '8'|| Control == '9'|| Control == 'q') // เมื่อ bluetooth ส่งค่า 1-9 และ q มาก{if(Control == 'q') // เมื่อค่าที่ bluetooth ส่งมาเป็น 'q'{Speed = 220; // กำหนดความเร็ว = 220}else if(Control == '9') // เมื่อค่าที่ bluetooth ส่งมาเป็น '9'{Speed = 200; // กำหนดความเร็ว = 200}else if(Control == '8') // เมื่อค่าที่ bluetooth ส่งมาเป็น '8'{Speed = 180; // กำหนดความเร็ว = 180}else if(Control == '7') // เมื่อค่าที่ bluetooth ส่งมาเป็น '7'{Speed = 160; // กำหนดความเร็ว = 160}else if(Control == '6') // เมื่อค่าที่ bluetooth ส่งมาเป็น '6'{Speed = 140; // กำหนดความเร็ว = 140}else if(Control == '5') // เมื่อค่าที่ bluetooth ส่งมาเป็น '5'{Speed = 120; // กำหนดความเร็ว = 120}else if(Control == '4') // เมื่อค่าที่ bluetooth ส่งมาเป็น '4'{Speed = 100; // กำหนดความเร็ว = 100}else if(Control == '3') // เมื่อค่าที่ bluetooth ส่งมาเป็น '3'{Speed = 80; // กำหนดความเร็ว = 80}else if(Control == '2') // เมื่อค่าที่ bluetooth ส่งมาเป็น '2'{Speed = 60; // กำหนดความเร็ว = 60}else if(Control == '1') // เมื่อค่าที่ bluetooth ส่งมาเป็น '1'{Speed = 40; // กำหนดความเร็ว = 40}}}}void Forward() // ฟังก์ชันสั่งให้หนุ่นยนต์เดินหน้า{// สั่งให้มอเตอร์ซ้ายหมุนไปหน้าanalogWrite(Speed_Motor_Left,Speed); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Left_forward, HIGH); // สั่งให้มอเตอร์ซ้ายหมุนไปหน้าdigitalWrite(Motor_Left_reward, LOW);// สั่งให้มอเตอร์ขวาหมุนไปหน้าanalogWrite(Speed_Motor_Right,Speed); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Right_forward, HIGH); // สั่งให้มอเตอร์ขวาหมุนไปหน้าdigitalWrite(Motor_Right_reward, LOW);Serial.println("Forward"); // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังเดินหน้า}void Reward() // ฟังก์ชันสั่งให้หุ่นยนต์ถอยหลัง{// สั่งให้มอเตอร์ซ้ายหมุนไปกลับหลังanalogWrite(Speed_Motor_Left,Speed); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Left_forward, LOW);digitalWrite(Motor_Left_reward, HIGH); // สั่งให้มอเตอร์ซ้ายหมุนกลับหลัง// สั่งให้มอเตอร์ขวาหมุนกลับหลังanalogWrite(Speed_Motor_Right,Speed); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Right_forward, LOW);digitalWrite(Motor_Right_reward, HIGH); // สั่งให้มอเตอร์ขวาหมุนกลับหลังSerial.println("Reward"); // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังถอยหลัง}void Turn_left() // ฟังก์ชันสั่งให้หุนยนต์เลี้ยวซ้าย{// สำหรับคำนวณความเร็วในการเลี้ยวซ้ายint Speed_right = Speed + (Speed/2);if(Speed_right > 255)Speed_right = 250;analogWrite(Speed_Motor_Left,Speed-60); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Left_forward, HIGH); // สั่งให้มอเตอร์ซ้ายหมุนไปหน้าdigitalWrite(Motor_Left_reward, LOW);analogWrite(Speed_Motor_Right,Speed_right); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed_rightdigitalWrite(Motor_Right_forward, HIGH); // สั่งให้มอเตอร์ขวาหมุนไปหน้าdigitalWrite(Motor_Right_reward, LOW);Serial.println("Turn_left"); // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังเลี้ยวซ้าย}void Turn_right() // ฟังก์ชันสั่งให้หุนยนต์เลี้ยวขวา{// สำหรับคำนวณความเร็วในการเลี้ยวขวาint Speed_left = Speed + (Speed/2);if(Speed_left > 255)Speed_left = 250;analogWrite(Speed_Motor_Left,Speed_left); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed_leftdigitalWrite(Motor_Left_forward, HIGH); // สั่งให้มอเตอร์ซ้ายหมุนไปหน้าdigitalWrite(Motor_Left_reward, LOW);analogWrite(Speed_Motor_Right,Speed-30); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Right_forward, HIGH); // สั่งให้มอเตอร์ขวาหมุนไปหน้าdigitalWrite(Motor_Right_reward, LOW);Serial.println("Turn_right"); // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังเลี้ยวขวา}void Reward_turn_left() // ฟังก์ชันสั่งให้หุ่นยนต์ถอยหลัง{int Speed_trun_left = Speed + (Speed/2);if(Speed_trun_left > 255)Speed_trun_left = 250;// สั่งให้มอเตอร์ซ้ายหมุนไปกลับหลังanalogWrite(Speed_Motor_Left,Speed-30); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Left_forward, LOW);digitalWrite(Motor_Left_reward, HIGH); // สั่งให้มอเตอร์ซ้ายหมุนกลับหลัง// สั่งให้มอเตอร์ขวาหมุนกลับหลังanalogWrite(Speed_Motor_Right,Speed_trun_left); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed_trun_leftdigitalWrite(Motor_Right_forward, LOW);digitalWrite(Motor_Right_reward, HIGH); // สั่งให้มอเตอร์ขวาหมุนกลับหลังSerial.println("Reward_turn_left"); // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังถอยหลัง}void Reward_turn_right() // ฟังก์ชันสั่งให้หุ่นยนต์ถอยหลัง{int Speed_trun_right = Speed + (Speed/2);if(Speed_trun_right > 255)Speed_trun_right = 250;// สั่งให้มอเตอร์ซ้ายหมุนไปกลับหลังanalogWrite(Speed_Motor_Left,Speed_trun_right); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed_trun_rightdigitalWrite(Motor_Left_forward, LOW);digitalWrite(Motor_Left_reward, HIGH); // สั่งให้มอเตอร์ซ้ายหมุนกลับหลัง// สั่งให้มอเตอร์ขวาหมุนกลับหลังanalogWrite(Speed_Motor_Right,Speed - 30); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Right_forward, LOW);digitalWrite(Motor_Right_reward, HIGH); // สั่งให้มอเตอร์ขวาหมุนกลับหลังSerial.println("Reward_turn_right"); // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังถอยหลัง}void Spin_left() // ฟังก์ชันสั่งให้หุนยนต์หมุนซ้าย{analogWrite(Speed_Motor_Left,Speed); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Left_forward, LOW);digitalWrite(Motor_Left_reward, HIGH); // สั่งให้มอเตอร์ซ้ายหมุนกลับหลังanalogWrite(Speed_Motor_Right,Speed); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Right_forward, HIGH); // สั่งให้มอเตอร์ขวาหมุนไปหน้าdigitalWrite(Motor_Right_reward, LOW);Serial.println("Spin_left"); // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังหมุนซ้าย}void Spin_right() // ฟังก์ชันสั่งให้หุนยนต์หมุนขวา{analogWrite(Speed_Motor_Left,Speed); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Left_forward, HIGH); // สั่งให้มอเตอร์ซ้ายหมุนไปหน้าdigitalWrite(Motor_Left_reward, LOW);analogWrite(Speed_Motor_Right,Speed); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร SpeeddigitalWrite(Motor_Right_forward, LOW);digitalWrite(Motor_Right_reward, HIGH); // สั่งให้มอเตอร์ขวาหมุนกลับหลังSerial.println("Spin_right"); // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังหมุนขวา}void Stop() // ฟังก์ชันสั่งให้หุนยนต์หยุดเดิน{analogWrite(Speed_Motor_Left,0); // กำหนดความเร็วมอเตอร์ = 0digitalWrite(Motor_Left_forward, LOW); // มอเตอร์หยุดหมุนdigitalWrite(Motor_Left_reward, LOW); // มอเตอร์หยุดหมุนanalogWrite(Speed_Motor_Right,0); // กำหนดความเร็วมอเตอร์ = 0digitalWrite(Motor_Right_forward, LOW); // มอเตอร์หยุดหมุนdigitalWrite(Motor_Right_reward, LOW); // มอเตอร์หยุดหมุน}
บทความจาก arduinothai.com
ไม่มีความคิดเห็น:
แสดงความคิดเห็น