วันอังคารที่ 3 เมษายน พ.ศ. 2561

ใบงานที่ 8 Ultrasonic Object Radar System

ผู้จัดทำ
1.นางสาวณัฐธิชา ชนเก่าน้อย 011
2. นางสาวกุลจิรา       ทองคง 003

อุปกรณ์ที่ใช้ 
1. บอร์ด Arduino 1 บอร์ด
2. Servo motor   1 ตัว
3.็ HC-SR04       1 ตัว
4. สายไฟผู้-ผู้      3 สาย
5. สายไฟผู้-เมีย   4 สาย
6. สายอัพโหลด   1 สาย

รูปวงจร



Code Arduino

// Includes the Servo library #include <Servo.h>. // Defines Tirg and Echo pins of the Ultrasonic Sensor const int trigPin = 10; const int echoPin = 11; // Variables for the duration and the distance long duration; int distance; Servo myServo; // Creates a servo object for controlling the servo motor void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(9600); myServo.attach(12); // Defines on which pin is the servo motor attached } void loop() { // rotates the servo motor from 15 to 165 degrees for(int i=15;i<=165;i++){ myServo.write(i); delay(30); distance = calculateDistance();// Calls a function for calculating the distance measured by the Ultrasonic sensor for each degree Serial.print(i); // Sends the current degree into the Serial Port Serial.print(","); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing Serial.print(distance); // Sends the distance value into the Serial Port Serial.print("."); // Sends addition character right next to the previous value needed later in the Processing IDE for indexing } // Repeats the previous lines from 165 to 15 degrees for(int i=165;i>15;i--){ myServo.write(i); delay(30); distance = calculateDistance(); Serial.print(i); Serial.print(","); Serial.print(distance); Serial.print("."); } } // Function for calculating the distance measured by the Ultrasonic sensor int calculateDistance(){ digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds distance= duration*0.034/2; return distance; }

Code Processing
import processing.serial.*; // imports library for serial communication
import java.awt.event.KeyEvent; // imports library for reading the data from the serial port
import java.io.IOException;
Serial myPort; // defines Object Serial
// defubes variables
String angle="";
String distance="";
String data="";
String noObject;
float pixsDistance;
int iAngle, iDistance;
int index1=0;
int index2=0;
PFont orcFont;
void setup() {

size (1200, 700); // **CHANGE THIS TO YOUR SCREEN RESOLUTION**
smooth();
myPort = new Serial(this,"COM5", 9600); // starts the serial communication
myPort.bufferUntil('.'); // reads the data from the serial port up to the character '.'. So actually it reads this: angle,distance.
}
void draw() {

fill(98,245,31);
// simulating motion blur and slow fade of the moving line
noStroke();
fill(0,4);
rect(0, 0, width, height-height*0.065);

fill(98,245,31); // green color
// calls the functions for drawing the radar
drawRadar();
drawLine();
drawObject();
drawText();
}
void serialEvent (Serial myPort) { // starts reading data from the Serial Port
// reads the data from the Serial Port up to the character '.' and puts it into the String variable "data".
data = myPort.readStringUntil('.');
data = data.substring(0,data.length()-1);

index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1"
angle= data.substring(0, index1); // read the data from position "0" to position of the variable index1 or thats the value of the angle the Arduino Board sent into the Serial Port
distance= data.substring(index1+1, data.length()); // read the data from position "index1" to the end of the data pr thats the value of the distance

// converts the String variables into Integer
iAngle = int(angle);
iDistance = int(distance);
}
void drawRadar() {
pushMatrix();
translate(width/2,height-height*0.074); // moves the starting coordinats to new location
noFill();
strokeWeight(2);
stroke(98,245,31);
// draws the arc lines
arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI);
arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI);
arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI);
arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI);
// draws the angle lines
line(-width/2,0,width/2,0);
line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90)));
line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120)));
line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150)));
line((-width/2)*cos(radians(30)),0,width/2,0);
popMatrix();
}
void drawObject() {
pushMatrix();
translate(width/2,height-height*0.074); // moves the starting coordinats to new location
strokeWeight(9);
stroke(255,10,10); // red color
pixsDistance = iDistance*((height-height*0.1666)*0.025); // covers the distance from the sensor from cm to pixels
// limiting the range to 40 cms
if(iDistance<40){
// draws the object according to the angle and the distance
line(pixsDistance*cos(radians(iAngle)),-pixsDistance*sin(radians(iAngle)),(width-width*0.505)*cos(radians(iAngle)),-(width-width*0.505)*sin(radians(iAngle)));
}
popMatrix();
}
void drawLine() {
pushMatrix();
strokeWeight(9);
stroke(30,250,60);
translate(width/2,height-height*0.074); // moves the starting coordinats to new location
line(0,0,(height-height*0.12)*cos(radians(iAngle)),-(height-height*0.12)*sin(radians(iAngle))); // draws the line according to the angle
popMatrix();
}
void drawText() { // draws the texts on the screen

pushMatrix();
if(iDistance>40) {
noObject = "Out of Range";
}
else {
noObject = "In Range";
}
fill(0,0,0);
noStroke();
rect(0, height-height*0.0648, width, height);
fill(98,245,31);
textSize(25);

text("10cm",width-width*0.3854,height-height*0.0833);
text("20cm",width-width*0.281,height-height*0.0833);
text("30cm",width-width*0.177,height-height*0.0833);
text("40cm",width-width*0.0729,height-height*0.0833);
textSize(40);
text(" VIRAL SCIENCE ", width-width*0.875, height-height*0.0277);
text("Angle: " + iAngle +" ฐ", width-width*0.48, height-height*0.0277);
text("Distance: ", width-width*0.26, height-height*0.0277);
if(iDistance<40) {
text(" " + iDistance +" cm", width-width*0.225, height-height*0.0277);
}
textSize(25);
fill(98,245,60);
translate((width-width*0.4994)+width/2*cos(radians(30)),(height-height*0.0907)-width/2*sin(radians(30)));
rotate(-radians(-60));
text("30ฐ",0,0);
resetMatrix();
translate((width-width*0.503)+width/2*cos(radians(60)),(height-height*0.0888)-width/2*sin(radians(60)));
rotate(-radians(-30));
text("60ฐ",0,0);
resetMatrix();
translate((width-width*0.507)+width/2*cos(radians(90)),(height-height*0.0833)-width/2*sin(radians(90)));
rotate(radians(0));
text("90ฐ",0,0);
resetMatrix();
translate(width-width*0.513+width/2*cos(radians(120)),(height-height*0.07129)-width/2*sin(radians(120)));
rotate(radians(-30));
text("120ฐ",0,0);
resetMatrix();
translate((width-width*0.5104)+width/2*cos(radians(150)),(height-height*0.0574)-width/2*sin(radians(150)));
rotate(radians(-60));
text("150ฐ",0,0);
popMatrix();
}

คำอธิบายหลักการทำงาน
กำหนดให้อุปกรณ์ตรวจจับสิ่งของถ้าเจอวัตถุอยู่ไกล้ในโปรแกรม processing จะขึ้นสีแดง ถ้าไม่เจอวัตถจะขึ้นสีเขียว

วิดิโอ



วันจันทร์ที่ 2 เมษายน พ.ศ. 2561

ใบงานที่ 7 Measure Distance with Arduino and Ultrasonic Sensor


ผู้จัดทำ
1.นางสาวณัฐธิชา ชนเก่าน้อย 011
2. นางสาวกุลจิรา       ทองคง 003

อุปกรณ์ที่ใช้
1.โฟโต้บอร์ด            1 บอร์ด
2. บอร์ด Arduino      1 บอร์ด
3. สายอัพโหลด        1 สาย
4. สายไฟผู้-ผู้            7 สาย
5. HC-SR04              1  ตัว
6. Servo motor          1  ตัว


รูปวงจร




 Code
#include <Servo.h> //Load Servo Library int trigPin=13; //Sensor Trip pin connected to Arduino pin 13 int echoPin=11; //Sensor Echo pin connected to Arduino pin 11 int servoControlPin=6; //Servo control line is connected to pin 6 float pingTime; //time for ping to travel from sensor to target and return float targetDistance; //Distance to Target in inches float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees. float servoAngle; //Variable for the value we want to set servo to. Servo myPointer; //Create a servo object called myPointer void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(servoControlPin, OUTPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); myPointer.attach(servoControlPin); //Tell arduino where the servo is attached. } void loop() { // put your main code here, to run repeatedly: digitalWrite(trigPin, LOW); //Set trigger pin low delayMicroseconds(2000); //Let signal settle digitalWrite(trigPin, HIGH); //Set trigPin high delayMicroseconds(15); //Delay in high state digitalWrite(trigPin, LOW); //ping has now been sent delayMicroseconds(10); //Delay in low state pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second) pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour) targetDistance= speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per hour targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance. targetDistance= targetDistance*63360; //Convert miles to inches by multipling by 63360 (inches per mile) Serial.print("The Distance to Target is: "); Serial.print(targetDistance); Serial.println(" inches"); servoAngle = (106./7.) * targetDistance + 37; //Calculate Servo Angle from targetDistance myPointer.write(servoAngle); //write servoAngle to the servo Serial.println(servoAngle); delay(100); //delay tenth of a second to slow things down a little. } #include <Servo.h> //Load Servo Library int trigPin=13; //Sensor Trip pin connected to Arduino pin 13 int echoPin=11; //Sensor Echo pin connected to Arduino pin 11 int servoControlPin=6; //Servo control line is connected to pin 6 float pingTime; //time for ping to travel from sensor to target and return float targetDistance; //Distance to Target in inches float speedOfSound=776.5; //Speed of sound in miles per hour when temp is 77 degrees. float servoAngle; //Variable for the value we want to set servo to. Servo myPointer; //Create a servo object called myPointer void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(servoControlPin, OUTPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); myPointer.attach(servoControlPin); //Tell arduino where the servo is attached. } void loop() { // put your main code here, to run repeatedly: digitalWrite(trigPin, LOW); //Set trigger pin low delayMicroseconds(2000); //Let signal settle digitalWrite(trigPin, HIGH); //Set trigPin high delayMicroseconds(15); //Delay in high state digitalWrite(trigPin, LOW); //ping has now been sent delayMicroseconds(10); //Delay in low state pingTime = pulseIn(echoPin, HIGH); //pingTime is presented in microceconds pingTime=pingTime/1000000; //convert pingTime to seconds by dividing by 1000000 (microseconds in a second) pingTime=pingTime/3600; //convert pingtime to hourse by dividing by 3600 (seconds in an hour) targetDistance= speedOfSound * pingTime; //This will be in miles, since speed of sound was miles per hour targetDistance=targetDistance/2; //Remember ping travels to target and back from target, so you must divide by 2 for actual target distance. targetDistance= targetDistance*63360; //Convert miles to inches by multipling by 63360 (inches per mile) Serial.print("The Distance to Target is: "); Serial.print(targetDistance); Serial.println(" inches"); servoAngle = (106./7.) * targetDistance + 37; //Calculate Servo Angle from targetDistance myPointer.write(servoAngle); //write servoAngle to the servo Serial.println(servoAngle); delay(100); //delay tenth of a second to slow things down a little. }
คำอธิบายหลักการทำงาน
- วัดระยะทางแสดงผลโดยใช้ Servo motor (เข็มชี้ตัวเลข)
- Monitor แสดงผล ระยะทางในหน่วยของนิ้ว และ เซนติเมตร
- Monitor แสดงผลมุมของ Servo (Angle)





วันพุธที่ 28 มีนาคม พ.ศ. 2561

ใบงานที่ 6 การควบคุม Servo Motor (SG90) ด้วย Arduino UNO R3

ผู้จัดทำ
นางสาวณัฐธิชา ชนเก่าน้อย 001
นางสาวกุลจิรา        ทองคง 003

  Servo เป็นคำศัพท์ที่ใช้กันทั่วไปในระบบควบคุมอัตโนมัติ มาจากภาษาละตินคำว่า Sevus หมายถึง “ทาส” (Slave) ในเชิงความหมายของ Servo Motor ก็คือ Motor ที่เราสามารถสั่งงานหรือตั้งค่า แล้วตัว Motor จะหมุนไปยังตำแหน่งองศาที่เราสั่งได้เองอย่างถูกต้อง โดยใช้การควบคุมแบบป้อนกลับ (Feedback Control) ในบทความนี้จะกล่าวถึง RC Servo Motor ซึ่งนิยมนำมาใช้ในเครื่องเล่นที่บังคับด้วยคลื่นวิทยุ (RC = Radio - Controlled) เช่น เรือบังคับวิทยุ รถบังคับวิทยุ เฮลิคอปเตอร์บังคับวิทยุ เป็นต้น
          Feedback Control คือ ระบบควบคุมที่มีการวัดค่าเอาต์พุตของระบบนำมาเปรียบเทียบกับค่าอินพุตเพื่อควบคุมและปรับแต่งให้ค่าเอาต์พุตของระบบให้มีค่า เท่ากับ หรือ ใกล้เคียงกับค่าอินพุต 
ส่วนประกอบภายนอก RC Servo Motor
02
- Case ตัวถัง หรือ กรอบของตัว Servo Motor
- Mounting Tab ส่วนจับยึดตัว Servo กับชิ้นงาน
- Output Shaft เพลาส่งกำลัง
- Servo Horns ส่วนเชื่อมต่อกับ Output shaft เพื่อสร้างกลไกล
- Cable สายเชื่อมต่อเพื่อ จ่ายไฟฟ้า และ ควบคุม Servo Motor จะประกอบด้วยสายไฟ 3 เส้น และ ใน RC Servo Motor จะมีสีของสายแตกต่างกันไปดังนี้ 
         o สายสีแดง คือ ไฟเลี้ยง (4.8-6V)
         o สายสีดำ หรือ น้ำตาล คือ กราวด์
         o สายสีเหลือง (ส้ม ขาว หรือฟ้า) คือ สายส่งสัญญาณพัลซ์ควบคุม (3-5V) 
- Connector จุดเชื่อมต่อสายไฟ 
ส่วนประกอบภายใน RC Servo Motor
03Ref: www.pololu.com
1. Motor เป็นส่วนของตัวมอเตอร์
2. Gear Train หรือ Gearbox เป็นชุดเกียร์ทดแรง
3. Position Sensor เป็นเซ็นเซอร์ตรวจจับตำแหน่งเพื่อหาค่าองศาในการหมุน
4. Electronic Control System เป็นส่วนที่ควบคุมและประมวลผล 
Servo Motor Block Diagram
04
หลักการทำงานของ RC Servo Motor 
             เมื่อจ่ายสัญญาณพัลซ์เข้ามายัง RC Servo Motor ส่วนวงจรควบคุม (Electronic Control System) ภายใน Servo จะทำการอ่านและประมวลผลค่าความกว้างของสัญญาณพัลซ์ที่ส่งเข้ามาเพื่อแปลค่าเป็นตำแหน่งองศาที่ต้องการให้ Motor หมุนเคลื่อนที่ไปยังตำแหน่งนั้น แล้วส่งคำสั่งไปทำการควบคุมให้ Motor หมุนไปยังตำแหน่งที่ต้องการ โดยมี Position Sensor เป็นตัวเซ็นเซอร์คอยวัดค่ามุมที่ Motor กำลังหมุน เป็น Feedback กลับมาให้วงจรควบคุมเปรียบเทียบกับค่าอินพุตเพื่อควบคุมให้ได้ตำแหน่งที่ต้องการอย่างถูกต้องแม่นยำ
สัญญาณ RC ในรูปแบบ PWM
            ตัว RC Servo Motor ออกแบบมาใช้สำหรับรับคำสั่งจาก Remote Control ที่ใช้ควบคุมของเล่นด้วยสัญญาณวิทยุต่างๆ เช่น เครื่องบินบังคับ รถบังบังคับ เรือบังคับ เป็นต้น ซึ่ง Remote จำพวกนี้ที่ภาครับจะแปลงความถี่วิทยุออกมาในรูปแบบสัญญาณ PWM (Pulse Width Modulation)
05           มุมหรือองศาจะขึ้นอยู่กับความกว้างของสัญญาณพัลซ์ ซึ่งโดยส่วนมากความกว้างของพัลซ์ที่ใช้ใน RC Servo Motor จะอยู่ในช่วง 1-2 ms หรือ 0.5-2.5 ms 
           ยกตัวอย่างเช่นหากกำหนดความกว้างของสัญญาณพัลซ์ไว้ที่ 1 ms ตัว Servo Motor จะหมุนไปทางด้ายซ้ายจนสุด ในทางกลับกันหากกำหนดความกว้างของสัญญาณพัลซ์ไว้ที่ 2 ms ตัว Servo Motor จะหมุนไปยังตำแหน่งขวาสุด แต่หากกำหนดความกว้างของสัญญาณพัลซ์ไว้ที่ 1.5 ms ตัว Servo Motor ก็จะหมุนมาอยู่ที่ตำแหน่งตรงกลางพอดี
06
           ดังนั้นสามารถกำหนดองศาการหมุนของ RC Servo Motor ได้โดยการเทียบค่า เช่น RC Servo Motor สามารถหมุนได้ 180 องศา โดยที่ 0 องศาใช้ความกว้างพัลซ์เท่ากับ 1000 us ที่ 180 องศาความกว้างพัลซ์เท่ากับ 2000 us เพราะฉะนั้นค่าที่เปลี่ยนไป 1 องศาจะใช้ความกว้างพัลซ์ต่างกัน (2000-1000)/180 เท่ากับ 5.55 us
           จากการหาค่าความกว้างพัลซ์ที่มุม 1 องศาข้างต้น หากต้องกำหนดให้ RC Servo Motor หมุนไปที่มุม 45 องศาจะหาค่าพัลซ์ที่ต้องการได้จาก 5.55 x 45 เท่ากับ 249.75 us แต่ที่มุม 0 องศาเราเริ่มที่ความกว้างพัลซ์ 1ms หรือ 1000 us เพราะฉะนั้นความกว้างพัลซ์ที่ใช้กำหนดให้ RC Servo Motor หมุนไปที่ 45 องศา คือ 1000 + 249.75 เท่ากับประมาณ 1250 us
07
วิธีควบคุม RC Servo Motor ด้วย Arduino
            Arduino มีไลบรารี่สำหรับสั่งงาน RC Servo Motor มาให้ใช้งานอยู่แล้วเป็นฟังก์ชั่นสำเร็จรูปและใช้งานได้ง่ายในหน้าเว็บไซต์ http://arduino.cc/en/reference/servo ได้ให้ข้อมูลไว้ว่า Servo Library ของ Arduino สามารถสั่งงาน RC Servo Motor ได้ทั้งแบบหมุนไป-กลับได้ 0-180 องศา (ที่กล่าวถึงตามตัวอย่างข้างต้น) และแบบต่อเนื่องที่หมุนครบรอบได้เรียกว่าเป็น Continuous Rotation Servo (ซึ่งในช่วงท้ายบทความจะกล่าวถึงเพิ่มเติม) โดยสามารถรองรับการเชื่อมต่อ RC Servo Motor ได้ถึง 12 ตัวกับบอร์ด Arduino UNO และรองรับสูงสุดถึง 48 ตัวหากใช้บอร์ด Arduino Mega 
ฟังก์ชั่นภายใน Servo Library
- attach()
- write()
- writeMicroseconds()
- read()
- attached()
- detach()
attach()
Description
          คือฟังก์ชั่นที่ใช้ในการกำหนดขาสัญญาณที่ Servo Motor ต่อกับ Arduino และกำหนดความกว้างของพัลซ์ที่ 0 องศาและ 180 องศา 
Syntax
          Servo.attach(pin)
          Servo.attach(pin,min,max)
Parameters
          Pin: คือ ขาสัญญาณของ Arduino ที่ใช้เชื่อมต่อกับ Servo Motor
          Min: คือ ความกว้างของพัลซ์ที่ 0 องศาของ Servo ตัวที่ใช้ในหน่วยไมโครวินาที (us) โดยปกติแล้วหากไม่มีการตั้งค่าโปรแกรมจะกำหนดค่าไว้ที่ 544 us
          Max: คือ ความกว้างของพัลซ์ที่ 180 องศาของ Servo ตัวที่ใช้ในหน่วยไมโครวินาที (us) โดยปกติแล้วหากไม่มีการตั้งค่าโปรแกรมจะกำหนดค่าไว้ที่ 2400 us
Write()
Description
          คือฟังก์ชั่นที่ใช้ควบคุมตำแหน่งที่ต้องการให้ Servo Motor หมุนไปยังองศาที่กำหนดสามารถกำหนดเป็นค่าองศาได้เลย คือ 0-180 องศา แต่ใน Servo Motor ที่เป็น Full Rotation คำสั่ง write จะเป็นการกำหนดความเร็วในการหมุน โดย 
          ค่าเท่ากับ 90 คือคำสั่งให้ Servo Motor หยุดหมุน
          ค่าเท่ากับ 0 คือการหมุนด้วยความเร็วสูงสุดในทิศทางหนึ่ง
          ค่าเท่ากับ 180 คือการหมุนด้วยความเร็วสูงสุดในทิศทางตรงกันข้าม
Syntax
          servo.write(angle)
Parameters
          Angle: คือมุมที่ต้องการให้ RC Servo Motor แบบ 0-180 องศาหมุนไป แต่หากเป็น RC Servo Motor แบบ Full Rotation ค่า Angle คือ การกำหนดความเร็วและทิศทางในการหมุน
writeMicroseconds()
Description
          คือฟังก์ชั่นที่ใช้ควบคุมตำแหน่งที่ให้ Servo Motor หมุนไปยังตำแหน่งองศาที่กำหนดโดยกำหนดเป็นค่าความกว้างของพัลซ์ในหน่วย us ซึ่งปกติแล้ว RC Servo Motor จะใช้ความกว้างของพัลซ์อยู่ที่ 1000-2000 us ตามที่ได้กล่าวไปข้างต้นแล้ว แต่ RC Servo Motor บางรุ่นหรือบางยี่ห้อไม่ได้ใช้ ช่วงความกว้างของพัลซ์ตามที่ได้กล่าวเอาไว้นี้ อาจจะใช้ช่วง 700-2300 แทนก็สามารถใช้ฟังก์ชั่น writeMicroseconds นี้เพื่อกำหนดความกว้างพัลซ์ได้เอง
          การใช้ฟังก์ชั่น writeMicroseconds สามารถกำหนดค่าได้อิสระ ตรงนี้ ”ต้องระวังในการใช้งาน” หากสั่งงาน RC Servo Motor (แบบ 0 - 180 องศา) จนหมุนไปเกินจุดสิ้นสุดคือเกินทั้งฝั่ง 0 หรือ 180 องศา จะทำให้เกิดเสียงครางดังจากการหมุนไปต่อไม่ได้และมอเตอร์จะกินกระแสสูงขึ้นด้วยในเวลาเดียวกันนั้น ซึ่งอาจทำให้ RC Servo Motor เกิดความเสียหายได้
อุปกรณ์ที่ใช้
1. บอร์ด Arduino      1 บอร์ด
2. สาย  USB            1 สาย
3. สายไฟผู้-ผู้           3 สาย
4. Motor SG90         1 ตัว
รูปวงจร


Code 0-180
#include <Servo.h> Servo servo; int angle = 10; void setup() { servo.attach(8); servo.write(angle); } void loop() { // scan from 0 to 180 degrees for(angle = 0; angle < 180; angle++) { servo.write(angle); delay(15); } // now scan back from 180 to 0 degrees for(angle = 180; angle > 0; angle--) { servo.write(angle); delay(15); } }
Code 45-135
#include <Servo.h> Servo servo; int angle = 10; void setup() { servo.attach(8); servo.write(angle); } void loop() { // scan from 0 to 180 degrees for(angle = 45; angle < 135; angle++) { servo.write(angle); delay(15); } // now scan back from 180 to 0 degrees for(angle = 135; angle > 45; angle--) { servo.write(angle); delay(15); } }
Code 90-180
#include <Servo.h> Servo servo; int angle = 10; void setup() { servo.attach(8); servo.write(angle); } void loop() { // scan from 0 to 180 degrees for(angle = 90; angle < 180; angle++) { servo.write(angle); delay(15); } // now scan back from 180 to 0 degrees for(angle = 180; angle > 90; angle--) { servo.write(angle); delay(15); } }

คำอธิบายหลักการทำงาน
เมื่ออัพโหลดโปรแกรมให้ Servo Motor (SG90) ด้วย Arduino UNO R3 หมุนจากซ้ายไปขวาและขวามาซ้าย
- Servo หมุน จาก 0 องศา - 180 องศา - Servo หมุน จาก 45 องศา - 135 องศา - Servo หมุน จาก 90 องศา - 180 องศา

วิดีโอ 0 องศา - 180 องศา

วิดีโอ 45 องศา - 135 องศา


วิดีโอ 90 องศา - 180 องศา






วันอังคารที่ 27 มีนาคม พ.ศ. 2561

ใบงานที่ 5 เรื่องHC-SR04 with Buzzer

ผู้จัดทำ
1.นางสาวณัฐธิชา ชนเก่าน้อย 011
2. นางสาวกุลจิรา      ทองคง 003


อุปกรณ์
1.บอร์ด Arduinno
2.LED 1 ดวง
3.สายไฟผู้-ผู้ 8 สาย
4.USB
5.HC-SR04
6.Buzzer
7.ตัวต้านทาน 1 ตัว

CODE
const int trigPin = 9; const int echoPin = 10; int buzzer = 5; int LED1 = 2; float duration, distance; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(LED1, OUTPUT); Serial.begin(9600); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration*.0343)/2; Serial.print("ระยะวัตถุ : "); Serial.print(distance); Serial.println(" ซม."); Serial.print("ระยะวัตถุ : "); Serial.print(distance * 0.39370); Serial.println(" นิ้ว "); delay(500); if (distance < 30 && distance >= 21) { digitalWrite(buzzer,HIGH); digitalWrite(LED1,HIGH); delay(1000); digitalWrite(buzzer,LOW); digitalWrite(LED1,LOW); delay(1000); } else if(distance <=21 && distance >= 11) { digitalWrite(buzzer,HIGH); digitalWrite(LED1,HIGH); delay(500); digitalWrite(buzzer,LOW); digitalWrite(LED1,LOW); delay(500); } else if(distance <=11 && distance >= 6) { digitalWrite(buzzer,HIGH); digitalWrite(LED1,HIGH); delay(200); digitalWrite(buzzer,LOW); digitalWrite(LED1,LOW); delay(200); } else if(distance <=5) { digitalWrite(buzzer,HIGH); digitalWrite(LED1,HIGH); } }

ให้เขียนโปรแกรมให้ทำงานดังต่อไปนี้ - ถ้า ระยะวัตถุ ตั้งแต่ 21-30 ซม. ให้ Buzzer ส่งเสียงเตือนเป็นจังหวะ ติด 1 วินาที - ดับ 1 วินาที - ถ้า ระยะวัตถุ ตั้งแต่ 11-20 ซม. ให้ Buzzer ส่งเสียงเตือนเป็นจังหวะ ติด 0.5 วินาที - ดับ 0.5 วินาที - ถ้า ระยะวัตถุ ตั้งแต่ 6-10 ซม. ให้ Buzzer ส่งเสียงเตือนเป็นจังหวะ ติด 0.2 วินาที - ดับ 0.2 วินาที - ถ้า ระยะวัตถุ ตั้งแต่ 5 ซม. ลงมา ให้ Buzzer ส่งเสียงเตือนติดยาวตลอด - ถ้า ระยะวัตถุ ตั้งแต่ 31 ซม. ให้ Buzzer ไม่เสียงเตือน และให้ LED แสดงผลตามจังหวะเสียงของ Buzzer ทั้งนี้ให้ Serial Monitor แสดงค่าระยะทาง หน่วยเป็น cm และ inch (นิ้ว)




วีดีโอ

ใบงานที่ 8 Ultrasonic Object Radar System

ผู้จัดทำ 1.นางสาวณัฐธิชา ชนเก่าน้อย 011 2. นางสาวกุลจิรา       ทองคง 003 อุปกรณ์ที่ใช้  1. บอร์ด Arduino 1 บอร์ด 2. Servo motor...