Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.3 kB
4
Indexable
Never
// Define LED pins
const int led1Pin = 9; // Change to your desired pin number
const int led2Pin = 8; // Change to your desired pin number
const int led3Pin = 7; // Change to your desired pin number

// Define button pin
const int buttonPin = 10; // Change to your desired pin number

// Define variables
int mode = 0; // Current LED effect mode
int lastButtonState = HIGH; // Previous state of the button
int buttonState; // Current state of the button
unsigned long lastModeChangeTime = 0; // Time of last mode change
const unsigned long modeChangeInterval = 30000; // Mode change interval (30 seconds)

void setup() {
  // Initialize LED pins as outputs
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);
  pinMode(led3Pin, OUTPUT);

  // Initialize button pin as input
  pinMode(buttonPin, INPUT_PULLUP); // Use internal pull-up resistor

  // Seed random number generator
  randomSeed(analogRead(0));
}

void loop() {
  // Read the state of the button
  buttonState = digitalRead(buttonPin);

  // Check if the button is pressed
  if (buttonState == LOW && lastButtonState == HIGH) {
    // Toggle mode between 0 and 1
    mode = (mode == 0) ? 1 : 0;

    // Delay to debounce the button
    delay(50);
  }

  // Update last button state
  lastButtonState = buttonState;

  // Check if it's time to change the mode
  unsigned long currentTime = millis();
  if (currentTime - lastModeChangeTime >= modeChangeInterval) {
    // Change mode
    mode = (mode + 1) % 2; // Toggle between 0 and 1
    lastModeChangeTime = currentTime;
  }

  // Perform actions based on the current mode
  switch (mode) {
    case 0:
      randomBlinkEffect();
      break;
    case 1:
      sequentialBlinkEffect();
      break;
  }
}

// Function for random blink effect
void randomBlinkEffect() {
  digitalWrite(led1Pin, random(2) == 0 ? HIGH : LOW);
  digitalWrite(led2Pin, random(2) == 0 ? HIGH : LOW);
  digitalWrite(led3Pin, random(2) == 0 ? HIGH : LOW);
  
  delay(random(100, 1000)); // Random delay between 100ms and 1000ms
}

// Function for sequential blink effect
void sequentialBlinkEffect() {
  digitalWrite(led1Pin, HIGH);
  delay(500);
  digitalWrite(led1Pin, LOW);
  
  digitalWrite(led2Pin, HIGH);
  delay(500);
  digitalWrite(led2Pin, LOW);
  
  digitalWrite(led3Pin, HIGH);
  delay(500);
  digitalWrite(led3Pin, LOW);
}
Leave a Comment