📣ความรู้ไปประยุกต์ใช้อุปกรณ์อิเล็กทรอนิกส์เพื่อสร้างสรรค์เป็นโครงงานตามที่โจทย์กำหนดได้ คำสั่งเขียน
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(32, 16, 2); // Adjust the I2C address if necessary
const int trigPin = 9; // Digital pin connected to the Trig pin of the ultrasonic sensor
const int echoPin = 10; // Digital pin connected to the Echo pin of the ultrasonic sensor
const int buzzerPin = 11; // Digital pin connected to a buzzer
const int ledPin = 13; // Digital pin connected to an LED
const float waterLevelThreshold = 10.0; // Adjust this value based on your tank setup
void setup() {
Serial.begin(9600); // Open serial communication for debugging
lcd.begin(16, 2); // Initialize LCD display
lcd.init();
lcd.setCursor(0, 0);
lcd.backlight();
lcd.display();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// Send ultrasonic signal
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Receive the echo and calculate distance
int duration = pulseIn(echoPin, HIGH);
float distance = duration * 0.034 / 2;
// Display distance on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Distance: ");
lcd.print(distance);
lcd.print(" cm");
// Check water level and trigger actions
if (distance < waterLevelThreshold) {
lcd.setCursor(0, 1);
lcd.print("Low water level");
digitalWrite(buzzerPin, HIGH);
digitalWrite(ledPin, HIGH);
} else {
lcd.setCursor(0, 1);
lcd.print("Normal water level");
digitalWrite(buzzerPin, LOW);
digitalWrite(ledPin, LOW);
}
delay(1000);
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น
ครูเต้ง เทคโนฯ