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

ตัวอย่างงานที่ใช้โปรแกรม Arduino

ตรวจจับระยะทางด้วย Ultrasonic Sensor HC-SR04







งานทฤษฎี สปที่7

void setup(){
pinMode(TriggerPin,OUTPUT); 
pinMode(EchoPin,INPUT); 
Serial.begin(9600); 
}
void loop(){
digitalWrite(TriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(TriggerPin, HIGH); 
delayMicroseconds(10);
digitalWrite(TriggerPin, LOW); 
Duration = pulseIn(EchoPin,HIGH); 
// returns the Duration in microseconds
long Distance_mm = Distance(Duration); 
Serial.print(“Distance = “); 
Serial.print(Distance_mm);
Serial.println(” mm”);
delay(1000); 
}
long Distance(long time)
{
long DistanceCalc;
DistanceCalc = ((time /2.9) / 2);
//DistanceCalc = time / 74 / 2; 
return DistanceCalc; 
}

ตัวอย่างงานที่ใช้โปรแกรม Arduino

การใช้งาน 3-axis Accelerometer Module (MMA7361) กับ Arduino




งานทฤษฎี สปที่6

//Programa : Conectando Acelerômetro 3 Eixos MMA7361 no Arduino
//Autor : FILIPEFLOP
#include <AcceleroMMA7361.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
AcceleroMMA7361 accelero;
int x;
int y;
int z;
void setup()
{
  lcd.begin(16, 2); 
  Serial.begin(9600);
  accelero.begin(13, 12, 11, 10, A0, A1, A2);
  accelero.setARefVoltage(3.3);  //sets the AREF voltage to 3.3V
  accelero.setSensitivity(LOW);  //sets the sensitivity to +/-6G
  accelero.calibrate();
  lcd.setCursor(0,0);
  lcd.print("X: ");
  lcd.setCursor(8,0);
  lcd.print("Y: ");
  lcd.setCursor(0,1);
  lcd.print("Z: ");
}
void loop()
{
  x = accelero.getXRaw();
  lcd.setCursor(3,0);
  lcd.print(x);
  y = accelero.getYRaw();
  lcd.setCursor(11,0);
  lcd.print(y);
  z = accelero.getZRaw();
  lcd.setCursor(3,1);
  lcd.print(z);
  Serial.print("nx: ");
  Serial.print(x);
  Serial.print("ty: ");
  Serial.print(y);
  Serial.print("tz: ");
  Serial.print(z);
  delay(500);                                  
}
                          ตัวอย่างงานที่ใช้โปรแกรม Arduino

                                                    การใช้ Flex sensor สั่งงาน Servo




งานทฤษฎี สปที่5

#include <Servo.h>
Servo servo1;
const int flexPin = A0;

void setup()

{
Serial.begin(9600);

servo1.attach(9);

}
void loop()
{
int flexPosition;
int servoPosition;
flexPosition = analogRead(flexPin);
servoPosition = map(flexPosition, 600, 900, 0, 180);
servoPosition = constrain(servoPosition, 0, 180);
servo1.write(servoPosition);
Serial.print(“sensor: “);
Serial.print(flexPosition);
Serial.print(” servo: “);
Serial.println(servoPosition);
delay(20);
}

วันเสาร์ที่ 17 พฤศจิกายน พ.ศ. 2561

ตัวอย่างงานที่ใช้โปรแกรม Arduino

เซ็นเซอร์วัดแสง LDR ร่วมกับ Arduino





งานทฤษฎี สปที่4

int pin = A0; //กำหนดพินที่ต่อ
int val = 0; //กำหนดค่าตั้งต้นของผลลัพธ์

void setup(){
 Serial.begin(9600); //กำหนดความเร็วของซีเรียล
}

void loop(){   //ลูป
  val = analogRead(pin); //ใช้ฟังก์ชั่นอ่านค่าอนาล็อกตามพินที่กำหนดไว้
  Serial.println(val); //ส่งค่าผ่านทางซีเรียลโดยใช้คำสั่ง println
  delay(300);  //ตั้งค่าดีเลย์ไว้ที่ 0.3sec
}


LDR คือ  ความต้านทานชนิดที่ไวต่อแสง ตัวความต้านทานนี้สามารถเปลี่ยนสภาพทางความนำไฟฟ้า ได้เมื่อมีแสงมาตกกระทบ บางครั้งเรียกว่าโฟโตรีซีสเตอร์ Photo  Resistor หรือ โฟโตคอนดัคเตอร์ Photo Conductor เป็นตัวต้านทานที่ทำมาจากสารกึ่งตัวนำ  

ตัวอย่างงานที่ใช้โปรแกรม Arduino

แบบเรียงลำดับกันไป จาก LED 1 To LED 4





งานทฤษฎี สปที่3

 pin 2 ต่อกับ LED 1 
 pin 3 ต่อกับ LED 2 
 pin 4 ต่อกับ LED 3 
 pin 5 ต่อกับ LED 4

int led1 = 2; // กำหนดขาใช้งาน 
int led2 = 3; 
int led3 = 4; 
int led4 = 5; 
void setup() 
{ 
pinMode(led1, OUTPUT); // กำหนดขาทำหน้าที่ OUTPUT 
pinMode(led2, OUTPUT); 
pinMode(led3, OUTPUT); 
pinMode(led4, OUTPUT); 
} 
void loop() 
{ 
digitalWrite(led1,HIGH); // ไฟ LED 1 ติด 500 ms 
delay(500); 
digitalWrite(led1,LOW); // ไฟ LED 1 ดับ500 ms 
delay(500); 
digitalWrite(led2,HIGH); 
delay(500); 
digitalWrite(led2,LOW); 
delay(500); 
digitalWrite(led3,HIGH); 
delay(500); 
digitalWrite(led3,LOW); 
delay(500); 
digitalWrite(led4,HIGH); 
delay(500); 
digitalWrite(led4,LOW); 
delay(500); 
} 

 อ้างอิง http://www.myarduino.net
ตัวอย่างงานที่ใช้โปรแกรม Arduino

Arduino วัดอุณหภูมิและความชื้น ด้วยเซนเซอร์ DHT22 





งานทฤษฎี สปที่2

#include "DHT.h"



DHT dht; // สร้างออปเจก DHT22 สำหรับติดต่อกับเซนเซอร์

void setup()
{
  Serial.begin(9600);
  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F)");

  dht.setup(2); // กำหนดขาที่ต่อกับ data ของ DHT22 เป็น ขา arduino pin 2
}

void loop()
{
  delay(dht.getMinimumSamplingPeriod());

  float humidity = dht.getHumidity(); // คำสั่งดึงค่าความชื้นจาก DHT22
  float temperature = dht.getTemperature(); // คำสั่งดึงค่าอุณหภูมิจาก DHT22

  Serial.print(dht.getStatusString());
  Serial.print("\tHumidity :");
  Serial.print(humidity, 1);
  Serial.print("\t\tTemp C:");
  Serial.print(temperature, 1);
  Serial.print("\t\tTemp F:");
  Serial.println(dht.toFahrenheit(temperature), 1); // แปลงองศาเซลเซียสเป็นฟาเรนไฮน์

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

ตัวอย่างงานที่ใช้โปรแกรม Arduino


Arduino เปิดปิดประตู (P3)




งานทฤษฎี สปที่1
#include <Servo.h>
Servo myservo; 
int pos = 0; 
void setup() {
myservo.attach(9);
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { 
myservo.write(pos); 
delay(15); 
}
for (pos = 180; pos >= 0; pos ‐= 1) { 
myservo.write(pos); 
delay(15); // 
}
}
#include <Servo.h> คือการเรียกใช ้librarly เพื่อควบคุม servo ﴾การเขียนโค้ด ตัว พิมพ์เล็กกับพิมพ์ใหญ่ ไม่ใช่ตัวอักษรเดียวกัน﴿
Servo myservo; คือการประกาศตัวแปร ชื่อ myservo ซึ่งเป็นตัวแปรประเภท Servo จากใน librarly Servo.h
แค่ ตัว compiler ฉลาดพอทีจะเติมใหเ้ราอัตโนมัติ﴿
int pos = 0; ประกาศตัวแปรชื่อ pos มีค่าเท่ากับ 0 เป็นตัวแปรประเภท int
ในทีนี้การ setup สั่งใหตั้วแปร myservo attach pin 9 หรือก็คือ กำหนด pin ส่งสัญญาณ pwm เพื่อควบคุม servo เป็น pin 9 เมื่อ เราเรียกใช ้myservo ครั้งต่อไป มันจะส่งสัญญาณออกไปที่ pin 9
อัตโนมัติ เราไม่จำเป็นต้อง attach ซ้ำๆถ้าในภาษา ของ network ก็คือการ binding
ใน loop มี for loop อยู่ รูปแบบของ for loop คือ ในทีนี้เรากำหนดให ้pos เป็น 0 เมื่อเริ่ม loop และ จะทำงานไปเรื่อยๆ เมื่อ pos มีค่าน้อยกว่า หรือเท่ากับ 180 ใน แต่ละครั้งทีทำงาน ค่าของ pos จะเพิ่มขึ้นทีละ 1 ใน loop

เราสั่งให้ myservo write ตัวแปร pos write คือการส่งสัญญาณ ออกไป ส่วนค่าทีสั่งออกไป คือ pos
มีค่าเป็น ตาม for loop ในครั้งแรก มีค่าเป็น 0 หมายถึง สั่งให้ servo หมุนไปตำแหน่ง 0 วนไปเรื่อยๆจนถึง 180
delay﴾15﴿; หมายถึงให้หยุดพัก 15 มิลลิ วินาที ก่อนทำงานต่อ ทีต้องมี delay เพราะบางครั้ง
โปรแกรมทำงานเร็วเกินไป จนอุปกรณ์ตัวอื่นตามไม่ทัน
for loop ที่ 2 คือการสั่ง ให้servo หมุนจาก 180 กลับไป ที่ 0 เหมือนเดิม