Skip to content

Instantly share code, notes, and snippets.

@penpencool
penpencool / bin.ino
Last active September 28, 2024 09:18
#include <Servo.h>
/*
การต่อขา Ultrasonic - Uno
VCC - 5V
GND - GND
Trig - 9
Echo - 8
การต่อขา Servo - Uno
สีแดง - 5V
const int relayPin = 8; // ขาที่ relay ต่อกับ Arduino
const int switchPin = 13; // ขาที่สวิตช์ต่อกับ Arduino
bool isPressed = false; // ตัวแปรเพื่อตรวจสอบการกดของสวิตช์
void setup() {
pinMode(relayPin, OUTPUT); // กำหนด relayPin เป็น OUTPUT
pinMode(switchPin, INPUT_PULLUP); // กำหนด switchPin เป็น INPUT พร้อมใช้ค่า resistance ภายใน
digitalWrite(relayPin, LOW); // กำหนดให้ relay ปิดอยู่ในตอนเริ่มต้น
}
int relay = 7; // กำหนดขาที่ใช้งาน Relay
void setup() {
pinMode(relay, OUTPUT); // กำหนดให้ขาที่ใช้ Relay เป็น Output
}
void loop() {
digitalWrite(relay, HIGH); // สั่งให้ Relay ทำงาน
delay(1000); // รอ 1 วินาที
digitalWrite(relay, LOW); // สั่งให้ Relay หยุดทำงาน
// ตัวแปรสำหรับเซนเซอร์วัดความชื้นและ LED
const int soilSensor = A0; // ต่อเซนเซอร์วัดความชื้นกับขา A0
const int ledPin = 13; // ต่อ LED กับขา 13
// ตัวแปรสำหรับกำหนดค่าความชื้นที่ต้องการ
const int moistureThreshold = 900; // ค่าความชื้นที่ต้องการ, สามารถปรับเปลี่ยนได้ตามต้องการ
void setup() {
pinMode(ledPin, OUTPUT); // ตั้งค่า LED เป็น OUTPUT
Serial.begin(9600); // เริ่มต้นการสื่อสารผ่าน Serial
int sensorPin = A0; // กำหนด pin ของเซนเซอร์
int sensorValue = 0; // ตัวแปรเพื่อเก็บค่าจากเซนเซอร์
void setup() {
Serial.begin(9600); // เริ่มต้นการสื่อสารผ่าน Serial
}
void loop() {
sensorValue = analogRead(sensorPin); // อ่านค่าจากเซนเซอร์
Serial.println(sensorValue); // แสดงค่าผ่าน Serial
const int ledPin = 13; // ต่อ LED ที่ pin 13 ของ Arduino
const int sensorPin = A0; // ต่อเซนเซอร์ TCRT5000L ที่ pin A0 ของ Arduino
const int threshold = 10; // ค่า threshold สำหรับตรวจจับว่ามีการบังแสงหรือไม่
bool ledState = false; // สถานะปัจจุบันของ LED
bool previousState = false; // สถานะก่อนหน้านี้ของการตรวจจับ
void setup() {
pinMode(ledPin, OUTPUT); // ตั้ง pin ของ LED เป็น OUTPUT
pinMode(sensorPin, INPUT); // ตั้ง pin ของเซนเซอร์เป็น INPUT
#define SENSOR_PIN A0
#define THRESHOLD 12
void setup() {
pinMode(SENSOR_PIN, INPUT);
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(SENSOR_PIN);
#define WATER_SENSOR_PIN 2 // กำหนดให้เซนเซอร์ตรวจจับน้ำต่อที่ pin 2
#define BUZZER_PIN 3 // กำหนดให้ buzzer ต่อที่ pin 3
void setup() {
pinMode(WATER_SENSOR_PIN,INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
digitalWrite(BUZZER_PIN, LOW);
}
void loop() {
//ขาสัญญาณ PWM สำหรับควบคุมมอเตอร์
#define RPWM 5
#define LPWM 6
void setup() {
pinMode(RPWM, OUTPUT);
pinMode(LPWM, OUTPUT);
// สั่งให้มอเตอร์หยุด
#include "Adafruit_VL53L1X.h"
#define IRQ_PIN 2
#define XSHUT_PIN 3
Adafruit_VL53L1X vl53 = Adafruit_VL53L1X(XSHUT_PIN, IRQ_PIN);
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);