You are on page 1of 7

https://www.tinkercad.com/things/50B8IUvN1yM-copy-of-copy-of-hungndhe172333/editel?

sharecode=hAponoCAp-UsdDX1mUcGMTXeXgq0AzCphBTMkIPsoC0

//------------- Thêm thư viện ---------


#include <LiquidCrystal.h>
LiquidCrystal lcd_1(9, 8, 7, 6, 5,4);

//-------- dịnh nghĩa đổi tên chân cổng --------


#define ledR 13
#define ledY 12
#define ledG 11

#define bt1 3
#define bt2 2

#define bientro A0
#define quangtro A1

//---------- Khai báo biến toàn cục ---------


int flag=0;
int flag2=0;
int mode=6;

int h=12, m=30, s=45;


int sh=10, sm=45, ss=30;
//----------- các chương trình con ----------
void kieunhay1()
{
}
void kieunhay2()
{
}

void kieunhay3()
{
}
//---------- cài đặt ban đầu ----------
void setup()
{
pinMode(ledR, OUTPUT);
pinMode(ledY, OUTPUT);
pinMode(ledG, OUTPUT);

pinMode(bt1, INPUT_PULLUP);
pinMode(bt2, INPUT_PULLUP);

Serial.begin(9600);
lcd_1.begin(16, 2);
}

//------------------- chương trình chính -------------------


void loop()
{
//-------------B1: thu thập dữ liệu------------
int val_bt1 = digitalRead(bt1);
int val_bt2 = digitalRead(bt2);

int val_bientro = analogRead(bientro);


int val_quangtro = analogRead(quangtro);
//-------------B2: xử lý, chống nhiễu, truyền thông -------
// Xử lý lệnh truyên thông
if ( Serial.available() > 0 )
{
String command = Serial.readStringUntil('/'); // *mode=0/

if (command== "*mode=0" ) mode = 0;


if (command== "*mode=1" ) mode = 1;
if (command== "*mode=2" ) mode = 2;
if (command== "*mode=3" ) mode = 3;
if (command== "*mode=4" ) mode = 4;
if (command== "*mode=5" ) mode = 5;
if (command== "*mode=6" ) mode = 6;
}
// B2.0 xử lý thơi gian thực
s++; if (s>=60) s=0; //delay(300);

// B2.1: chống nhiễu nút MODE


if (val_bt1==0)
{
delay(1);
if (digitalRead(bt1) == 0) flag2=1;
}
if (flag2==1 & val_bt1==1)
{
delay(1);
if (digitalRead(bt1) == 1)
{
mode++; if(mode>=7) mode=0;
Serial.println(mode);
flag2=0;
}
}
//B2.2: quét nút UP

//-------------B3: đk đầu ra, led, loa, động cơ, lcd-------


//B3.1: Giao diện LCD
if (mode==0)
{
lcd_1.setCursor(0, 0);
lcd_1.print("Time: ");
lcd_1.print(h);lcd_1.print(':');
lcd_1.print(m);lcd_1.print(':');
lcd_1.print(s);

lcd_1.setCursor(0, 1);
lcd_1.print("SetT: ");
lcd_1.print(sh);lcd_1.print(':');
lcd_1.print(sm);lcd_1.print(':');
lcd_1.print(ss);
}
if (mode==1)
{
lcd_1.setCursor(0, 0);
lcd_1.print("mode1: ");
lcd_1.setCursor(0, 1);
lcd_1.print(" ");
}
if (mode==2)
{
lcd_1.setCursor(0, 0);
lcd_1.print("mode2: ");
lcd_1.setCursor(0, 1);
lcd_1.print(" ");
}
if (mode==3)
{
lcd_1.setCursor(0, 0);
lcd_1.print("mode3: ");
lcd_1.setCursor(0, 1);
lcd_1.print(" ");
}
if (mode==4)
{
lcd_1.setCursor(0, 0);
lcd_1.print("mode4: ");
lcd_1.setCursor(0, 1);
lcd_1.print(" ");
}

if (mode==5)
{
lcd_1.setCursor(0, 0);
lcd_1.print("ADC0: ");
lcd_1.setCursor(0, 1);
lcd_1.print(" hello ");
delay(1);
lcd_1.setCursor(6, 0);
lcd_1.print(val_bientro);

float vol = (float)val_bientro * 5.0 / 1023.0;


lcd_1.setCursor(11, 0);
lcd_1.print(vol);

delay(10);

Serial.print(val_bientro);
Serial.print(' ');
Serial.println(vol);

analogWrite(11,val_bientro/4);
}
if (mode==6)
{

String line1 = "Dang Thanh Trung";


String line2 = "HE172400 ";
line2 = line2 + String(val_quangtro);
lcd_1.setCursor(0, 0);
lcd_1.print(line1);
lcd_1.setCursor(0, 1);
lcd_1.print(line2);

Serial.println(val_quangtro);

//if (val_quangtro<250) digitalWrite(13,1);


//if (val_quangtro>=2 digitalWrite(13,0);
}
}

You might also like