วันศุกร์ที่ 28 กันยายน พ.ศ. 2561

งานปฏิบัติ 14

Code 1


#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int PIRpin = 8;
void setup() 
{ 
  lcd.begin();
  pinMode(PIRpin,INPUT);
  lcd.home();
  lcd.print("Waiting for PIR");
  delay(10000);       
  lcd.clear();
}
void loop()
{ 
  int x = digitalRead(PIRpin);
  lcd.home();
  lcd.print("PIR = ");
  lcd.print(x);
  delay(100);


}

งานปฏิบัติ 13

Code 1


#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int triggerPin = 13;
int echoPin = 12;
long duration,distCM,temp_distCM=0;
void setup() 
{
  lcd.begin();
  lcd.setCursor(0,0);
  lcd.print("Dist =      cm");
  pinMode(triggerPin,OUTPUT);
  pinMode(echoPin,INPUT);
}
void loop() 
{
  digitalWrite(triggerPin,LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin,LOW);
  duration = pulseIn(echoPin,HIGH);
  distCM = duration/58;
  if (temp_distCM != distCM)
  {
    lcd.setCursor(7,0);
    lcd.print("    ");
  }
  lcd.setCursor(7,0);
  lcd.print(distCM);
  temp_distCM = distCM;
  delay(300);


}


Code 2


#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int triggerPin = 13;
int echoPin = 12;
long duration,distCM;
void setup() 
{
  lcd.begin();
  pinMode(triggerPin,OUTPUT);
  pinMode(echoPin,INPUT);
}
void loop() 
{
  digitalWrite(triggerPin,LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin,LOW);
  duration = pulseIn(echoPin,HIGH);
  distCM = duration/58;
  int x = constrain(distCM,4,19);
  lcd.clear();
  lcd.home();
  for (int i=4;i<=x;i++)
      lcd.print("*");
  delay(150);


}

งานปฏิบัติ 12

Code 1

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int aval,temp_aval=0;
void setup() 
{
  lcd.begin();
  lcd.setCursor(0,0);
  lcd.print("aval = ");
}
void loop() 
{
   aval = analogRead(A0);
   if (temp_aval != aval)
   {
      lcd.setCursor(7,0);
      lcd.print("    ");
   }
   lcd.setCursor(7,0);
   lcd.print(aval);
   temp_aval = aval;
   delay(300);
}




Code 2

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int aval,temp_aval=0;
void setup() 
{
  lcd.begin();
  lcd.setCursor(0,0);
  lcd.print("aval = ");
}
void loop() 
{
   aval = analogRead(A0);
   if (temp_aval != aval)
   {
      lcd.setCursor(7,0);
      lcd.print("    ");
   }
   lcd.setCursor(7,0);
   lcd.print(aval);
   if (aval>400)
   {
      lcd.setCursor(0,1);
      lcd.print("Hi_Pattayatech"); 
   }
   else
   {
      lcd.setCursor(0,1);
      lcd.print("-----"); 
   }
   temp_aval = aval;
   delay(300);
}

งาน ปฎิบัติ 11 เชื่อมต่อโมดูลแส้ง อินฟาเรต



#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int aval,temp_aval=0;
void setup() 
{
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("aval = ");
}
void loop() 
{
   aval = analogRead(A0);
   if (temp_aval != aval)
   {
      lcd.setCursor(7,0);
      lcd.print("    ");
   }
   lcd.setCursor(7,0);
   lcd.print(aval);
   if (aval>400)
   {
      lcd.setCursor(0,1);
      lcd.print("Hi_Pattayatech"); 
   }
   else
   {
      lcd.setCursor(0,1);
      lcd.print("-----"); 
   }
   temp_aval = aval;
   delay(300);
}

งานปฏิบัติ 10

wiring diagram

Code 1

#include <Wire.h>
#include <LiquidCrystal_I2C.h> 
LiquidCrystal_I2C lcd(0x27,16,2);
void setup()
{
  lcd.backlight();
  lcd.begin();//LiquidCrystal_I2C
}
void loop() 
{
  lcd.setCursor(0,0);
  lcd.print("Hello LCD I2C");
  lcd.setCursor(0,1);
  lcd.print("Pattayatech");

}



Code 2

#include <LiquidCrystal_I2C.h> 
LiquidCrystal_I2C lcd(0x27,16,2);
int x=48;
int y=-79;
float z=8.74586; 
void setup()
{
lcd.begin();
}
void loop() 
{
  lcd.setCursor(0,0);
  lcd.print("x = ");
  lcd.print(x);
  lcd.setCursor(8,0);
  lcd.print("y = ");
  lcd.print(y);
  lcd.setCursor(0,1);
  lcd.print("z = ");
  lcd.print(z,4);

}




Code 3

#include <LiquidCrystal_I2C.h> 
LiquidCrystal_I2C lcd(0x27,16,2);
byte heart[8] = {0x00,0x0A,0x1F,0x1F,0x0E,0x04,0x00,0x00};
byte smile[8] = {0x00,0x11,0x00,0x00,0x11,0x0E,0x00,0x00};
void setup() 
{
  lcd.begin();
  lcd.createChar(0,heart);
  lcd.createChar(1,smile);
}
void loop() 
{
  lcd.setCursor(0,0);
  lcd.write(0);
  lcd.setCursor(0,1);
  lcd.write(1);

}



Code 4

#include <LiquidCrystal_I2C.h> 
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() 
{
  lcd.begin();
}
void loop() 
{
  lcd.home();
  lcd.print("ECS");
  for (int i=0;i<13;i++)
  {
    delay(400);
    lcd.scrollDisplayRight();
  }
  for (int i=0;i<13;i++)
  {
    delay(400);
    lcd.scrollDisplayLeft();
  }
}

งานปฎิบัติ 9

BUZZER + KEYPAD
Code 1



#include "LedControl.h"
#include "Keypad.h"
#define C4  262
#define D4  294
#define E4  330
#define F4  349
#define G4  392
#define A4  440
#define B4  494
#define C5  523
char keys[4][4]={
  {7,8,9,0},
  {4,5,6,0},
  {1,2,3,0},
  {0,0,0,0}};
byte rowPins[] = {7,6,5,4};
byte colPins[] = {3,2,1,0};
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,4,4);
int melody[] = {C4,D4,E4,F4,G4,A4,B4,C5};
int dl = 300;
int buzzerpin = 11;
LedControl lc=LedControl(8,10,9,1); 
// Pin 8->DIN, 10->CLK, 9->CS(LOAD), 1 = No.of devices
void setup() 
{
  lc.shutdown(0,false);  
  lc.setIntensity(0,5); 
  lc.clearDisplay(0);
  pinMode(buzzerpin,OUTPUT);
  digitalWrite(buzzerpin,HIGH);
}
void loop() 
{
  char key = keypad.getKey();
  if ((key != NO_KEY) && (key < 9))
  {
    lc.setDigit(0,0,key,false);
    tone(buzzerpin, melody[key-1],dl);
    delay(dl);
    digitalWrite(buzzerpin,HIGH);
  }
}



Code 2

#include "LedControl.h"
#include "Keypad.h"
#define A3  220
#define B3  247
#define C4  262
#define D4  294
#define E4  330
#define F4  349
#define G4  392
#define A4  440
#define ST  0
int melody[] = {G4,G4,G4,G4,G4,G4,E4,D4,E4,G4,C4,G4,E4,
D4,E4,C4,D4,C4,A3,C4,C4,C4,A3,C4,ST,C4,A3,C4,C4,C4,A3,
C4,ST,G4,A4,G4,E4,D4,C4,ST};
float beats[] ={0.5,0.5,0.5,0.5,1.5,0.5,0.5,0.5,0.5,0.5,
1.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,
0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,0.5,1,1,1,1};
int buzzerpin = 11;
int timestop = 70;
char keys[4][4]={
  {'7','8','9','0'},
  {'4','5','6','0'},
  {'1','2','3','0'},
  {'0','0','0','0'}};
byte rowPins[] = {7,6,5,4};
byte colPins[] = {3,2,1,0};
Keypad keypad = Keypad(makeKeymap(keys),rowPins,colPins,4,4);
LedControl lc=LedControl(8,10,9,1); 
// Pin 8->DIN, 10->CLK, 9->CS(LOAD), 1 = No.of devices
int m=1;
int num[2]; //num[0] to num[1]
char number;
int pw;
void setup() 
{
  lc.shutdown(0,false);  
  lc.setIntensity(0,5); 
  lc.clearDisplay(0);    
  pinMode(buzzerpin,OUTPUT);
  digitalWrite(buzzerpin,HIGH);
  randomSeed(analogRead(0));
  pw = random(100);
}
void loop() 
{
  char key = keypad.getKey();
  if (key != NO_KEY)
  {
     if (m==1)
        lc.clearDisplay(0); 
     lc.setChar(0,m,key,false);
     num[m]=key;
     m = m-1;
     if (m < 0)
     {
          delay(500);
          number = ((num[1]-48)*10)+(num[0]-48);
          if (number == pw)
          {
            lc.setChar(0,7,'0',false);
            lc.setChar(0,6,'H',false);
            int dl = 350;
            int numnote;
            numnote = sizeof(melody)/2;  
            for (int i=0;i<numnote;i++) 
            {
              if (melody[i]>0)
                tone(buzzerpin, melody[i],dl*beats[i]);
              delay(dl*beats[i]);
              digitalWrite(buzzerpin,HIGH);
              delay(timestop);
            }
          }
          else if (number < pw)
          {
            lc.setChar(0,7,'P',false);
            lc.setChar(0,6,'P',false);
          }
          else if (number > pw)
          {
            lc.setChar(0,7,'L',false);
            lc.setChar(0,6,'L',false);
          }
          m = 1;
     }
  }
}

งาน ปฎิบัติ 8

Code 01
#include "LedControl.h"
#define C4  262
#define D4  294
#define E4  330
#define F4  349
#define G4  392
#define A4  440
#define B4  494
#define C5  523
int melody[] = {C4,D4,E4,F4,G4,A4,B4,C5};
float beats[] ={1,1,1,1,1,1,1,1};
int buzzerpin = 11;
int timestop = 70;
LedControl lc=LedControl(8,10,9,1);
// Pin 8->DIN, 10->CLK, 9->CS(LOAD), 1 = No.of devices
void setup()
{
  lc.shutdown(0,false);
  lc.setIntensity(0,5);
  lc.clearDisplay(0);
  int dl = 500;
  pinMode(buzzerpin,OUTPUT);
  int numnote;
  numnote = sizeof(melody)/2;
  for (int i=0;i<numnote;i++)
  {
    lc.setChar(0,7-i,'-',false);
    tone(buzzerpin, melody[i],dl*beats[i]);
    delay(dl*beats[i]);
    digitalWrite(buzzerpin,HIGH);
    delay(timestop);
  }
}
void loop()
{
}


Code 02

#include "LedControl.h"
#define C4  262
#define D4  294
#define E4  330
#define F4  349
#define FS4 370
#define G4  392
#define A4  440
#define B4  494
#define C5  523
#define D5  587
int melody[] = {D4,D4,E4,D4,G4,FS4,D4,D4,E4,D4,A4,G4,D4,D4,D5,
  B4,G4,FS4,E4,C5,C5,B4,G4,A4,G4};
float beats[] ={0.5,0.5,1,1,1,2,0.5,0.5,1,1,1,2,0.5,0.5,1,1,
  1,1,1,0.5,0.5,1,1,1,2};
int buzzerpin = 11;
int timestop = 70;
LedControl lc=LedControl(8,10,9,1);
// Pin 8->DIN, 10->CLK, 9->CS(LOAD), 1 = No.of devices
void setup()
{
  lc.shutdown(0,false);
  lc.setIntensity(0,5);
  lc.clearDisplay(0);
  int dl = 400;
  pinMode(buzzerpin,OUTPUT);
  lc.setChar(0,7,'H',false);
  lc.setChar(0,6,'b',false);
  lc.setChar(0,5,'d',false);
  int numnote;
  numnote = sizeof(melody)/2;
  for (int i=0;i<numnote;i++)
  {
    tone(buzzerpin, melody[i],dl*beats[i]);
    delay(dl*beats[i]);
    digitalWrite(buzzerpin,HIGH);
    delay(timestop);
  }
}
void loop()
{}

Code 03

#include "LedControl.h"
#define C4  262
#define D4  294
#define E4  330
#define F4  349
#define G4  392
int melody[] = {E4,D4,C4,D4,E4,E4,E4,D4,D4,D4,E4,G4,G4,
                E4,D4,C4,D4,E4,E4,E4,D4,D4,E4,D4,C4};
float beats[] ={1,1,1,1,1,1,2,1,1,2,1,1,2,1,
                1,1,1,1,1,2,1,1,1,1,4};
int buzzerpin = 11;
int timestop = 70;
LedControl lc=LedControl(8,10,9,1); 
// Pin 8->DIN, 10->CLK, 9->CS(LOAD), 1 = No.of devices
void setup() 
{
  lc.shutdown(0,false);  
  lc.setIntensity(0,5); 
  lc.clearDisplay(0);
  int dl = 250;
  pinMode(buzzerpin,OUTPUT);
  int numnote;
  numnote = sizeof(melody)/2;  
  for (int i=0;i<numnote;i++) 
  {
    lc.setDigit(0,0,beats[i],false);
    tone(buzzerpin, melody[i],dl*beats[i]);
    delay(dl*beats[i]);
    digitalWrite(buzzerpin,HIGH);
    delay(timestop);
  }
}
void loop() 
{
}

งานแก้ไข แยก อธิบายส่วนต่างๆของproject

Hardware 1.Arduino uno r3 2.bluetooth hc 06 3.1N4007 4.2N2222 5.relay 6.load INPUT 1.bluetooth hc 06 OUTPUT 1.relay 2.lo...