Created
June 4, 2025 07:16
-
-
Save fxprime/cb86d70a1a19874184331c134754a390 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void loop() { | |
digitalWrite(LED_BUILTIN, HIGH); | |
delay(1000); | |
digitalWrite(LED_BUILTIN, LOW); | |
delay(1000); | |
} | |
unsigned long previousMillis = 0; | |
bool ledState = false; | |
const long interval = 1000; | |
void loop() { | |
unsigned long currentMillis = millis(); | |
if (currentMillis - previousMillis >= interval) { | |
previousMillis = currentMillis; | |
ledState = !ledState; | |
digitalWrite(LED_BUILTIN, ledState); | |
} | |
// ตรงนี้โปรแกรมสามารถทำงานอื่นได้! | |
} | |
// กำหนดตัวแปรสำหรับแต่ละงาน | |
unsigned long previousLedMillis = 0; | |
unsigned long previousSensorMillis = 0; | |
unsigned long previousSerialMillis = 0; | |
const long ledInterval = 500; // LED กระพริบทุก 500ms | |
const long sensorInterval = 2000; // อ่านเซนเซอร์ทุก 2 วินาที | |
const long serialInterval = 10000; // ส่งข้อมูลทุก 10 วินาที | |
bool ledState = false; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(LED_BUILTIN, OUTPUT); | |
pinMode(2, INPUT_PULLUP); // ปุ่มกด | |
} | |
void loop() { | |
unsigned long currentMillis = millis(); | |
// งานที่ 1: LED กระพริบ | |
if (currentMillis - previousLedMillis >= ledInterval) { | |
previousLedMillis = currentMillis; | |
ledState = !ledState; | |
digitalWrite(LED_BUILTIN, ledState); | |
} | |
// งานที่ 2: อ่านเซนเซอร์ | |
if (currentMillis - previousSensorMillis >= sensorInterval) { | |
previousSensorMillis = currentMillis; | |
int sensorValue = analogRead(A0); | |
Serial.print("Sensor: "); | |
Serial.println(sensorValue); | |
} | |
// งานที่ 3: ส่งข้อมูลผ่าน Serial | |
if (currentMillis - previousSerialMillis >= serialInterval) { | |
previousSerialMillis = currentMillis; | |
Serial.println("=== Status Report ==="); | |
} | |
// งานที่ 4: ตรวจสอบปุ่มกด (ทำงานตลอดเวลา) | |
if (digitalRead(2) == LOW) { | |
Serial.println("Button Pressed!"); | |
delay(50); // debounce สั้นๆ ได้ | |
} | |
} | |
// วัดระยะเวลาการตอบสนองแบบแม่นยำ | |
void measureResponseTime() { | |
unsigned long startTime = micros(); | |
// ทำงานที่ต้องการวัดเวลา | |
int result = digitalRead(2); | |
unsigned long endTime = micros(); | |
unsigned long duration = endTime - startTime; | |
Serial.print("Response time: "); | |
Serial.print(duration); | |
Serial.println(" microseconds"); | |
} | |
// วิธีที่ปลอดภัยในการคำนวณเวลา | |
unsigned long previousMillis = 0; | |
const long interval = 1000; | |
void loop() { | |
unsigned long currentMillis = millis(); | |
// วิธีนี้ทำงานได้แม้เกิด overflow | |
if (currentMillis - previousMillis >= interval) { | |
previousMillis = currentMillis; | |
// ทำงานที่ต้องการ | |
Serial.println("Timer triggered!"); | |
} | |
} | |
#include <DHT.h> | |
// กำหนดพิน | |
#define DHT_PIN 2 | |
#define FAN_PIN 3 | |
#define HEATER_PIN 4 | |
#define LED_PIN 13 | |
// กำหนดค่าเซนเซอร์ | |
DHT dht(DHT_PIN, DHT22); | |
// ตัวแปรเวลา | |
unsigned long previousTempRead = 0; | |
unsigned long previousDisplay = 0; | |
unsigned long previousLedBlink = 0; | |
// ช่วงเวลา | |
const long tempReadInterval = 5000; // อ่านอุณหภูมิทุก 5 วินาที | |
const long displayInterval = 10000; // แสดงผลทุก 10 วินาที | |
const long ledBlinkInterval = 1000; // LED กระพริบทุก 1 วินาที | |
// ตัวแปรระบบ | |
float currentTemp = 0; | |
float targetTemp = 25.0; | |
bool ledState = false; | |
bool fanState = false; | |
bool heaterState = false; | |
void setup() { | |
Serial.begin(9600); | |
dht.begin(); | |
pinMode(FAN_PIN, OUTPUT); | |
pinMode(HEATER_PIN, OUTPUT); | |
pinMode(LED_PIN, OUTPUT); | |
Serial.println("=== Temperature Control System ==="); | |
} | |
void loop() { | |
unsigned long currentMillis = millis(); | |
// อ่านค่าอุณหภูมิ | |
if (currentMillis - previousTempRead >= tempReadInterval) { | |
previousTempRead = currentMillis; | |
readTemperature(); | |
controlTemperature(); | |
} | |
// แสดงสถานะระบบ | |
if (currentMillis - previousDisplay >= displayInterval) { | |
previousDisplay = currentMillis; | |
displayStatus(); | |
} | |
// LED แสดงสถานะการทำงาน | |
if (currentMillis - previousLedBlink >= ledBlinkInterval) { | |
previousLedBlink = currentMillis; | |
ledState = !ledState; | |
digitalWrite(LED_PIN, ledState); | |
} | |
// ตรวจสอบคำสั่งผ่าน Serial (ทำงานตลอดเวลา) | |
checkSerialCommands(); | |
} | |
void readTemperature() { | |
float temp = dht.readTemperature(); | |
if (!isnan(temp)) { | |
currentTemp = temp; | |
} | |
} | |
void controlTemperature() { | |
if (currentTemp > targetTemp + 1.0) { | |
// เปิดพัดลม ปิดฮีตเตอร์ | |
fanState = true; | |
heaterState = false; | |
} else if (currentTemp < targetTemp - 1.0) { | |
// เปิดฮีตเตอร์ ปิดพัดลม | |
fanState = false; | |
heaterState = true; | |
} else { | |
// ปิดทั้งคู่ | |
fanState = false; | |
heaterState = false; | |
} | |
digitalWrite(FAN_PIN, fanState); | |
digitalWrite(HEATER_PIN, heaterState); | |
} | |
void displayStatus() { | |
Serial.println("=== Status ==="); | |
Serial.print("Current Temp: "); | |
Serial.print(currentTemp); | |
Serial.println("°C"); | |
Serial.print("Target Temp: "); | |
Serial.print(targetTemp); | |
Serial.println("°C"); | |
Serial.print("Fan: "); | |
Serial.println(fanState ? "ON" : "OFF"); | |
Serial.print("Heater: "); | |
Serial.println(heaterState ? "ON" : "OFF"); | |
Serial.println(); | |
} | |
void checkSerialCommands() { | |
if (Serial.available()) { | |
String command = Serial.readString(); | |
command.trim(); | |
if (command.startsWith("SET")) { | |
float newTarget = command.substring(3).toFloat(); | |
if (newTarget >= 15 && newTarget <= 35) { | |
targetTemp = newTarget; | |
Serial.print("Target temperature set to: "); | |
Serial.println(targetTemp); | |
} | |
} | |
} | |
} | |
// ❌ ผิด: เวลาจะไม่แม่นยำ | |
if (millis() - previousMillis > interval) { | |
previousMillis = millis(); // ผิด! | |
} | |
// ✅ ถูก: เวลาแม่นยำ | |
if (millis() - previousMillis >= interval) { | |
previousMillis += interval; // หรือ previousMillis = millis(); | |
} | |
// ❌ ผิด: ใช้วินาทีกับ millis() | |
const long interval = 5; // 5 วินาที? | |
// ✅ ถูก: ใช้มิลลิวินาที | |
const long interval = 5000; // 5000ms = 5 วินาที | |
// ❌ อาจมีปัญหาเมื่อ overflow | |
if (millis() > previousMillis + interval) { | |
// ✅ ปลอดภัยจาก overflow | |
if (millis() - previousMillis >= interval) { | |
class SimpleTimer { | |
private: | |
unsigned long previousMillis; | |
unsigned long interval; | |
public: | |
SimpleTimer(unsigned long _interval) { | |
interval = _interval; | |
previousMillis = 0; | |
} | |
bool isReady() { | |
unsigned long currentMillis = millis(); | |
if (currentMillis - previousMillis >= interval) { | |
previousMillis = currentMillis; | |
return true; | |
} | |
return false; | |
} | |
void setInterval(unsigned long _interval) { | |
interval = _interval; | |
} | |
}; | |
// การใช้งาน | |
SimpleTimer ledTimer(1000); // 1 วินาที | |
SimpleTimer sensorTimer(5000); // 5 วินาที | |
void loop() { | |
if (ledTimer.isReady()) { | |
// กระพริบ LED | |
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); | |
} | |
if (sensorTimer.isReady()) { | |
// อ่านเซนเซอร์ | |
int sensorValue = analogRead(A0); | |
Serial.println(sensorValue); | |
} | |
} | |
// ใช้กับ WiFi และ Web Server | |
#include <WiFi.h> | |
#include <WebServer.h> | |
WebServer server(80); | |
unsigned long previousWiFiCheck = 0; | |
unsigned long previousDataSend = 0; | |
const long wifiCheckInterval = 30000; // ตรวจ WiFi ทุก 30 วินาที | |
const long dataSendInterval = 60000; // ส่งข้อมูลทุก 1 นาที | |
void loop() { | |
unsigned long currentMillis = millis(); | |
// จัดการ Web Server (ต้องทำงานตลอดเวลา) | |
server.handleClient(); | |
// ตรวจสอบสถานะ WiFi | |
if (currentMillis - previousWiFiCheck >= wifiCheckInterval) { | |
previousWiFiCheck = currentMillis; | |
if (WiFi.status() != WL_CONNECTED) { | |
Serial.println("WiFi disconnected, reconnecting..."); | |
WiFi.reconnect(); | |
} | |
} | |
// ส่งข้อมูลไปยัง Cloud | |
if (currentMillis - previousDataSend >= dataSendInterval) { | |
previousDataSend = currentMillis; | |
sendDataToCloud(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment