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

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

 การสร้างนาฬิกาดิจิตอลด้วยบอร์ด Arduino 



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


#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <TM1637Display.h>

// Module connection pins (Digital Pins)
#define CLK 2
#define DIO 3
unsigned long previousMillis;
bool showdot = false;
String SerialGET = "";

TM1637Display display(CLK, DIO);

void setup() {
  Serial.begin(9600);
}

void loop() {
  tmElements_t tm;
  if (!RTC.read(tm)) {
    tm.Hour = 0;
    tm.Minute = 0;
    tm.Second = 0;
    RTC.write(tm);
  }

  display.write(tm.Hour/10, 0);
  display.write(tm.Hour%10, 1);
  display.write(tm.Minute/10, 2);
  display.write(tm.Minute%10, 3);

  // Dot blink
  unsigned long currentMillis = millis();
  if (currentMillis-previousMillis>500){
    previousMillis = currentMillis;
    display.dotShow(showdot=!showdot);
  }

  while (Serial.available()) {
    char c = Serial.read();
    if (c == '\n') {
      int Hour, Min, Sec;
      if (sscanf(SerialGET.c_str(), "%d:%d:%d", &Hour, &Min, &Sec) == 3) {
        tm.Hour = Hour;
        tm.Minute = Min;
        tm.Second = Sec;
        RTC.write(tm);
        Serial.print("Set to ");
        Serial.print(Hour);
        Serial.print(":");
        Serial.print(Min);
        Serial.print(":");
        Serial.print(Sec);
        Serial.println();
      }else{
        Serial.println("Invalid format, Please enter Hour:Min:Sec");
      }
      SerialGET = "";
    }
    else
      SerialGET += c;
  }
}

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

Arduino การใช้DS3231 โมดูลนาฬิกา ตั้งเวลา 




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

#include <Wire.h>
#include <SPI.h>  
#include <RTClib.h>
RTC_DS3231 RTC;
void setup () {
    Serial.begin(9600);
    Wire.begin();
    RTC.begin();    
  RTC.adjust(DateTime(__DATE__, __TIME__));   //จุดนี้เป็นการตั้งเวลา ตั้งครั้งแรกเสร็จแล้วให้ // ไว้ด้วย
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  DateTime now = RTC.now();
  RTC.setAlarm1Simple(2158);  //เป็นการตั้งเวลาปลุก เวลา 22.58 น.
  RTC.turnOnAlarm(1);  //ปลุกช่วงเวลาที่ 1
  if (RTC.checkAlarmEnabled(1)) {
    Serial.println("Alarm Enabled");
  }
}
void loop () {
    DateTime now = RTC.now();
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    if (RTC.checkIfAlarm(1)) {   // เมื่อถึงเวลาให้ทำการปลุกโดยการทำตามเงือนไขใน if
      Serial.println("Alarm Triggered");
    }
    Serial.println();
    delay(3000);
}

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

LDR Photoresistor Sensor Module โมดูลวัดแสง



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

int value = 0;
void setup() {
  Serial.begin(9600);
}
void loop() {
  value = analogRead(A0);
  Serial.print("Raw value: ");Serial.println(value);
  delay(100);
}

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

Arduino การ Based Countdown Timer



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

#include <LiquidCrystal.h>
#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three columns

long int set1;
long int set2;
long int set3;
long int set4;
long int j;

String hours;
String minutes;
String seconds;
// Define the Keymap

char keys[ROWS][COLS] = {

  {'1','2','3','A'},

  {'4','5','6','B'},

  {'7','8','9','C'},

  {'*','0','#','D'}

};

byte rowPins[ROWS] = { 6, 7, 8, 9 };// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins
byte colPins[COLS] = { 10, 11, 12, 13 };// Connect keypad COL0, COL1 and COL2 to t

Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

LiquidCrystal lcd(A0, A1, 5, 4, 3, 2); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

 int t1, t2, t3, t4, t5, t6;
 int r1, r2, r3;

boolean feed = true; // condition for alarm

 char key;
 String r[8];

 void setFeedingTime()
{
  feed = true;
   int i=0;

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Set feeding Time");
  lcd.clear();
  lcd.print("HH:MM:SS");
  lcd.setCursor(0,1);

  
  while(1){
    key = kpd.getKey();

    char j;
    
  if(key!=NO_KEY){
    
    lcd.setCursor(j,1);
    
    lcd.print(key);
    
    r[i] = key-48;
    i++;
    j++;

    if (j==2 || j == 5)
    {
      lcd.print(":"); j++;
    }
    delay(500);
  }

  if (key == 'D')
  {key=0; break; }
  }
 lcd.clear();
}

 void setup() 
 { 
  lcd.begin(16,2);
  Serial.begin(9600);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A3, INPUT);
  pinMode(A4, OUTPUT);
 } 

 void loop() 
 { 
      lcd.setCursor(0,0);
      lcd.print("Arduino Timer");
      //Serial.println(A3);

      if (digitalRead(A3)==1) //
      {
      lcd.clear();
      setFeedingTime(); 
      for(int i = 0; i < 6; i++)  // this for loop is used to get the value of the feeding time and print it serially
      {
       Serial.print(r[i]);
       Serial.println();
      }
      
   hours = String (r[0]) + String (r[1]) ; //combining two separate int values of r[0] and r[1] into one string and save it to "hours" 
   minutes = String (r[2]) + String (r[3]) ;  //combining two separate int values of r[2] and r[3] into one string and save it to "minutes" 
   seconds = String (r[4]) + String (r[5]) ;  //combining two separate int values of r[4] and r[5] into one string and save it to "seconds" 

  
  set1 = (hours.toInt()*3600); //converting hours into seconds
  set2 = (minutes.toInt() * 60);  //converting minutes into seconds
  set3 = seconds.toInt();
  set4 = (hours.toInt() * 3600)+ (minutes.toInt() * 60) + seconds.toInt(); //adding set1, set2 and set3 together in set4
  Serial.print("set4");
  Serial.print(set4);
  Serial.println();

  lcd.setCursor(0,0);
  lcd.print("Countdown begins");
  delay(1000);
  lcd.clear();
  
  for(long int j = set4; j >= 0; j--) // this for loopis used to decrease the total time in seconds
  {
  Serial.println(j);
  
  lcd.setCursor(0,0);
  lcd.print("HH:MM:SS");
  
  long int HH = j / 3600; // converting the remaining time into remaining hours
  lcd.setCursor(0,1);
  Serial.println(HH);
  if (HH < 10) { lcd.print('0');  }
  lcd.print(HH);
  lcd.print(":");
  
  long int MM = (j - (HH*3600))/60 ; //converting the remaining time into remaining minutes
  lcd.setCursor(3,1);
  Serial.println(MM);
  if (MM < 10) { lcd.print('0');  }
  lcd.print(MM);
  lcd.print(":");
  
  long int SS = j - ((HH*3600)+(MM*60)); //converting the remaining time into remaining seconds
  lcd.setCursor(6,1);
  Serial.println(SS);
  if (SS < 10) { lcd.print('0');  } 
  lcd.print(SS);
  delay(1000);

  if (digitalRead(A3)==1){break;} 

   if (j == 0)
    
    {
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("Timer Stop");
      lcd.setCursor(2,1);
      lcd.print("-Ring-Ring-");
      
    for(int k =0; k<= 100; k++) //this for loop is used for the buzzer to beep 100 time as the timer reaches zero
      {
        digitalWrite(A4,HIGH);
        delay(300);
        digitalWrite(A4,LOW);  
        delay(300); 
        if (digitalRead(A3)==1){break;}  
      }
    }
   }
  }

}       

อ้างอิง https://circuitdigest.com

วันอาทิตย์ที่ 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);
}