#include <Adafruit_NeoPixel.h>
// Define pins
#define WATER_PUMP_PIN 3
#define LED_STRIP_PIN 5
#define MODE_BUTTON_PIN 7 // Example pin for mode selection button
#define INCREASE_BUTTON_PIN 8 // Button to increase sync mode interval
#define DECREASE_BUTTON_PIN 9 // Button to decrease sync mode interval
// Define the number of LEDs in the strip
#define NUM_LEDS 10
// Define the NeoPixel object
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_STRIP_PIN, NEO_GRB + NEO_KHZ800);
// Timing variables
unsigned long previousPumpMillis = 0;
unsigned long previousRedMillis = 0;
unsigned long previousBlueMillis = 0;
unsigned long previousGreenMillis = 0;
const unsigned long pumpIntervalOn = 125; // Water pump on time in milliseconds
const unsigned long pumpIntervalOff = 250; // Water pump off time in milliseconds
// Timing intervals for strobing mode
const unsigned long redInterval = 250; // Red LED interval in milliseconds
const unsigned long blueInterval = 350; // Blue LED interval in milliseconds
const unsigned long greenInterval = 150; // Green LED interval in milliseconds
// Timing intervals for synchronized mode and user-defined color mode
unsigned long syncModeInterval = 200; // Interval for synchronized mode and user-defined color mode
// Mode variables
int currentMode = 0; // Variable to track the current mode
// Button state variables
bool modeButtonState = false;
bool increaseButtonState = false;
bool decreaseButtonState = false;
void setup() {
pinMode(WATER_PUMP_PIN, OUTPUT);
pinMode(MODE_BUTTON_PIN, INPUT); // Assuming a button to change modes
pinMode(INCREASE_BUTTON_PIN, INPUT); // Button to increase interval
pinMode(DECREASE_BUTTON_PIN, INPUT); // Button to decrease interval
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
unsigned long currentMillis = millis();
// Check for mode change button press
bool newModeButtonState = digitalRead(MODE_BUTTON_PIN) == LOW;
if (newModeButtonState && !modeButtonState) {
currentMode = (currentMode + 1) % 3; // Cycle through 0, 1, 2 for modes
modeButtonState = true;
} else if (!newModeButtonState) {
modeButtonState = false;
}
// Check for interval increase button press
bool newIncreaseButtonState = digitalRead(INCREASE_BUTTON_PIN) == LOW;
if (newIncreaseButtonState && !increaseButtonState) {
syncModeInterval += 1; // Increase interval by 50 milliseconds
increaseButtonState = true;
} else if (!newIncreaseButtonState) {
increaseButtonState = false;
}
// Check for interval decrease button press
bool newDecreaseButtonState = digitalRead(DECREASE_BUTTON_PIN) == LOW;
if (newDecreaseButtonState && !decreaseButtonState) {
if (syncModeInterval > 50) {
syncModeInterval -= 1; // Decrease interval by 50 milliseconds, with a minimum of 50
}
decreaseButtonState = true;
} else if (!newDecreaseButtonState) {
decreaseButtonState = false;
}
// Mode selection based on currentMode
switch (currentMode) {
case 0:
// Strobing mode with precise timings
strobeColor(255, 0, 0, redInterval); // Red
strobeColor(0, 0, 255, blueInterval); // Blue
strobeColor(0, 255, 0, greenInterval); // Green
break;
case 1:
// Synchronized mode
strobeSyncedColor(255, 255, 255); // Red
break;
case 2:
// User-defined color mode (example with purple)
strobeColor(148, 0, 211, syncModeInterval); // Purple (example)
break;
}
// Water pump control
if (currentMillis - previousPumpMillis >= pumpIntervalOff) {
digitalWrite(WATER_PUMP_PIN, HIGH);
previousPumpMillis = currentMillis;
}
// Turn off the pump after pumpIntervalOn time
if (currentMillis - previousPumpMillis >= pumpIntervalOn) {
digitalWrite(WATER_PUMP_PIN, LOW);
}
}
void strobeColor(uint8_t red, uint8_t green, uint8_t blue, unsigned long interval) {
unsigned long currentMillis = millis();
if (currentMillis - previousRedMillis >= interval) {
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(red, green, blue));
}
strip.show();
delay(50); // Delay for LED visibility
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 0);
}
strip.show();
previousRedMillis = currentMillis;
}
}
void strobeSyncedColor(uint8_t red, uint8_t green, uint8_t blue) {
unsigned long currentMillis = millis();
if (currentMillis - previousRedMillis >= syncModeInterval) {
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, strip.Color(red, green, blue));
}
strip.show();
delay(50); // Delay for LED visibility
for (int i = 0; i < NUM_LEDS; i++) {
strip.setPixelColor(i, 0);
}
strip.show();
previousRedMillis = currentMillis;
}
}