#include "FastLED.h"
#define NUM_LEDS_PER_STRIP 12
#define BUTTON_PIN 8 // Define the pin for the button
CRGB strip1[NUM_LEDS_PER_STRIP];
CRGB strip2[NUM_LEDS_PER_STRIP];
int PWM; // Channel 1 input variable
int p = 0;
int position = 0; // Position of the moving light
int direction = 1; // Direction of movement (1 for forward, -1 for backward)
unsigned long lastButtonPress = 0; // To store the time of the last button press
void setup() {
pinMode(7, INPUT); // Read the pulse width at pin 7
pinMode(BUTTON_PIN, INPUT_PULLUP); // Initialize the button pin with a pull-up resistor
FastLED.addLeds<NEOPIXEL, 10>(strip1, NUM_LEDS_PER_STRIP);
FastLED.addLeds<NEOPIXEL, 11>(strip2, NUM_LEDS_PER_STRIP);
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
// Uncomment the method you want to use for testing:
// 1. To manually cycle through patterns using a button:
cyclePatternsManually();
// 2. To automatically cycle through patterns with a delay:
// cyclePatternsAutomatically(3000); // 3000 milliseconds = 3 seconds
// Existing code to read PWM signal
PWM = pulseIn(7, HIGH, 25000); // Read the pulse width of channel 1
Serial.print("Channel 1:"); // Print the value of channel 1
Serial.println(PWM); // each channel
// Display the pattern based on the current value of p
if (p == 1) {
Pattern1(255, 0, 0, 100, 1);
Pattern1(0, 255, 0, 100, 1);
Pattern1(0, 0, 255, 100, 1);
}
if (p == 2) {
Pattern2(255, 0, 0, 100, 1);
Pattern2(0, 255, 0, 100, 1);
Pattern2(0, 0, 255, 100, 1);
}
if (p == 3) {
Pattern3(100);
}
if (p == 4) {
Pattern4(255, 0, 0, 4, 0, 0, 255, 100);
Pattern4(255, 0, 0, 4, 0, 255, 0, 100);
Pattern4(0, 255, 0, 4, 0, 100, 0, 100);
}
if (p == 5) {
Pattern5(3);
}
if (p == 6) {
Pattern6(100); // Call the new Knight Rider pattern
}
}
// Function to automatically cycle through patterns every specified interval
void cyclePatternsAutomatically(unsigned long interval) {
static unsigned long lastChange = 0; // Track the last time the pattern changed
if (millis() - lastChange > interval) {
lastChange = millis();
p++; // Move to the next pattern
if (p > 6) p = 1; // Loop back to the first pattern after the last
}
}
// Function to manually cycle through patterns using a button
void cyclePatternsManually() {
if (digitalRead(BUTTON_PIN) == LOW) { // Check if the button is pressed
if (millis() - lastButtonPress > 200) { // Debounce the button press
lastButtonPress = millis();
p++; // Increment the pattern counter
if (p > 6) p = 1; // Loop back to the first pattern after the last
}
}
}
void Pattern1(byte red, byte green, byte blue, int speedDelay, int eye) {
// Existing Pattern1 code
// ...
}
void Pattern2(byte red, byte green, byte blue, int speedDelay, int eye) {
// Existing Pattern2 code
// ...
}
void Pattern3(int speedDelay) {
// Existing Pattern3 code
// ...
}
void Pattern4(byte red, byte green, byte blue, int postion, byte color1, byte color2, byte color3, int speedDelay) {
// Existing Pattern4 code
// ...
}
void Pattern5(int pos) {
// Existing Pattern5 code
// ...
}
// New Pattern6 function: Knight Rider effect
void Pattern6(int speedDelay) {
// Clear the strips
fill_solid(strip1, NUM_LEDS_PER_STRIP, CRGB::Black);
fill_solid(strip2, NUM_LEDS_PER_STRIP, CRGB::Black);
// Determine which strip to light up based on the current position
if (position < NUM_LEDS_PER_STRIP) {
strip1[position] = CRGB::Red; // Light up strip1
} else {
strip2[position - NUM_LEDS_PER_STRIP] = CRGB::Red; // Light up strip2
}
// Show the LEDs
FastLED.show();
delay(speedDelay);
// Turn off the previous LED
if (position < NUM_LEDS_PER_STRIP) {
strip1[position] = CRGB::Black;
} else {
strip2[position - NUM_LEDS_PER_STRIP] = CRGB::Black;
}
// Update the position for the next frame
position += direction;
// Reverse direction if we reach the end of the combined strips
if (position == (NUM_LEDS_PER_STRIP * 2) - 1 || position == 0) {
direction = -direction;
}
}