//LiquidCrystal_I2C
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
//LiquidCrystal_I2C
// ***** ultrasonic sensor ***** //
const int trigPin1 = 14;
const int echoPin1 = 27;
const int trigPin2 = 26;
const int echoPin2 = 25;
const int trigPin3 = 33;
const int echoPin3 = 32;
// ***** ultrasonic sensor ***** //
// ***** Load Cell ***** //
#include "HX711.h"
HX711 scale;
#define DOUT 13 //(DT)
#define CLK 12 //(SCK)
// ***** Load Cell ***** //
// ***** Global variable ***** //
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
long duration1;
float distanceCm1;
long duration2;
float distanceCm2;
long duration3;
float distanceCm3;
// ***** Global variable ***** //
// Initialize LCD
void initLCD(){
// initialize the LCD
// lcd.begin();
lcd.init();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.setCursor(5, 0); // ไปที่ตัวอักษรที่ 0 แถวที่ 1
lcd.print("Welcome To");
lcd.setCursor(6, 1); // ไปที่ตัวอักษรที่ 6 แถวที่ 2
lcd.print("KnowTechTH");
lcd.setCursor(1, 2); // ไปที่ตัวอักษรที่ 0 แถวที่ 1
lcd.print("SMART INDUSTRY");
lcd.setCursor(1, 3); // ไปที่ตัวอักษรที่ 2 แถวที่ 2
lcd.print("SMART CONTROL BOX");
// initialize the LCD
}
void read_ultrasonic_sensor(){
// Sensor 1
// Clears the trigPin
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration1 = pulseIn(echoPin1, HIGH);
// Calculate the distance
distanceCm1 = duration1 * SOUND_SPEED/2;
// Prints the distance in the Serial Monitor
Serial.print("Distance 1 (cm): ");
Serial.println(distanceCm1);
delay(1000);
// Sensor 2
// Clears the trigPin
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration2 = pulseIn(echoPin2, HIGH);
// Calculate the distance
distanceCm2 = duration2 * SOUND_SPEED/2;
// Prints the distance in the Serial Monitor
Serial.print("Distance 2 (cm): ");
Serial.println(distanceCm2);
delay(1000);
// Sensor 3
// Clears the trigPin
digitalWrite(trigPin3, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration3 = pulseIn(echoPin3, HIGH);
// Calculate the distance
distanceCm3 = duration3 * SOUND_SPEED/2;
// Prints the distance in the Serial Monitor
Serial.print("Distance 3 (cm): ");
Serial.println(distanceCm3);
delay(1000);
}
void setup() {
Serial.begin(115200); // Starts the serial communication
pinMode(trigPin1, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin1, INPUT); // Sets the echoPin as an Input
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(trigPin3, OUTPUT);
pinMode(echoPin3, INPUT);
// Init
initLCD();
// ***** Load Cell ***** //
scale.begin(DOUT, CLK);
// ***** Load Cell ***** //
}
void loop() {
// ***** ultrasonic sensor ***** //
read_ultrasonic_sensor();
// ***** ultrasonic sensor ***** //
// ***** Load Cell ***** //
Serial.println(scale.get_units(), 1);
// ***** Load Cell ***** //
delay(1000);
}