วันอังคารที่ 18 กุมภาพันธ์ พ.ศ. 2563

โปรเจค Arduino เปิดปิดไฟด้วยเสียง (พร้อมโค้ดตัวอย่าง)

โปรเจค Arduino เปิดปิดไฟด้วยเสียง (พร้อมโค้ดตัวอย่าง)

ประกอบด้วย

Code ตัวอย่าง

#define signalToRelayPin 8 #define sensorPin 7
int lastSoundValue;
int soundValue;
long lastNoiseTime = 0;
long currentNoiseTime = 0;
long lastLightChange = 0;
int relayStatus = HIGH;
void setup() {
pinMode(sensorPin, INPUT);
pinMode(signalToRelayPin, OUTPUT);
digitalWrite(signalToRelayPin, HIGH);
}
void loop() {
soundValue = digitalRead(sensorPin);
currentNoiseTime = millis();
if (soundValue == 1) {
if (
(currentNoiseTime > lastNoiseTime + 200)&&
(lastSoundValue == 0)&&
(currentNoiseTime < lastNoiseTime + 800)&&
(currentNoiseTime > lastLightChange + 1000)
) {
relayStatus = !relayStatus;
digitalWrite(signalToRelayPin, relayStatus);
lastLightChange = currentNoiseTime;
}
lastNoiseTime = currentNoiseTime;
}
lastSoundValue = soundValue;
}


บทความจาก arduinothai.com

โปรเจค Arduino ควบคุมรถบังคับด้วย App Bluetooth RC Controller


ควบคุมรถบังคับด้วย App Bluetooth RC Controller วันนี้เรามาเตรียมอุปกรณ์กันก่อนนะครับ

อุปกรณ์มีดังนี้

การต่อวงจร


ตัวอย่างโปรแกรม

#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 ให้ทำงานแบบ Output
  pinMode(Motor_Left_reward, OUTPUT);     // กำหนด pin ให้ทำงานแบบ Output   
  pinMode(Speed_Motor_Left, OUTPUT);      // กำหนด pin ให้ทำงานแบบ Output 
  pinMode(Motor_Right_forward, OUTPUT);   // กำหนด pin ให้ทำงานแบบ Output
  pinMode(Motor_Right_reward, OUTPUT);    // กำหนด pin ให้ทำงานแบบ Output   
  pinMode(Speed_Motor_Right, OUTPUT);     // กำหนด pin ให้ทำงานแบบ Output   
  pinMode(Horn, OUTPUT);                  // กำหนด pin ให้ทำงานแบบ Output
}

// the loop routine runs over and over again forever:
void loop() 
{  
    // รับค่าจาก bluetooth
  if (BTSerial.available()>0) // ตรวจสอบว่า bluetooth มีการส่งค่ามาหรือไม่
  {
    char Control = BTSerial.read();    // อ่านค่าจาก bluetooth มาเก็บไว้ที่ ตัวแปร Control
   // Serial.println(Control);           // แสดงค่า จาก bluetooth (ตัวแปร Control) ทาง Serial Monitor
    
    if(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);    // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed 
  digitalWrite(Motor_Left_forward, HIGH); // สั่งให้มอเตอร์ซ้ายหมุนไปหน้า
  digitalWrite(Motor_Left_reward, LOW); 
  
  // สั่งให้มอเตอร์ขวาหมุนไปหน้า
  analogWrite(Speed_Motor_Right,Speed);    // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed 
  digitalWrite(Motor_Right_forward, HIGH); // สั่งให้มอเตอร์ขวาหมุนไปหน้า 
  digitalWrite(Motor_Right_reward, LOW);  
  Serial.println("Forward");               // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังเดินหน้า

}

void Reward() // ฟังก์ชันสั่งให้หุ่นยนต์ถอยหลัง
{
  // สั่งให้มอเตอร์ซ้ายหมุนไปกลับหลัง
  analogWrite(Speed_Motor_Left,Speed);     // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed
  digitalWrite(Motor_Left_forward, LOW);   
  digitalWrite(Motor_Left_reward, HIGH);   // สั่งให้มอเตอร์ซ้ายหมุนกลับหลัง
  
  // สั่งให้มอเตอร์ขวาหมุนกลับหลัง
  analogWrite(Speed_Motor_Right,Speed);    // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed
  digitalWrite(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);     // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed
  digitalWrite(Motor_Left_forward, HIGH);     // สั่งให้มอเตอร์ซ้ายหมุนไปหน้า
  digitalWrite(Motor_Left_reward, LOW);  
  
  analogWrite(Speed_Motor_Right,Speed_right); // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed_right
  digitalWrite(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_left
  digitalWrite(Motor_Left_forward, HIGH);    // สั่งให้มอเตอร์ซ้ายหมุนไปหน้า
  digitalWrite(Motor_Left_reward, LOW);  
  
  analogWrite(Speed_Motor_Right,Speed-30);   // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed
  digitalWrite(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);     // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed
  digitalWrite(Motor_Left_forward, LOW);   
  digitalWrite(Motor_Left_reward, HIGH);   // สั่งให้มอเตอร์ซ้ายหมุนกลับหลัง
  
  // สั่งให้มอเตอร์ขวาหมุนกลับหลัง
  analogWrite(Speed_Motor_Right,Speed_trun_left);    // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed_trun_left
  digitalWrite(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_right
  digitalWrite(Motor_Left_forward, LOW);   
  digitalWrite(Motor_Left_reward, HIGH);   // สั่งให้มอเตอร์ซ้ายหมุนกลับหลัง
  
  // สั่งให้มอเตอร์ขวาหมุนกลับหลัง
  analogWrite(Speed_Motor_Right,Speed - 30);    // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed
  digitalWrite(Motor_Right_forward, LOW);   
  digitalWrite(Motor_Right_reward, HIGH);  // สั่งให้มอเตอร์ขวาหมุนกลับหลัง
  Serial.println("Reward_turn_right");                // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังถอยหลัง

}

void Spin_left() // ฟังก์ชันสั่งให้หุนยนต์หมุนซ้าย
{
  analogWrite(Speed_Motor_Left,Speed);       // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed
  digitalWrite(Motor_Left_forward, LOW);   
  digitalWrite(Motor_Left_reward, HIGH);     // สั่งให้มอเตอร์ซ้ายหมุนกลับหลัง
  
  analogWrite(Speed_Motor_Right,Speed);      // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed
  digitalWrite(Motor_Right_forward, HIGH);   // สั่งให้มอเตอร์ขวาหมุนไปหน้า
  digitalWrite(Motor_Right_reward, LOW);  
  Serial.println("Spin_left");               // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังหมุนซ้าย
}
void Spin_right()  // ฟังก์ชันสั่งให้หุนยนต์หมุนขวา
{
  analogWrite(Speed_Motor_Left,Speed);       // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed
  digitalWrite(Motor_Left_forward, HIGH);    // สั่งให้มอเตอร์ซ้ายหมุนไปหน้า 
  digitalWrite(Motor_Left_reward, LOW);  
  
  analogWrite(Speed_Motor_Right,Speed);      // กำหนดความเร็วมอเตอร์ ด้วยตัวแปร Speed
  digitalWrite(Motor_Right_forward, LOW);   
  digitalWrite(Motor_Right_reward, HIGH);    // สั่งให้มอเตอร์ขวาหมุนกลับหลัง
  Serial.println("Spin_right");              // แสดงค่าที่ serial monitor ว่าหุ่นยนต์กำลังหมุนขวา
}

void Stop()  // ฟังก์ชันสั่งให้หุนยนต์หยุดเดิน
{
  analogWrite(Speed_Motor_Left,0);           // กำหนดความเร็วมอเตอร์ = 0
  digitalWrite(Motor_Left_forward, LOW);     // มอเตอร์หยุดหมุน
  digitalWrite(Motor_Left_reward, LOW);      // มอเตอร์หยุดหมุน
  
  analogWrite(Speed_Motor_Right,0);          // กำหนดความเร็วมอเตอร์ = 0
  digitalWrite(Motor_Right_forward, LOW);    // มอเตอร์หยุดหมุน
  digitalWrite(Motor_Right_reward, LOW);     // มอเตอร์หยุดหมุน
}

บทความจาก arduinothai.com



วิธีควบคุมทิศทาง DC Motor ด้วย Relay 2 Chanel

วิธีควบคุมทิศทางดีซี มอเตอร์ (DC Motor) ให้หมุนไป-กลับ (ตามเข็มนาฬิกา-ทวนเข็มนาฬิกา) ด้วย Relay 2 Chanel โดยวิธีการดังกล่าวนี้จะไม่สามารถควบคุมความเร็วของมอเตอร์ได้จะควบคุมได้เฉพาะทิศทางเท่านั้น หากต้องการที่จะควบคุมความเร็วจะต้องใช้บอร์ดอีกประเภทครับ

อุปกรณ์มีดังนี้



การต่อวงจร
  • Arduino Uno >- Relay
  • 5 V >- VCC
  • GND >- GND
  • D3 >- IN1
  • D4 >- IN2
Code ตัวอย่าง

// Example code by www.arduinothai.com

// กำหนด pin ที่จะใช้งาน
#define CLOCKWISE 3
#define COUTERCLOCKWISE 4

void setup() 
{
  pinMode(CLOCKWISE, OUTPUT); // กำหนด pin ที่จะใช้งานให้ทำงานเป็น Output
  pinMode(COUTERCLOCKWISE, OUTPUT);
}

void loop() 
{
  digitalWrite(CLOCKWISE, LOW); 
  digitalWrite(COUTERCLOCKWISE, HIGH);   
  delay(3000);              // รอ 3 วินาที
  digitalWrite(CLOCKWISE, HIGH); 
  digitalWrite(COUTERCLOCKWISE, LOW);     
  delay(3000);              // รอ 3 วินาที
}
คำอธิบาย
จาก Code ตัวอย่างจะเห็นว่า ขา PIN ที่ใช้งานคือ ขา  3 และ 4 ตามที่ได้ตั้งไว้ การต่อใช้ งานคือ ขา 3 และ 4 ของบอร์ด Arduino UNO จะต่อเข้ากับขา IN1 และ IN2 ของบอร์ด Relay ส่วนขา GND และ VCC ที่บอร์ด Relay ให้ต่อเข้ากับขา GND และ 5V ของบอร์ด Arduino UNO

บทความจาก arduinothai.com
Mini Project Arduino Nodemcu แจ้งเตือน น้ำท่วม น้ำล้น ผ่าน Line Notify

โปรเจคนี้พัฒนาโปรแกรมด้วย Arduino IDE เป็นโปรเจคที่แนะนำเกี่ยวกับการใช้ Sensor Water Level และการแจ้งเตือนผ่าน Line เหมาะสำหรับน้องๆที่ศึกษาเกี่ยวกับ Nodemcu การแจ้งเตือนผ่าน Line เป็นเบสิก พื้นฐานสำหรับน้องๆเลยครับ.

ก่อนอื่นน้องๆจะต้องตั้งค่า ID Line ของน้องๆก่อนเลยครับ ลองอ่าน Link นี้ได้เลย

https://www.ab.in.th/b/6

อุปกรณ์มีดังต่อไปนี้ ราคาไม่เกิน 400 บาท 




การต่อสายวงจร


ตัวอย่าง Code 

/*
ติดตั้ง Library Line Notify ด้วยน่ะครับ
https://github.com/TridentTD/TridentTD_LineNotify
*/
#include <TridentTD_LineNotify.h>
#define SSID "9arduino" //SSID Wifi
#define PASSWORD "tv357911itv" //Pass Wifi
#define LINE_TOKEN "--------------------" //Token ที่ได้จากการสมัคร ID Line
int waterlevel = 300; //ปรับระดับน้ำ
void setup()
{
Serial.begin(9600);
Serial.println();
Serial.println(LINE.getVersion());
WiFi.begin(SSID, PASSWORD);
Serial.printf("WiFi connecting to %s\n", SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(400);
}
Serial.printf("\nWiFi connected\nIP : ");
Serial.println(WiFi.localIP());
// กำหนด Line Token
LINE.setToken(LINE_TOKEN);
}
void loop()
{
int val = analogRead(A1); // read input value
Serial.print("Water Level : ");
Serial.println(val);
delay(2000);
if (val >= waterlevel) {
Serial.println("Send Line");
LINE.notify("น้ำล้นแล้วจ่า !!");
while (val >= 200) delay(10);
} else {
}
}

หลักการทำงาน 

Sensor Water Level เป็น Sensor แบบ Analog จะป้อนสัญญาณไปยัง Nodemcu เพื่อให้ Nodemcu ตัดสินใจ โดยเราจะปรับค่าระดับน้ำ ที่ ตัวแปร 

int waterlevel = 300;   //ปรับระดับน้ำ

ปรับได้ตามที่เราต้องการเลยครับให้เหมาะสมกับหน้างานน้องๆ เมื่อเข้าเงือนไขการทำงาน Nodemcu ก็จะทำการส่งข้อมูลไปยัง Server Line Notify จากนั้น ก็จะมีข้อความแจ้งเตือนมายัง ID Line ของน้องๆครับ 

บทความจาก ab.in.th