Skip to content

Instantly share code, notes, and snippets.

@tranduydat
Created August 29, 2019 04:12
Show Gist options
  • Save tranduydat/7d8bf3913ae0b0df2a874e4fceec0e83 to your computer and use it in GitHub Desktop.
Save tranduydat/7d8bf3913ae0b0df2a874e4fceec0e83 to your computer and use it in GitHub Desktop.
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// cam bien sieu am config
const unsigned int TRIG_PIN = 7;
const unsigned int ECHO_PIN = 6;
const unsigned int BAUD_RATE = 9600;
const int ledPin = 13;
const int buzzPin = 10;
const int tempSensor = A0;
const int buttonPullPin = 8;
// int statePullUp = 0;
// int statePullUpon = 0;
// int statePullUpoff = 1;
void setup()
{
// set up the LCD's number of columns and rows:
// ultrasonic sensor configures
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(BAUD_RATE);
pinMode(8, INPUT);
}
void loop()
{
Serial.println("---------------------------------------"); // make a line
// int statePullUp = digitalRead(buttonPullPin);
// switch (statePullUp)
// {
// case 1:
// Serial.println("Da bam nut");
// statePullUpon++;
// delay(10);
// case 0:
// Serial.println("Chua bam nut");
// statePullUpoff++;
// delay(10);
// default:
// Serial.println("Kiem tra lai");
// }
Serial.print("State pull on: ");
Serial.print(statePullUpon);
Serial.print("\n");
Serial.print("State pull off: ");
Serial.print(statePullUpoff);
Serial.print("\n");
// cau hinh cam bien nhiet do
int readTemp = analogRead(tempSensor); // doc gia tri tu cam bien LM35
float voltage = readTemp * 5.0 / 1024.0; // tinh gia tri hieu dien the tu gia tri cam bien
float temp = voltage * 100.0;
Serial.print("Nhiet do hien tai: ");
Serial.print(temp);
Serial.print("\n");
lcd.begin(16, 2); // declare this is lcd 16x2
lcd.print("Nhiet do: ");
lcd.print(temp);
lcd.print(" ");
// untrlsonic configures
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
const unsigned long duration = pulseIn(ECHO_PIN, HIGH);
int distance = duration / 29 / 2;
if (duration == 0)
{
Serial.println("Warning: no pulse from sensor");
}
else
{
Serial.print("Khoang cach vat the gan nhat: ");
Serial.print(distance);
Serial.print(" cm\n");
if (distance <= 90)
{
Serial.println("+ Canh bao co nguoi!");
delay(50);
}
else
{
Serial.println("- Khong co nguoi!");
delay(50);
}
}
delay(100);
// cau hinh buzzer
if (distance <= 90)
{
tone(buzzPin, 1200, 1500);
digitalWrite(ledPin, HIGH);
delay(500);
}
else
{
tone(buzzPin, 1200, 300);
digitalWrite(ledPin, LOW);
delay(500); // delay 2s
}
// cau hinh hien thi LCD
if (distance <= 90)
{
lcd.setCursor(0, 1);
lcd.display(); //Hiển thị trở lại
lcd.print(" Co nguoi! ");
delay(500);
}
else
{
lcd.display(); //Hiển thị trở lại
lcd.setCursor(0, 1);
lcd.print(" Khong co nguoi");
delay(500);
}
// In ra dong chu
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment