#include <Adafruit_NeoPixel.h>
#define LED_PIN 6 // The pin where your LED strip is connected
#define TOUCH_PIN 2 // The pin where your touch sensor is connected
#define SWITCH_PIN 3 // The pin where your switch is connected
#define NUM_LEDS 32 // Number of LEDs in your strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
bool ledState = false; // Track whether LEDs are on or off
bool lastTouchState = false; // Track the last state of the touch sensor
bool lastSwitchState = true; // Track the last state of the switch (true because of INPUT_PULLUP)
int brightness = 0; // Current brightness level (0-255)
int touchMode = 0; // Track the current mode of the touch sensor (0 to 3)
const int brightnessLevels[] = {0, 102, 178, 255}; // Brightness levels for each mode (0%, 40%, 70%, 100%)
void setup() {
strip.begin();
strip.show(); // Initialize all pixels to 'off'
pinMode(TOUCH_PIN, INPUT);
pinMode(SWITCH_PIN, INPUT_PULLUP);
}
int ct=0;
int cs=0;
void loop() {
bool currentTouchState = digitalRead(TOUCH_PIN);
bool currentSwitchState = !digitalRead(SWITCH_PIN); // Invert because of INPUT_PULLUP
ct = currentTouchState;
cs = currentSwitchState;
// Check for switch change
if (currentSwitchState != lastSwitchState) {
ledState = currentSwitchState; // Set LED state to match switch
brightness = ledState ? 255 : 0; // Set brightness based on ledState
touchMode = 0; // Reset touch mode
updateLEDs();
}
// Check for touch sensor change (rising edge) if switch is off
if (!currentSwitchState && currentTouchState && !lastTouchState) {
ledState = !ledState; // Toggle LED state
brightness = ledState ? 255 : 0; // Set brightness based on ledState
updateLEDs();
}
lastTouchState = currentTouchState;
lastSwitchState = currentSwitchState;
delay(1); // Small delay for responsiveness
}
void updateLEDs() {
if (ledState) {
// Turn on LEDs to warm white with current brightness
for(int i=0; i<NUM_LEDS; i++) {
uint8_t r = 255 * brightness / 255;
uint8_t g = 147 * brightness / 255;
uint8_t b = 41 * brightness / 255;
strip.setPixelColor(i, strip.Color(r, g, b));
}
} else {
// Turn off all LEDs
strip.clear();
}
strip.show(); // Update the strip
}