วันอาทิตย์ที่ 13 มกราคม พ.ศ. 2562

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

การใช้ Arduinoร่วมกับDHT22แสดงผลออกLCD
วัดอุณภูมมิ


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

#include <DHT.h>
#include <LiquidCrystal.h>
// Pin connected to the sensor data pin
#define DHTPIN 7
// Display pins
LiquidCrystal lcd(12,11,5,4,3,2);
// Use the line according to the sensor model
// #define DHTTYPE DHT11 // Sensor DHT11
#define DHTTYPE DHT22 //DHT Sensor 22 (AM2302)
// #define DHTTYPE DHT21 // DHT Sensor 21 (AM2301)
// Definitions sensor: pin, type
DHT dht (DHTPIN, DHTTYPE);
// Array symbol degree
byte degree [8] =
{
B00001100,
B00010010,
B00010010,
B00001100,
B00000000,
B00000000,
B00000000,
B00000000,
};
void setup() {
// put your setup code here, to run once:
// Initialize the display
lcd.begin (16,2);
lcd.clear ();
// Create the custom character with the symbol of the degree
lcd.createChar (0,degree);
// Information on the initial display
lcd.setCursor (0,0);
lcd.print (“Temp. : “);
lcd.setCursor (13,0);
// Shows the symbol of the degree
lcd.write (byte (0));
lcd.print (“C”);
lcd.setCursor (0,1);
lcd.print (“Umid. : “);
lcd.setCursor (14,1);
lcd.print (“%”);
Serial.begin (9600);
Serial.println (“Waiting for data …”);
// Starts DHT sensor
dht.begin ();
}
void loop()
{
// put your main code here, to run repeatedly:
// Wait 2 seconds between the measurements
delay (2000);
// Moisture reading
float h = dht.readHumidity();
float t = dht.readTemperature();
// dht.readHumidity float h = ();
// Reading of temperature (Celsius)
// t = dht.readTemperature float ();
// Check if the sensor is responding
if (isnan (h) || isnan (t))
{
Serial.println (“Failed to read DHT sensor data !!!”);
return;
}
// Display the temperature in the serial monitor and display
Serial.print (“Temperature: “);
Serial.print (t);
lcd.setCursor(8,0);
lcd.print (t);
Serial.print (” C * “);
// Show the moisture in the serial monitor and display
Serial.print (“humidity: “);
Serial.print (h);
Serial.println (” %”);
lcd.setCursor (8,1);
lcd.print (h);
}

ตัวอย่างงานที่ใช้โปรแกรม Arduino
ตัวอย่างการส่งข้อมูลหากันระหว่าง Arduino กับ Nodemuc Esp8266 โดยผ่าน Serial



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

int a = 13;
int b = 25;
#include <SoftwareSerial.h>
SoftwareSerial chat(10, 11); // RX, TX
int i;
void setup()  {
  Serial.begin(9600);
  chat.begin(4800);
}

void loop() {
  if (chat.readString()){
     // chat.print(1);
     if(chat.readString()== "Question1"){ 
      chat.print(a);
     }
     if(chat.readString()== "Question2"){ 
      chat.print(b);
     }
      Serial.print("Send = ");
      Serial.println(i);
  }
  i++;
  delay(1000);

}

วันเสาร์ที่ 5 มกราคม พ.ศ. 2562

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

สั่งงาน Arduino ผ่าน Bluetooth


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

char BYTE;
int  LED = 13; // LED on pin 13

void setup() {
  Serial.begin(9600); 
  pinMode(LED, OUTPUT);
}

void loop() {
  Serial.println("Press 1 to turn Arduino pin 13 LED ON or 0 to turn it OFF:");
  while (!Serial.available());   
  BYTE = Serial.read();        
  Serial.print(BYTE);
  if( BYTE == '0' ) digitalWrite(LED, LOW);  
  if( BYTE == '1' ) digitalWrite(LED, HIGH);
  delay(50);
}

งานปฏิบัติที่3


โปรแกรม

int led = 10;
void setup ()
{
  Serial.begin(9600);
  pinMode (led,OUTPUT);
}
void loop()
{
  int x,y;
  x = analogRead(A0);
  y = map(x,0,1023,0,255);
  Serial.print("x = ");Serial.println(x);
  Serial.print("y = ");Serial.println(y);
  analogWrite(led,y); 
}


อธิบายการทำงาน
จะมีตัวเลขขึ้นตามที่เรากำหนดใน  y = map ตัวเลขจะเพิ่มมากน้อยขึ้นอยู่กับการปรับตัวต้านทาน


วันจันทร์ที่ 31 ธันวาคม พ.ศ. 2561

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

การใช้งาน 3-axis Accelerometer Module 





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

#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);                                  
}

วันอาทิตย์ที่ 30 ธันวาคม พ.ศ. 2561

งานปฏิบัติที่2


โปรแกรม


#include <LedControl.h>
LedControl lc=LedControl (5,7,6,1);
 void show4digit (int num)
{
  int seg1,seg2,seg3,seg4;
  seg1 = ((num%1000)%100)%10;
  seg2 = ((num%1000)%100)/10;
  seg3 = (num%1000)/100;
  seg4 = num/1000;
   lc.setDigit (0,0,seg1,false);
   if (num>=10)
   lc.setDigit(0,1,seg2,false);
   if (num>=100)
   lc.setDigit(0,2,seg3,false);
   if (num>=1000)
   lc.setDigit(0,3,seg4,false);
   delay(300); 
}
  void setup()
{
  Serial.begin(9600);
  lc.shutdown(0,false);
  lc.setIntensity(0,5);
  lc.clearDisplay(0);
}
  void loop()
{
  int num;
  num = analogRead(A0);
  Serial.print("analogRead=") ;Serial.println(num);
  lc.clearDisplay(0);
  show4digit(num);

อธิบายการทำงาน
 เมื่อปรับค่าตัวต้านทานตัวเลขจะเพิ่มขึ้นหรือลดลงตามการปรับตัวต้านทาน
งานปฏิบัติที่1


โปรแกรม

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
 {
 pinMode (0, INPUT);
 pinMode (1, INPUT);
 pinMode (8, OUTPUT);
 pinMode (9, OUTPUT);
 lcd.begin(16, 2);
 }

void loop() 
  {
  int re=digitalRead (0);
  int le=digitalRead (1);
  if (re==0)
  { digitalWrite (8, HIGH);
  delay(500);
  lcd.setCursor(0, 1);
  lcd. print(" Motor1 ");
  digitalWrite (8, LOW);
  }
  if  (le==0){
    digitalWrite (9, HIGH);
    delay (500);
    lcd.setCursor (0, 1);
    lcd.print (" Motor2 ");
  }
}

อธิบายการทำงาน
เมื่อเรากดสวิตซ์ที่1 LCD จะขึ้นคำว่า Motor1 และ ถ้ากด สวิตซ์ที่2 LCD จะขึ้นคำว่า Motor2 หากกดค้างทั้งสองสวิตซ์จะขึ้น Motor1 Motor2 สลับกัน