Advertisement
belrey10

Day 9: The Festival Of Lights

Nov 3rd, 2024 (edited)
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const int buttonPin = 2;  // the pin where the button is connected
  2. const int ledPin1 = 9;    // the pin where the first LED is connected
  3. const int ledPin2 = 10;    // the pin where the second LED is connected
  4. const int ledPin3 = 11;   // the pin where the third LED is connected
  5. const int onboardLed = 13; // the onboard LED
  6.  
  7. int buttonState = 0;      // variable for reading the button status
  8. int lastButtonState = 0;  // variable to store the last button state
  9. int mode = 0;             // variable to store the current mode
  10. unsigned long previousMillis = 0;  // will store last time LED was updated
  11. const long interval = 100;         // interval at which to blink (milliseconds)
  12.  
  13. void setup() {
  14.   pinMode(buttonPin, INPUT);  // initialize the button pin as an input
  15.   pinMode(ledPin1, OUTPUT);   // initialize the first LED pin as an output
  16.   pinMode(ledPin2, OUTPUT);   // initialize the second LED pin as an output
  17.   pinMode(ledPin3, OUTPUT);   // initialize the third LED pin as an output
  18.   pinMode(onboardLed, OUTPUT); // initialize the onboard LED as an output
  19.   Serial.begin(9600);         // begin serial communication for debugging
  20. }
  21.  
  22. void loop() {
  23.   unsigned long currentMillis = millis();
  24.   buttonState = digitalRead(buttonPin);
  25.  
  26.   if (buttonState != lastButtonState) {
  27.     if (buttonState == HIGH) {
  28.       mode = (mode + 1) % 3;  // change mode
  29.       digitalWrite(onboardLed, HIGH); // blink onboard LED when button is pressed
  30.       delay(200); // debounce delay
  31.       digitalWrite(onboardLed, LOW);
  32.     }
  33.     delay(50);  // debounce delay
  34.   }
  35.   lastButtonState = buttonState;
  36.  
  37.   switch (mode) {
  38.     case 0:
  39.       fireMode(currentMillis);
  40.       break;
  41.     case 1:
  42.       fairyMode(currentMillis);
  43.       break;
  44.     case 2:
  45.       steadyMode();
  46.       break;
  47.   }
  48. }
  49.  
  50. void fireMode(unsigned long currentMillis) {
  51.   static unsigned long previousMillis = 0;
  52.   if (currentMillis - previousMillis >= interval) {
  53.     previousMillis = currentMillis;
  54.     int brightness1 = random(150, 255);
  55.     int brightness2 = random(100, 200);
  56.     int brightness3 = random(50, 150);
  57.     analogWrite(ledPin1, brightness1);
  58.     analogWrite(ledPin2, brightness2);
  59.     analogWrite(ledPin3, brightness3);
  60.   }
  61. }
  62.  
  63. void fairyMode(unsigned long currentMillis) {
  64.   int brightness1 = (sin(currentMillis / 200.0) * 127) + 128;
  65.   int brightness2 = (cos(currentMillis / 200.0) * 127) + 128;
  66.   int brightness3 = (sin((currentMillis + 1000) / 200.0) * 127) + 128;
  67.   analogWrite(ledPin1, brightness1);
  68.   analogWrite(ledPin2, brightness2);
  69.   analogWrite(ledPin3, brightness3);
  70. }
  71.  
  72. void steadyMode() {
  73.   analogWrite(ledPin1, 255);
  74.   analogWrite(ledPin2, 255);
  75.   analogWrite(ledPin3, 255);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement