วันศุกร์ที่ 8 กุมภาพันธ์ พ.ศ. 2562

ส่วนประกอบงานวิชา เขียนโปรแกรม ตัวอย่างงานที่ 12

ส่วนประกอบงานวิชา  เขียนโปรแกรม ตัวอย่างงานที่ 12

ใบรายงานผลการปฏิบัติงาน

โปรเจคหุ่นยนต์ Arduino UNO + L298P บังคับด้วย Bluetooth




โปรเจค นี้จะเป็นการนำ Arduino UNO R3 และ L298P Motor Shield Board มาทำเป็น หุ่นยนต์ บังคับด้วย สมาร์ทโฟน แอนดรอยด์ ผ่านทาง Bluetooth HC-06 ซึ่งการใช้ 
L298P Motor Shield จะทำให้การประกอบหุ่นยนต์ สะดวกและง่ายขึ้น เมื่อเทียบกับการใช้ Motor Driver Module L298N


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


1. ประกอบหุ่นยนต์



เริ่มต้นด้วยการ ประกอบ หุ่นยนต์ UNO + L298P และ ทดสอบการเคลื่อนที่ของหุ่นยนต์ ตามลิงค์ บทความด้านล่าง

https://robotsiam.blogspot.com/2017/12/uno-l298p.html

 

 

2. ประกอบ Bluetooth HC-06



ใช้ จั้มเปอร์ ผู้-เมีย เชื่อมต่อ Bluetooth HC-06 เข้ากับ L298P ตามรูปด้านล่าง

HC-06 <-> L298P

VCC 
<-> 5V
GND 
<-> Gnd
TXD 
<-> RX(0)
RXD 
<-> TX(1)





3. การอัพโหลดโปรแกรม
/*

    Bluetooth Robot with Arduino UNO + L298P
    For more details visit:
    https://robotsiam.blogspot.com/2017/12/uno-l298p-bluetooth.html

*/

#include <SoftwareSerial.h>

int incomingByte = 0;

/*-------definning Outputs------*/

int MA1 = 12;     // Motor A1
int MA2 =  3;     // Motor A2
int PWM_A =  10;   // Speed Motor A

int MB1 =  13;     // Motor B1
int MB2 =  8;     // Motor B2
int PWM_B =  11;  // Speed Motor B

int SPEED = 200;  // Speed PWM สามารถปรับความเร็วได้ถึง 0 - 255

void setup() {

  //Setup Channel A
  pinMode(12, OUTPUT); //Motor A1
  pinMode(3, OUTPUT); //Motor A2
  pinMode(10, OUTPUT); //Speed PWM Motor A

  //Setup Channel B
  pinMode(13, OUTPUT);  //Motor B1
  pinMode(8, OUTPUT);  //Motor B2
  pinMode(11, OUTPUT); //Speed PWM Motor B

  Serial.begin(9600);
  Serial.println("Motor Ready");

}

void loop() {



  if (Serial.available() > 0) {
    incomingByte = Serial.read();
  }

  switch (incomingByte)
  {
    case 'S':
      // stop all motors
      {
        Stop(1);
        Serial.println("Stop\n");
        incomingByte = '*';
      }

      break;

    case 'F':
      // turn it on going forward
      {
        Forward(1);
        Serial.println("Forward\n");
        incomingByte = '*';
      }
      break;

    case 'B':
      // turn it on going backward
      {
        Backward(1);
        Serial.println("Backward\n");
        incomingByte = '*';
      }
      break;

    case 'R':
      // turn right
      {
        turnRight(1);
        Serial.println("Rotate Right\n");
        incomingByte = '*';
      }
      break;


    case 'L':
      // turn left
      {
        turnLeft(1);
        Serial.println("Rotate Left\n");
        incomingByte = '*';
      }
      break;

  }
}
void Forward(int time)
{
  digitalWrite(MA1, LOW);
  digitalWrite(MA2, HIGH);
  analogWrite(PWM_A, SPEED);

  digitalWrite(MB1, HIGH);
  digitalWrite(MB2, LOW);
  analogWrite(PWM_B, SPEED);

  delay(time);
}

void Backward(int time)
{
  digitalWrite(MA1, HIGH);
  digitalWrite(MA2, LOW);
  analogWrite(PWM_A, SPEED);

  digitalWrite(MB1, LOW);
  digitalWrite(MB2, HIGH);
  analogWrite(PWM_B, SPEED);

  delay(time);
}

void turnLeft(int time)
{
  digitalWrite(MA1, HIGH);
  digitalWrite(MA2, LOW);
  analogWrite(PWM_A, SPEED);

  digitalWrite(MB1, LOW);
  digitalWrite(MB2, LOW);
  analogWrite(PWM_B, 0);

  delay(time);
}

void turnRight(int time)
{
  digitalWrite(MA1, LOW);
  digitalWrite(MA2, LOW);
  analogWrite(PWM_A, 0);

  digitalWrite(MB1, LOW);
  digitalWrite(MB2, HIGH);
  analogWrite(PWM_B, SPEED);

  delay(time);
}

void Stop(int time)
{
  digitalWrite(MA1, LOW);
  digitalWrite(MA2, LOW);
  analogWrite(PWM_A, 0);

  digitalWrite(MB1, LOW);
  digitalWrite(MB2, LOW);
  analogWrite(PWM_B, 0);

  delay(time);

ส่วนประกอบงานวิชา เขียนโปรแกรม ตัวอย่างงานที่ 11

ส่วนประกอบงานวิชา  เขียนโปรแกรม ตัวอย่างงานที่ 11

ใบรายงานผลการปฏิบัติงาน

Hardware


Picture of ShotBot - Arduino Powered Pump Project


Sorfware



//define the input/output pins
//pump/relay pins
#define PUMP_1_PIN 7
#define PUMP_2_PIN 8

//pushbutton pins
#define BUTTON_1_PIN 2
#define BUTTON_2_PIN 4

//Time for pumping stations to turn on in milliseconds
#define PUMP_1_TIME 2500
#define PUMP_2_TIME 2500

//setup() runs once
void setup()
{
  //setup output pins for relays/pumping stations
  pinMode(PUMP_1_PIN, OUTPUT);
  pinMode(PUMP_2_PIN, OUTPUT);
  
  //setup input pins for buttons
  pinMode(BUTTON_1_PIN, INPUT);
  pinMode(BUTTON_2_PIN, INPUT);  
}

//loop() runs indefinitely
void loop()
{
  //check pushbutton on pin BUTTON_1_PIN to see if it is HIGH (it has been pressed)
  if(digitalRead(BUTTON_1_PIN) == HIGH)
  {
     digitalWrite(PUMP_1_PIN, HIGH); //turn pump 1 on
     delay(PUMP_1_TIME);             //wait PUMP_1_TIME milliseconds
     digitalWrite(PUMP_1_PIN, LOW);  //turn pump 1 off
  }
  
  //check pushbutton on pin BUTTON_2_PIN to see if it is HIGH (it has been pressed)
  if(digitalRead(BUTTON_2_PIN) == HIGH)
  {
     digitalWrite(PUMP_2_PIN, HIGH); //turn pump 2 on
     delay(PUMP_2_TIME);             //wait PUMP_2_TIME milliseconds
     digitalWrite(PUMP_2_PIN, LOW);  //turn pump 2 off
  }
}

ส่วนประกอบงานวิชา เขียนโปรแกรม ตัวอย่างงานที่ 10

ส่วนประกอบงานวิชา  เขียนโปรแกรม ตัวอย่างงานที่ 10

ใบรายงานผลการปฏิบัติงาน

Hardware




Sorfware



#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD to address 0x27, 16 chars, and 2 lines.
LiquidCrystal_I2C lcd(0x27,16,2);

void setup()
{
  lcd.init();
  lcd.backlight();
}

void loop()
{
  char str[16];
  double degC, degF;

  // Read the LM35 sensor on A3.
  int adu = analogRead(A3);

  // Calculate the temperature.
  degC = adu * 0.488;
  degF = (adu * 0.8784) + 32;

  // Turn it into a string.
  memset(str, 0, sizeof(str));
  dtostrf(degC,1,2,str);
  strcat(str, " C ");
  dtostrf(degF,1,2,str+(strlen(str)));
  strcat(str, " F");

  // Display the header and the string.
  lcd.setCursor(0,0);
  lcd.print("Temperature");
  lcd.setCursor(0,1);
  lcd.print(str);

  delay(1000);
}
 

ส่วนประกอบงานวิชา เขียนโปรแกรม ตัวอย่างงานที่ 9

ส่วนประกอบงานวิชา  เขียนโปรแกรม ตัวอย่างงานที่ 9

ใบรายงานผลการปฏิบัติงาน
photo transistor

Code Arduino

const byte Input = A1;
const byte LED = 5;

void setup(){
  pinMode(Input,INPUT);
  pinMode(LED,OUTPUT);
  digitalWrite(LED,LOW);
  analogReference(DEFAULT);
  Serial.begin(9600);
}

void loop(){
  float distance = analogRead(Input);  
  Serial.println(distance);
  if(distance > 170){ //have objects Value between 170-255
      distance = (1/distance)*100000; //Scale
      digitalWrite(LED, HIGH); //LED on 
      delay(distance); //delay
      digitalWrite(LED, LOW); //LED off
      delay(distance); //delay
  }
  else //value 0-170
   digitalWrite(LED,LOW);
}

       รูปถ่ายการทดลองประกอบตัวอย่าง



ตัวอย่างการออกแบบวงจร


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

การทดลองโดยนำกระดาษมาบังแสงตรงโฟโต้ทรานซิสเตอร์

ส่วนประกอบงานวิชา เขียนโปรแกรม ตัวอย่างงานที่ 8

ส่วนประกอบงานวิชา  เขียนโปรแกรม ตัวอย่างงานที่ 8

ใบรายงานผลการปฏิบัติงาน
LDR Sensor เซนเซอร์วัดแสง


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

1. บอร์ด Arduino Uno R3 
2. Breadboard 
3. LDR Sensor เซนเซอร์วัดแสง

Code

int LDR_Sensor_Pin = A0;  
 
void setup() {
  Serial.begin(9600);  
  
} 
void loop() {
  int sensor_Value = analogRead(LDR_Sensor_Pin);  
  Serial.print("LDR Sensor Value = ");  
  Serial.println(sensor_Value);  
  delay(500); 
}

ส่วนประกอบงานวิชา เขียนโปรแกรม ตัวอย่างงานที่ 7

ส่วนประกอบงานวิชา  เขียนโปรแกรม ตัวอย่างงานที่ 7

ใบรายงานผลการปฏิบัติงาน

ARDUINO BALANCING ROBOT








อุปกรณ์

- (1) Arduino Uno Board
- (1) 9V 1250 mA wall-wart
- (2) 12V DC motors 
- (1) MMA7455 Accelerometer 
- (1) Small breadboard



Wiring diagram




Code ทดสอบการทำงาน

#include <Wire.h> //Include the Wire library

// wired connections
#define HG7881_B_IA 3 // D3 --> Motor B Input A --> MOTOR B +
#define HG7881_B_IB 2 // D2 --> Motor B Input B --> MOTOR B -
#define HG7881_A_IA 5 // D5 --> Motor A Input A --> MOTOR A +
#define HG7881_A_IB 4 // D4 --> Motor A Input B --> MOTOR A -

// functional connections
#define MOTOR_B_PWM HG7881_B_IA // Motor B PWM Speed
#define MOTOR_B_DIR HG7881_B_IB // Motor B Direction
#define MOTOR_A_PWM HG7881_A_IA // Motor B PWM Speed
#define MOTOR_A_DIR HG7881_A_IB // Motor B Direction

/* Accelerometer Info
 http://yourduino.com/sunshop2/index.php?l=product_detail&p=391
 +5V and GND to Arduino/Yourduino (Module has 3.3V regulator)
 SCL -  A5 
 SDA -  A4
*/
#include <Wire.h> //Include the Wire library
#include <MMA_7455.h> //Include the MMA_7455 library    Accelerometer

MMA_7455 mySensor = MMA_7455(); //Make an instance of MMA_7455    Accelerometer
char xVal, yVal, zVal; //Variables for the values from the sensor  Accelerometer

int set = 0;
const float kp = 9;
const float kd = 7.75;
int error = 0;
int oldError = 0;
float sum = 0.0;
float pAction = 0.00;
float dAction = 0.00;
float Action = 0.00;
float time = 0;
float oldTime = 0;
int index=0;
int nAvg=4;
int de[4]= {0,0,0,0};
float deAvg=0.00;

void setup(){
  Serial.begin(115200);
  
  pinMode(MOTOR_B_DIR, OUTPUT);
  pinMode(MOTOR_B_PWM, OUTPUT);
  pinMode(MOTOR_A_DIR, OUTPUT);
  pinMode(MOTOR_A_PWM, OUTPUT);
  digitalWrite(MOTOR_B_DIR, LOW);
  digitalWrite(MOTOR_B_PWM, LOW);
  digitalWrite(MOTOR_A_DIR, LOW);
  digitalWrite(MOTOR_A_PWM, LOW);
  
  mySensor.initSensitivity(2); // Set the sensitivity you want to use: 2 = 2g (Good for "Tilt" measurements), 4 = 4g, 8 = 8g
  mySensor.calibrateOffset(10,17,-7);
  
}

void loop() 
{
  time = millis();
  
  xVal = mySensor.readAxis('x'); //Read out the 'x' Axis
  yVal = mySensor.readAxis('y'); //Read out the 'y' Axis
  zVal = mySensor.readAxis('z'); //Read out the 'z' Axis
  
  error = zVal - set;
  sum = sum - de[index];
  de[index] = (error-oldError)/((time-oldTime)/1000);  //moving average for dError
  sum = sum + de[index];
  deAvg = sum/nAvg;
  index++;
  if (index >= nAvg)
  {
    index = 0;
  }

pAction = kp*error;
dAction = kd*deAvg;
Action = (pAction + dAction);
Action = constrain(Action, -255, 255);
  
  if (Action > 0) {
    digitalWrite(MOTOR_B_DIR, HIGH); 
    analogWrite(MOTOR_B_PWM, 255 - Action);
    digitalWrite(MOTOR_A_DIR, LOW); 
    analogWrite(MOTOR_A_PWM, Action);
   }
   else if (Action < 0) {
     digitalWrite(MOTOR_B_DIR, LOW); 
     analogWrite(MOTOR_B_PWM, 0- Action);
     digitalWrite(MOTOR_A_DIR, HIGH); 
     analogWrite(MOTOR_A_PWM, 255 - (0-Action));
   }
   else {
     digitalWrite(MOTOR_B_DIR, LOW); 
     analogWrite(MOTOR_B_PWM, 0);
     digitalWrite(MOTOR_A_DIR, LOW); 
     analogWrite(MOTOR_A_PWM, 0);
   }
  
  Serial.print("Z = ");
  Serial.print(zVal, DEC);
  Serial.print("  Action: ");
  Serial.println(Action);

  delay(20);
}

ส่วนประกอบงานวิชา เขียนโปรแกรม ตัวอย่างงานที่ 6

ส่วนประกอบงานวิชา  เขียนโปรแกรม ตัวอย่างงานที่ 6

ใบรายงานผลการปฏิบัติงาน
กดกริ่งแบบไร้สาย


อุปกรณ์

For Transmitter

  • 434 MHz RF Transmitter Module
  • HT – 12E Encoder IC
  • 750 KΩ Resistor
  • Push Button
  • Power Supply
  • Connecting Wires
  • Prototyping Board (Breadboard)

For Receiver

  • Arduino UNO
  • 434 MHz RF Receiver Module
  • HT – 12D Decoder IC
  • 33 KΩ Resistor
  • Small Buzzer
  • Power Supply
  • Connecting Wires
  • Prototyping Board (Breadboard)
การต่อวงจร
Wireless Doorbell Transmitter

โค้ดทดสอบ