#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_NeoPixel.h>
#define LED_PIN 5
#define NUM_LEDS 256
#define MATRIX_WIDTH 16
#define MATRIX_HEIGHT 16
Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
// WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// URL Google Apps Script (Web App)
const char* sheet_url = "https://script.google.com/macros/s/AKfycbyLyh9_DTxnWRDFIOeGp-r2Th-WYqthcq9LTId0zAZFrcDULayurR27Qak4bQcr3fum/exec";
// ตัวแปรจาก Google Sheet
int B2_pattern = 4;
int B3_red = 0;
int B4_green = 50;
int B5_blue = 255;
// ---------------- Map XY → index (Snake wiring) ----------------
int getIndex(int x, int y) {
if (y % 2 == 0) {
// แถวคู่: ซ้าย → ขวา
return y * MATRIX_WIDTH + x;
} else {
// แถวคี่: ขวา → ซ้าย
return y * MATRIX_WIDTH + (MATRIX_WIDTH - 1 - x);
}
}
void setup() {
Serial.begin(115200);
strip.begin();
strip.show();
Serial.println("🔌 Connecting WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n✅ WiFi connected!");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
Serial.println("🌐 Sending GET request...");
http.begin(sheet_url);
int httpCode = http.GET();
if (httpCode == 200) {
String payload = http.getString();
Serial.println("📥 Raw payload:");
Serial.println(payload);
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, payload);
if (!error) {
Serial.println("✅ JSON parse success!");
// อ่านค่าจาก JSON (ถ้าว่าง → ใช้ default)
B2_pattern = doc["B2"].as<int>();
B3_red = doc["B3"].as<int>();
B4_green = doc["B4"].as<int>();
B5_blue = doc["B5"].as<int>();
// ถ้าค่าเป็น 0 หรือไม่ได้กำหนด → ใส่ default
if (B2_pattern < 1 || B2_pattern > 4) B2_pattern = 1;
if (B3_red <= 0) B3_red = 255;
if (B4_green < 0) B4_green = 0;
if (B5_blue < 0) B5_blue = 0;
Serial.printf("🎨 Pattern=%d | R=%d, G=%d, B=%d\n",
B2_pattern, B3_red, B4_green, B5_blue);
} else {
Serial.print("❌ JSON parse failed: ");
Serial.println(error.c_str());
}
} else {
Serial.printf("❌ HTTP GET failed, code: %d\n", httpCode);
}
http.end();
} else {
Serial.println("⚠️ WiFi disconnected!");
}
// 🔄 เลือก Pattern
switch(B2_pattern){
case 1: moveUp(); break;
case 2: moveDown(); break;
case 3: moveLeft(); break;
case 4: moveRight(); break;
default: solidColor(); break;
}
delay(1000); // refresh ทุก 1 วิ
}
// ---------------- Animation ----------------
void moveUp(){
for(int y=0; y<MATRIX_HEIGHT; y++){
for(int x=0; x<MATRIX_WIDTH; x++){
int i = getIndex(x,y);
strip.setPixelColor(i, strip.Color(B3_red,B4_green,B5_blue));
}
strip.show();
delay(100);
strip.clear();
}
}
void moveDown(){
for(int y=MATRIX_HEIGHT-1; y>=0; y--){
for(int x=0; x<MATRIX_WIDTH; x++){
int i = getIndex(x,y);
strip.setPixelColor(i, strip.Color(B3_red,B4_green,B5_blue));
}
strip.show();
delay(100);
strip.clear();
}
}
void moveLeft(){
for(int x=0; x<MATRIX_WIDTH; x++){
for(int y=0; y<MATRIX_HEIGHT; y++){
int i = getIndex(x,y);
strip.setPixelColor(i, strip.Color(B3_red,B4_green,B5_blue));
}
strip.show();
delay(100);
strip.clear();
}
}
void moveRight(){
for(int x=MATRIX_WIDTH-1; x>=0; x--){
for(int y=0; y<MATRIX_HEIGHT; y++){
int i = getIndex(x,y);
strip.setPixelColor(i, strip.Color(B3_red,B4_green,B5_blue));
}
strip.show();
delay(100);
strip.clear();
}
}
void solidColor(){
for(int i=0; i<strip.numPixels(); i++){
strip.setPixelColor(i, strip.Color(B3_red,B4_green,B5_blue));
}
strip.show();
}