// ===== PINS (MATCH YOUR WIRING) =====
#define FSR_PIN 0 // Analog in (slider/pot wiper)
#define DIP1_PIN 3 // DIP switch 1 (active LOW)
#define DIP2_PIN 4 // DIP switch 2 (active LOW)
#define DIP3_PIN 5 // DIP switch 3 (active LOW)
// RGB PWM outputs
#define R_PIN 2 // Red
#define G_PIN 6 // Green
#define B_PIN 1 // Blue <-- moved from 7 to 1 for reliable PWM in Wokwi
// ===== TUNING =====
#define FSR_MIN 200
#define FSR_MAX 3500
// Resting brightness: LED glows even with no pressure
#define REST_BRIGHTNESS 40 // try 20–80
// Neutral behavior: all DIP OFF
#define NEUTRAL_OFF true // true = LED off in neutral, false = default green glow
// Multiple DIP switches ON at same time:
// true -> mix colors (R+G=yellow, etc.)
// false -> priority DIP1 > DIP2 > DIP3
#define MIX_COLORS true
static inline int clamp255(int v) {
if (v < 0) return 0;
if (v > 255) return 255;
return v;
}
void setRGB(int r, int g, int b) {
analogWrite(R_PIN, clamp255(r));
analogWrite(G_PIN, clamp255(g));
analogWrite(B_PIN, clamp255(b));
}
void setup() {
Serial.begin(115200);
pinMode(DIP1_PIN, INPUT_PULLUP);
pinMode(DIP2_PIN, INPUT_PULLUP);
pinMode(DIP3_PIN, INPUT_PULLUP);
pinMode(R_PIN, OUTPUT);
pinMode(G_PIN, OUTPUT);
pinMode(B_PIN, OUTPUT);
setRGB(0, 0, 0);
}
void loop() {
// ---- Read FSR ----
int fsrRaw = analogRead(FSR_PIN);
// Clamp
if (fsrRaw < FSR_MIN) fsrRaw = FSR_MIN;
if (fsrRaw > FSR_MAX) fsrRaw = FSR_MAX;
// Map to brightness with resting glow
int brightness = map(fsrRaw, FSR_MIN, FSR_MAX, REST_BRIGHTNESS, 255);
// ---- Read DIP switches (active LOW) ----
bool s1 = (digitalRead(DIP1_PIN) == LOW);
bool s2 = (digitalRead(DIP2_PIN) == LOW);
bool s3 = (digitalRead(DIP3_PIN) == LOW);
// ---- Choose color ----
int r = 0, g = 0, b = 0;
if (!s1 && !s2 && !s3) {
// Neutral
if (NEUTRAL_OFF) {
r = g = b = 0;
} else {
// Default neutral glow color (green)
g = brightness;
}
} else if (MIX_COLORS) {
// Mix mode: any ON switch contributes its channel
if (s1) r = brightness; // DIP1 = Red
if (s2) g = brightness; // DIP2 = Green
if (s3) b = brightness; // DIP3 = Blue
} else {
// Priority mode
if (s1) r = brightness;
else if (s2) g = brightness;
else if (s3) b = brightness;
}
setRGB(r, g, b);
// ---- Debug ----
Serial.print("FSR=");
Serial.print(fsrRaw);
Serial.print(" bright=");
Serial.print(brightness);
Serial.print(" DIP=");
Serial.print(s1); Serial.print(s2); Serial.print(s3);
Serial.print(" RGB=");
Serial.print(r); Serial.print(",");
Serial.print(g); Serial.print(",");
Serial.println(b);
delay(25);
}