Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************************************
- * Arduino Foundation Series: Automatic Jackolantern *
- * Learnelectronics Oct. 2023 (C) *
- * https://www.youtube.com/learnelectronics *
- ********************************************************/
- // Define constants for pin numbers
- const int inputPin = 2; // The input pin to monitor
- const int ledPin1 = 5; // First LED pin
- const int ledPin2 = 6; // Second LED pin
- // Variables to track LED state and timing
- bool ledState1 = LOW; // Initial LED state for LED 1
- bool ledState2 = LOW; // Initial LED state for LED 2
- unsigned long previousMillis = 0; // Stores the last time LEDs were toggled
- const long interval = 1000; // Interval for the 1-second duty cycle (in milliseconds)
- void setup() {
- pinMode(inputPin, INPUT); // Set the input pin as INPUT
- pinMode(ledPin1, OUTPUT); // Set the LED pins as OUTPUT
- pinMode(ledPin2, OUTPUT);
- pinMode(3,OUTPUT);
- }
- void loop() {
- // Read the state of the input pin
- int buttonState = digitalRead(inputPin);
- // Check if the input pin is HIGH (button pressed)
- if (buttonState == HIGH) {
- unsigned long currentMillis = millis(); // Get the current time
- // Check if it's time to toggle the LEDs
- if (currentMillis - previousMillis >= interval) {
- // Save the current time
- previousMillis = currentMillis;
- // Toggle the state of both LEDs
- ledState1 = !ledState1;
- ledState2 = !ledState2;
- // Set the LEDs to their new states
- digitalWrite(ledPin1, ledState1);
- digitalWrite(ledPin2, ledState2);
- for (int i = 200; i <= 400; i++) {
- tone(3, i);
- delay(11);
- }
- noTone(3);
- }
- } else {
- // If the input pin is LOW (button not pressed), turn off both LEDs
- digitalWrite(ledPin1, LOW);
- digitalWrite(ledPin2, LOW);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement