วันศุกร์ที่ 3 พฤศจิกายน พ.ศ. 2560

Code โปรเจค หุ่นยนต์ควบคุมด้วยเสียงพูด Arduino + Speech Module


โปรเจค หุ่นยนต์ควบคุมด้วยเสียงพูด Arduino หุ่นยนต์ควบคุมด้วยเสียง หุ่นยนต์สั่งการด้วยเสียง หุ่นยนต์สั่งการด้วยเสียงภาษาไทย ด้วย Voice Recognition Module

โปรเจค หุ่นยนต์ควบคุมด้วยเสียงพูด Arduino + Speech Recognition Module

อุปกรณ์ที่ต้องใช้ก็คือ

     1. 4WD Smart Robot Car Chassis Kits

     2. Arduino UNO R3 - Made in italy

     3. Arduino Sensor Shield V5.0

     4. Motor Drive Module L298N

     5. สาย Jumper Female to Male ยาว 20cm.

     6. สาย Jumper Female to Female ยาว 30cm.

     7. รางถ่านแบบ 18650 ใส่ถ่าน 2 ก้อน

     8. แบตเตอรี่ลิเธียม 18650 จำนวน 2 ก้อน

     9. เสารองแผ่นพีซีบีโลหะแบบเหลี่ยม 8 mm

     10. สกรูหัวกลมน็อตตัวเมีย ขนาด 3มม ยาว12มม. 10 ตัว

     11. เพาเวอร์สวิตซ์สำหรับเปิดปิด (ON / OFF Rocker Switch)
   
     12. โมดูลควบคุมด้วยเสียง Speech Recognition Module

     13. CP2102 USB 2.0 to UART TTL 5PIN Connector Module

     14. สายไฟแดงดำ ขนาด 22AWG


เริ่มด้วย ต่อสายไฟสำหรับมอเตอร์ ทั้ง 4 ตัว ที่ 4WD smart car chassis ให้มา โดยให้ ขั้วลบ สายไฟสีดำ อยู่ด้านบน เหมือนกันทั้ง 4 ตัว


ประกอบเข้ากับ โครง ชิ้นที่ 1 ของ 4WD smart car chassis ดังรูป



ประกอบ น็อต 6 จุด สำหรับ วาง โครง ชิ้นที่ 2


ประกอบ บอร์ด Motor Drive Module L298N ลงที่ โครง ชิ้นที่ 1


เชื่อมสายไฟ ของ มอเตอร์ ด้านบน สายสีดำ ขั้ว -  ของมอเตอร์ ตัวที่1 เข้า กับมอเตอร์ ตัวที่2 และ สายสีแดง ขั้ว+  ของมอเตอร์ ตัวที่1 เข้า กับมอเตอร์ ตัวที่2 และ เช่นกันทำที่ มอเตอร์ คู่ด้านล่างด้วย เพื่อให้เหมือนมีมอเตอร์ จาก 4 ตัว เหลือ 2 ตัว

จากนั้น ประกอบสายไฟ ของมอเตอร์ ทั้ง 2 ตัว เข้ากับ บอร์ด L298N ดังรูป



ประกอบ โครงชิ้นที่ 2 เข้าที่ ด้านบนของ โครงชิ้นที่ 1 และ ประกอบ บอร์ด Arduino UNO R3 ลงที่โครงชิ้นที่ 2 ซึ่งอยู่ชั้นบน


ประกอบ บอร์ด Arduino Sensor Shield V5.0 ลงที่ บอร์ด Arduino UNO R3



ประกอบ รางถ่านแบบ 18650 และ เชื่อมต่อ วงจรตามรูป



หมายเหตุ : ถ้ามี Jumper อยู่ที่ขา ENA และ ENB ของ บอร์ด L298N  ให้ถอดออก




การเชื่อมต่อระหว่าง บอร์ด Arduino Sensor Shield V5.0 กับ บอร์ด L298N  


*** VCC ของ Arduino Sensor Shield V5.0  คือ V ***



หลังจากนั้นให้ทดสอบเบื้องต้น ว่าการหมุนของล้อถูกต้องหรือไม่
โดย

เปิดโปรแกรม Arduino (IDE) และ Upload โค้ดนี้ ไปยัง บอร์ด Arduino UNO R3


// Motor A pins (enableA = enable motor, pinA1 = forward, pinA2 = backward)
int enableA = 3;
int pinA1 = 6;
int pinA2 = 7;

//Motor B pins (enabledB = enable motor, pinB2 = forward, pinB2 = backward)
int enableB = 5;
int pinB1 = 8;
int pinB2 = 9;

//This lets you run the loop a single time for testing
boolean run = true;

void setup() {
 pinMode(enableA, OUTPUT);
 pinMode(pinA1, OUTPUT);
 pinMode(pinA2, OUTPUT);

 pinMode(enableB, OUTPUT);
 pinMode(pinB1, OUTPUT);
 pinMode(pinB2, OUTPUT);
}
void loop() {
  if (run) {
    delay(2000);
    enableMotors();
    //Go forward
    forward(600);
    //Go backward
    backward(600);
    //Turn left
    turnLeft(600);
    coast(200);
    //Turn right
    turnRight(600);
    coast(200);
    //This stops the loop
    run = false; 
  }
}

//Define high-level H-bridge commands

void enableMotors()
{
 motorAOn();
 motorBOn();
}

void disableMotors()
{
 motorAOff();
 motorBOff();
}

void forward(int time)
{
 motorAForward();
 motorBForward();
 delay(time);
}

void backward(int time)
{
 motorABackward();
 motorBBackward();
 delay(time);
}

void turnLeft(int time)
{
 motorABackward();
 motorBForward();
 delay(time);
}

void turnRight(int time)
{
 motorAForward();
 motorBBackward();
 delay(time);
}

void coast(int time)
{
 motorACoast();
 motorBCoast();
 delay(time);
}

void brake(int time)
{
 motorABrake();
 motorBBrake();
 delay(time);
}
//Define low-level H-bridge commands

//enable motors
void motorAOn()
{
 digitalWrite(enableA, HIGH);
}

void motorBOn()
{
 digitalWrite(enableB, HIGH);
}

 //disable motors
void motorAOff()
{
 digitalWrite(enableB, LOW);
}

void motorBOff()
{
 digitalWrite(enableA, LOW);
}

 //motor A controls
void motorAForward()
{
 digitalWrite(pinA1, HIGH);
 digitalWrite(pinA2, LOW);
}

void motorABackward()
{
 digitalWrite(pinA1, LOW);
 digitalWrite(pinA2, HIGH);
}

//motor B controls
void motorBForward()
{
 digitalWrite(pinB1, HIGH);
 digitalWrite(pinB2, LOW);
}

void motorBBackward()
{
 digitalWrite(pinB1, LOW);
 digitalWrite(pinB2, HIGH);
}

//coasting and braking
void motorACoast()
{
 digitalWrite(pinA1, LOW);
 digitalWrite(pinA2, LOW);
}

void motorABrake()
{
 digitalWrite(pinA1, HIGH);
 digitalWrite(pinA2, HIGH);
}

void motorBCoast()
{
 digitalWrite(pinB1, LOW);
 digitalWrite(pinB2, LOW);
}

void motorBBrake()
{
 digitalWrite(pinB1, HIGH);
 digitalWrite(pinB2, HIGH);

}


ถอดสาย USB ระหว่าง Arduino กับ คอมพิวเตอร์ออก และ หาอุปกรณ์ที่สามารถยกตัวรถ 4WD smart car chassis ขึ้นแล้ว ล้อไม่แตะพื้น เพือทดสอบการหมุนของล้อว่าถูกต้องหรือไม่

ใส่ แบตเตอรี่ลิเธียม 18650 จำนวน 2 ก้อน ไปที่ รางถ่าน และ ตรวจสอบขั้วของแบตเตอรี่ ใส่ถุกต้องหรือไม่



โปรแกรมนี้จะทำงานเพียง 1 ครั้ง ถ้าต้องการทดลองใหม่ให้ถอด แบตเตอรี่ออก (หรือ ปิดเปิด สวิทช์ไฟใหม่) แล้วใส่เข้าไปใหม่ เมื่อล้อหมุน ตรวจสอบการหมุน ขอล้อต่างๆถูกต้องหรือไม่ ถ้าต่อวงจรถูกต้อง ล้อ ทั้งสองข้างจะหมุนไปข้างหน้า 1ครั้ง กลับหลัง 1 ครั้ง และ เดินหน้าอีกหนึ่งครั้งแล้วจึงหยุด ถ้าไม่ถูกต้องให้แก้ไข เช่นการต่อขั้วของมอเตอร์ผิด เป็นต้น

ถ้าทุกอย่างถูกต้อง ทดลอง ยกลงวางที่พื้นแล้วทดสอบ อีกครั้ง ถอด แบตเตอรี่ออก (หรือ ปิดเปิด สวิทช์ไฟใหม่) แล้วใส่เข้าไปใหม่ ถ้าทุกอย่างถูกต้อง รถจะเคลื่อนที่ไปข้างหน้า-ถอยหลัง แล้ว เลี้ยวซ้าย แล้ว เลี้ยวขวา กลับสู่ตำแหน่งเดิม


เตรียมความพร้อม โมดูลควบคุมด้วยเสียง Speech Recognition Module

ตามขั้นตอน ลิงค์ด้านล่าง


การใช้งาน โมดูลควบคุมด้วยเสียง Speech Recognition Module

https://robotsiam.blogspot.com/2017/10/speech-recognition-module.html



การเชื่อมต่อระหว่าง บอร์ด Speech Recognition Module  กับ  Arduino Sensor Shield 



Speech <-> Shield

GND <-> G


VCC <-> V

TXD <->  0 (RX)

RXD <->  1 (TX)






การอัปโหลดซอร์สโค้ด หุ่นยนต์ควบคุมด้วยเสียงพูด Arduino

อย่าลืมถอดสาย TX และ RX ออกจาก Speech Module เมื่อต้องการจะอัพโหลดซอร์สโค้ด หลังจากอัปโหลดโค้ดแล้วให้เชื่อมต่อ TX และ RX เข้ากับบอร์ดดังเดิม

/*
   by : RobotSiam.com
*/

// Motor A
int enableA = 3;
int pinA1 = 6;
int pinA2 = 7;

//Motor B
int enableB = 5;
int pinB1 = 8;
int pinB2 = 9;

byte com = 0;

void setup() {

  pinMode(enableA, OUTPUT);
  pinMode(pinA1, OUTPUT);
  pinMode(pinA2, OUTPUT);

  pinMode(enableB, OUTPUT);
  pinMode(pinB1, OUTPUT);
  pinMode(pinB2, OUTPUT);

  Serial.begin(9600);

  Serial.write(0xAA);

  Serial.write(0x37);

  delay(1000);

  Serial.write(0xAA);

  Serial.write(0x21);

}
void loop() {

  //Run the motors at slightly less than full power

  enableMotors();
  analogWrite(enableA, 225);  // ปรับความเร็วหุ่นยนต์
  analogWrite(enableB, 225); // ปรับความเร็วหุ่นยนต์

  while (Serial.available()) {

    com = Serial.read();

    switch (com) {

      case 0x11:   //forward command

        forward(600);
        coast(10);
        break;

      case 0x12:  // backward command

        backward(600);
        coast(10);
        break;

      case 0x13:  //turnLeft command

        turnLeft(1000);
        coast(10);
        break;

      case 0x14:  // turnRight command

        turnRight(1000);
        coast(10);
        break;

      case 0x15:  //stop command

        coast(2000);

        break;

    }
  }

}

//Define high-level H-bridge commands

void enableMotors()
{
  motorAOn();
  motorBOn();
}

void disableMotors()
{
  motorAOff();
  motorBOff();
}

void forward(int time)
{
  motorAForward();
  motorBForward();
  delay(time);
}

void backward(int time)
{
  motorABackward();
  motorBBackward();
  delay(time);
}

void turnLeft(int time)
{
  motorABackward();
  motorBForward();
  delay(time);
}

void turnRight(int time)
{
  motorAForward();
  motorBBackward();
  delay(time);
}

void coast(int time)
{
  motorACoast();
  motorBCoast();
  delay(time);
}

void brake(int time)
{
  motorABrake();
  motorBBrake();
  delay(time);
}
//Define low-level H-bridge commands

//enable motors
void motorAOn()
{
  digitalWrite(enableA, HIGH);
}

void motorBOn()
{
  digitalWrite(enableB, HIGH);
}

//disable motors
void motorAOff()
{
  digitalWrite(enableB, LOW);
}

void motorBOff()
{
  digitalWrite(enableA, LOW);
}

//motor A controls
void motorAForward()
{
  digitalWrite(pinA1, HIGH);
  digitalWrite(pinA2, LOW);
}

void motorABackward()
{
  digitalWrite(pinA1, LOW);
  digitalWrite(pinA2, HIGH);
}

//motor B controls
void motorBForward()
{
  digitalWrite(pinB1, HIGH);
  digitalWrite(pinB2, LOW);
}

void motorBBackward()
{
  digitalWrite(pinB1, LOW);
  digitalWrite(pinB2, HIGH);
}

//coasting and braking
void motorACoast()
{
  digitalWrite(pinA1, LOW);
  digitalWrite(pinA2, LOW);
}

void motorABrake()
{
  digitalWrite(pinA1, HIGH);
  digitalWrite(pinA2, HIGH);
}

void motorBCoast()
{
  digitalWrite(pinB1, LOW);
  digitalWrite(pinB2, LOW);
}

void motorBBrake()
{
  digitalWrite(pinB1, HIGH);
  digitalWrite(pinB2, HIGH);
}


วีดีโอผลลัพธ์การทำงานของ โปรเจค หุ่นยนต์ควบคุมด้วยเสียงพูด Arduino


.

วันพุธที่ 9 สิงหาคม พ.ศ. 2560

Code โปรเจค Ethernet + Blynk ปิด เปิด ไฟ LED ผ่าน อินเตอร์เน็ต




โปรเจค นี้จะเป็นการนำ Ethernet Shield W5100 ควบคุมการแสดงผล ปิดเปิด ไฟ LED ผ่าน ทุกที่ ที่สามารถเชื่อมต่ออินเตอร์เน็ตได้ ด้วยแอพ Blynk ที่สามารถเชื่อมต่ออุปกรณ์ Device ของเราเข้ากับ internet ได้อย่างง่ายดาย

จาก โปรเจค Arduino UNO + Blynk ปิด เปิด ไฟ LED ผ่าน อินเตอร์เน็ต มีข้อเสียคือ ในการทำงานต้องเปิด คอมพิวเตอร์ หรือ PC ของเราตลอดเวลา เราจะแก้ปัญหานี้ด้วยการใช้ Ethernet Shield W5100 เพิ่มเข้าไป  ก็จะสามารถเชื่อมต่อ Arduino กับโลกกว้างภายนอกผ่านทาง Internet ได้แล้ว

การเชื่อมต่อ ระหว่าง Arduino + Ethernet Shield กับ อินเตอร์เน็ต






IoT หรือ Internet of Things หรือเทคโนโลยีของ อุปกรณ์และเครื่องใช้ต่างๆ ที่จะทำงานโดยมีการเชื่อมโยงถึงกันผ่านทางเครือข่ายอินเทอร์เน็ต กำลังเป็นเทคโนโลยีที่ถูกพูดถึงมากขึ้นเรื่อยๆ เราได้เห็นทั้งฮาร์ดแวร์รุ่นใหม่ๆ มีการประกาศมาตรฐาน มีทั้งบริการและเครื่องมือชนิดใหม่ ออกมาให้ได้ติดตามกันอย่างต่อเนื่อง  และ ก็มีบริการที่น่าสนใจเป็นอย่างมาก โดยเจ้าบริการที่ว่านี้มีชื่อว่า Blynk



 Blynk Application เป็นโปรแกรมบนมือถือที่ทำให้เราสร้างหน้าต่างควบคุมหรือแสดงผลเชื่อมต่อกับพวกไมโครคอนโทรเลอร์(Ardunio, ESP8266, Raspberry Pi) ได้ง่ายๆ และยังสามารถควบคุมผ่าน ทุกที่ ที่สามารถเชื่อมต่ออินเตอร์เน็ต  ได้อีกด้วย



อุปกรณ์ที่ใช้


1. Arduino UNO R3 - Made in italy

2. สาย Jumper Female to Male ยาว 20cm.

3. สาย Jumper Male to Male ยาว 20cm.

4. Prototype PCB Board 4x6 cm Double Sides

5. แผ่นอะคริลิคใส ขนาด 15 x 30 เซนติเมตร

6. สกรูหัวกลม+น็อตตัวเมีย ขนาด 3มม ยาว 12มม

7. Relay 1 Channel DC 5V Module

8. SMD LED Lighting G4 AC DC 12V

9. รางถ่าน AA 8 ก้อน

10. Ethernet Shield W5100

11. Adapter DC 5V 2A Power Supply



ประกอบ Ethernet Shield เข้ากับ Arduino UNO R3

ประกอบ Ethernet Shield W5100  ลงไป บน Arduino UNO ตามรูป


การต่อวงจร ระหว่าง Relay กับ 
Ethernet Shield






Relay
 <--> Ethernet

5V <--> 5V
GND <--> GND
IN <--> D7



การต่อวงจร ระหว่าง Ethernet + Relay + LED + รางถ่าน




ไปที่ Play Store ค้นหา Blynk

ติดตั้ง เหมือน App ทั่วๆไป



เปิด Blynk Application ขึ้นมา

-> Create New Account






กรอก อีเมล และ พาสเวิด -> Sign Up




-> New Project




เลือก DEVICE



ตั้งค่า ดังนี้

your hardware เป็น Arduino UNO



connection type เป็น Ethernet
ตั้งชื่อ Project ในตัวอย่างชื่อ W5100 แล้วจึงคลิก Create




โปรกรม จะส่ง Auth Toke ไปที่ อีเมล ที่เรากรอกไว้ -> OK


โดยทุกๆครั้งที่เริ่มสร้างโปรเจคใหม่ AUTH TOKEN จะถูกเปลี่ยนใหม่เสมอ ซึ่ง KEY นี้เองที่เป็นเสมือนกุญแจสำหรับเชื่อมต่อ


คลิก เครื่องหมาย +



คลิก เพิ่ม Button


จะมี ปุ่ม BUTTON เพิ่มเข้ามาที่หน้าจอ แล้วจึง คลิกที่ BUTTON เพื่อตั้งค่า



เลือก เป็นแบบ SWITCH แล้ว คลิก ที่ PIN


Select pin -> Digital -> D7 -> CONTINUE


BUTTON จะอยู่ริมซ้ายมือบน


เพื่อความสวยงาม ให้ ลาก BUTTON ไว้กลางหน้าจอ




ไปที่

http://www.blynk.cc/getting-started/

ดาวน์โหลด ไลบรารี่ Blynk





เปิด โปรแกรม 
ARDUINO IDE จากนั้นให้ทำการเพิ่ม ไลบรารี่ Blynk  ให้กับ Arduino (IDE) ของเรา

เมื่อเพิ่มเสร็จแล้วไปที่ File -> Eamples -> Blynk -> Boards_Ethernet -> Arduino_Ethernet



แก้ไข YourAuthToken ที่เราได้รับจาก อีเมล



ที่เราได้รับจาก อีเมล





เมื่อแก้ไขแล้ว อัพโหลดโค้ด ไปยัง  Arduino UNO




/*************************************************************

  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Social networks:            http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example shows how to use Arduino Ethernet shield (W5100)
  to connect your project to Blynk.

  NOTE: Pins 10, 11, 12 and 13 are reserved for Ethernet module.
        DON'T use them in your sketch directly!

  WARNING: If you have an SD card, you may need to disable it
        by setting pin 4 to HIGH. Read more here:
        https://www.arduino.cc/en/Main/ArduinoEthernetShield

  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "b8152a62d1c54b20862ab77ae4b23345";  // ต้องแก้ไข

#define W5100_CS  10
#define SDCARD_CS 4

void setup()
{
  // Debug console
  Serial.begin(9600);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card

  Blynk.begin(auth);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 8442);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8442);
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example
}

void loop()
{
  Blynk.run();
}



ถอดสาย USB ออก แล้วใช้สายแลน RJ45 เชื่อมต่อ ระหว่าง Ethernet Shield  กับ Router เพื่อที่จะทำให้สามารถติดต่อกับระบบเครือข่ายได้ และ  ป้อนไฟ จาก Adapter 5V เข้า Arduino UNO


กลับไปที่ Blynk Application คลิกที่ เครื่องหมาย สามเหลี่ยม


เพื่อทดสอบการทำงานของโปรแกรม W5100






วีดีโอผลลัพธ์การทำงานของ โปรเจค Ethernet + Blynk ปิด เปิด ไฟ LED ผ่าน อินเตอร์เน็ต
     

Code โปรเจค Arduino เปิดปิดไฟ ด้วย Wireless Joystick

PS2 Joystick Playstation Adapter for Arduino อะแดปเตอร์แปลงหัว PS2 เป็นขาต่อแบบ DIP สำหรับ Arduino คอนเนคเตอร์สำหรับแปลงจาก PS2 เป็นขา DI...