Advertisement
learnelectronics

Haloween Automation

Oct 2nd, 2023
3,454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********************************************************
  2.  *  Arduino Foundation Series: Automatic Jackolantern   *
  3.  *              Learnelectronics Oct. 2023 (C)         *
  4.  *       https://www.youtube.com/learnelectronics       *
  5.  ********************************************************/
  6. // Define constants for pin numbers
  7. const int inputPin = 2; // The input pin to monitor
  8. const int ledPin1 = 5;  // First LED pin
  9. const int ledPin2 = 6;  // Second LED pin
  10.  
  11.  
  12. // Variables to track LED state and timing
  13. bool ledState1 = LOW;   // Initial LED state for LED 1
  14. bool ledState2 = LOW;   // Initial LED state for LED 2
  15. unsigned long previousMillis = 0; // Stores the last time LEDs were toggled
  16. const long interval = 1000; // Interval for the 1-second duty cycle (in milliseconds)
  17.  
  18.  
  19.  
  20. void setup() {
  21.   pinMode(inputPin, INPUT); // Set the input pin as INPUT
  22.   pinMode(ledPin1, OUTPUT); // Set the LED pins as OUTPUT
  23.   pinMode(ledPin2, OUTPUT);
  24.   pinMode(3,OUTPUT);
  25. }
  26.  
  27. void loop() {
  28.   // Read the state of the input pin
  29.   int buttonState = digitalRead(inputPin);
  30.  
  31.   // Check if the input pin is HIGH (button pressed)
  32.   if (buttonState == HIGH) {
  33.     unsigned long currentMillis = millis(); // Get the current time
  34.    
  35.     // Check if it's time to toggle the LEDs
  36.     if (currentMillis - previousMillis >= interval) {
  37.       // Save the current time
  38.       previousMillis = currentMillis;
  39.      
  40.       // Toggle the state of both LEDs
  41.       ledState1 = !ledState1;
  42.       ledState2 = !ledState2;
  43.      
  44.       // Set the LEDs to their new states
  45.       digitalWrite(ledPin1, ledState1);
  46.       digitalWrite(ledPin2, ledState2);
  47.  
  48.       for (int i = 200; i <= 400; i++) {
  49.     tone(3, i);
  50.     delay(11);
  51.   }
  52.     noTone(3);  
  53.     }
  54.   } else {
  55.     // If the input pin is LOW (button not pressed), turn off both LEDs
  56.     digitalWrite(ledPin1, LOW);
  57.     digitalWrite(ledPin2, LOW);
  58.   }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement