int pirPin = 32;
int redPin = 23;
int greenPin = 1;
int bluePin = 3;
int motionDetected = 0;
void setup() {
pinMode(pirPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
}
void loop() {
motionDetected = digitalRead(pirPin);
if (motionDetected == HIGH) {
changeRGBColor();
delay(1000);
}
}
void changeRGBColor() {
for (int i = 0; i < 256; i++) {
analogWrite(redPin, i);
analogWrite(greenPin, 255 - i);
analogWrite(bluePin, 128 - i);
delay(10);
}
}
/*#include <Arduino.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
const char* ssid = "YourSSID"; // ชื่อของเครือข่าย WiFi
const char* password = "YourPassword"; // รหัสผ่านของเครือข่าย WiFi
const int LED_RED_PIN = 37;
const int LED_GREEN_PIN = 35;
const int LED_BLUE_PIN = 34;
AsyncWebServer server(80);
void setup() {
Serial.begin(115200);
pinMode(LED_RED_PIN, OUTPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
pinMode(LED_BLUE_PIN, OUTPUT);
// ตั้งค่าเป็นโหมด AP
WiFi.softAP(ssid, password);
IPAddress IP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(IP);
// กำหนดเส้นทางที่จะเรียกใช้จากหน้าเว็บไซต์
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/html", "<html><body>\
<h1>RGB LED Control</h1>\
<form action=\"/setcolor\" method=\"get\">\
<label for=\"color\">Select Color:</label>\
<select name=\"color\">\
<option value=\"red\">Red</option>\
<option value=\"green\">Green</option>\
<option value=\"yellow\">Yellow</option>\
<option value=\"orange\">Orange</option>\
<option value=\"purple\">Purple</option>\
</select>\
<input type=\"submit\" value=\"Set Color\">\
</form>\
</body></html>");
});
// เมื่อมีคำขอส่งมายัง URL /setcolor
server.on("/setcolor", HTTP_GET, [](AsyncWebServerRequest *request){
String color = request->arg("color");
if (color == "red") {
analogWrite(LED_RED_PIN, 255);
analogWrite(LED_GREEN_PIN, 0);
analogWrite(LED_BLUE_PIN, 0);
} else if (color == "green") {
analogWrite(LED_RED_PIN, 0);
analogWrite(LED_GREEN_PIN, 255);
analogWrite(LED_BLUE_PIN, 0);
} else if (color == "yellow") {
analogWrite(LED_RED_PIN, 255);
analogWrite(LED_GREEN_PIN, 255);
analogWrite(LED_BLUE_PIN, 0);
} else if (color == "orange") {
analogWrite(LED_RED_PIN, 255);
analogWrite(LED_GREEN_PIN, 165);
analogWrite(LED_BLUE_PIN, 0);
} else if (color == "purple") {
analogWrite(LED_RED_PIN, 128);
analogWrite(LED_GREEN_PIN, 0);
analogWrite(LED_BLUE_PIN, 128);
}
request->send(200, "text/html", "LED color set to " + color);
});
// เริ่มเซิร์ฟเวอร์ HTTP
server.begin();
}
void loop() {
// ในกรณีที่ต้องการเพิ่มการควบคุมจาก PIR คุณสามารถเพิ่มโค้ดในส่วนนี้ตามความต้องการ
}
*\