#include <FastLED.h>
#define LED_PIN 12
#define NUM_LEDS 1
CRGB leds[NUM_LEDS];
// Joystick 接腳
const int xPin = A4; // HORZ
const int yPin = A5; // VERT
void setup() {
FastLED.addLeds<WS2811, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.clear();
FastLED.show();
}
void loop() {
int xVal = analogRead(xPin);
int yVal = analogRead(yPin);
int x = xVal - 512;
int y = yVal - 512;
const int threshold = 100;
byte r = 0, g = 0, b = 0;
if (abs(x) < threshold && abs(y) < threshold) {
r = g = b = 0; // 中間,全暗
} else {
if (x < -threshold && abs(y) < threshold) {
r = map(abs(x), threshold, 512, 0, 255); // 左 = 紅
} else if (x > threshold && abs(y) < threshold) {
b = map(abs(x), threshold, 512, 0, 255); // 右 = 藍
}
if (y > threshold && abs(x) < threshold) {
g = map(abs(y), threshold, 512, 0, 255); // 上 = 綠
}
if (x < -threshold && y > threshold) {
r = map(abs(x), threshold, 512, 0, 255);
g = map(abs(y), threshold, 512, 0, 255); // 左上 = 紅+綠
}
if (x > threshold && y > threshold) {
b = map(abs(x), threshold, 512, 0, 255);
g = map(abs(y), threshold, 512, 0, 255); // 右上 = 藍+綠
}
}
leds[0] = CRGB(r, g, b);
FastLED.show();
delay(10);
}