วันอาทิตย์ที่ 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 ตัวเลขจะเพิ่มมากน้อยขึ้นอยู่กับการปรับตัวต้านทาน