Advertisement
Guest User

Arduino UNO AR TIMER CODE

a guest
Nov 28th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | Source Code | 0 0
  1. #include <avr/wdt.h>  // Include the watchdog timer library
  2.  
  3. const int button3Pin = 3;  // Pin for Button 3
  4. const int button4Pin = 4;  // Pin for Button 4
  5. const int button2Pin = 2;  // Pin for Button 2
  6. const int button5Pin = 5;  // Pin for Button 5
  7. const int pin9 = 9;        // Pin 9 for the primary output
  8. const int ledPin = 12;     // Pin 12 for the LED
  9.  
  10. void setup() {
  11.   pinMode(button3Pin, INPUT_PULLUP);  // Button 3
  12.   pinMode(button4Pin, INPUT_PULLUP);  // Button 4
  13.   pinMode(button2Pin, INPUT_PULLUP);  // Button 2
  14.   pinMode(button5Pin, INPUT_PULLUP);  // Button 5
  15.   pinMode(pin9, OUTPUT);              // Pin 9 as output
  16.   pinMode(ledPin, OUTPUT);            // LED on pin 12 as output
  17. }
  18.  
  19. void loop() {
  20.   // Check if Button 2 is pressed (to reset the Arduino)
  21.   if (digitalRead(button2Pin) == LOW) {
  22.     // Enable the watchdog timer to reset the system
  23.     wdt_enable(WDTO_15MS);  // Set a 15ms timeout, enough to reset the Arduino
  24.     while (true) {
  25.       // Infinite loop, the watchdog will reset the Arduino after 15ms
  26.     }
  27.   }
  28.  
  29.   // Handle Button 5 (pin 5) behavior:
  30.   if (digitalRead(button5Pin) == LOW) {
  31.     // Button 5 is pressed, blink LED on pin 12 every 0.5 seconds
  32.     digitalWrite(pin9, LOW);  // Reverse signal
  33.     digitalWrite(ledPin, HIGH);
  34.     delay(250);
  35.     digitalWrite(ledPin, LOW);
  36.     delay(250);
  37.   } else {
  38.     // Button 5 is not pressed, turn off LED and set pin 9 HIGH
  39.     digitalWrite(pin9, HIGH);  // Reverse signal
  40.     digitalWrite(ledPin, LOW);
  41.   }
  42.  
  43.   // If Button 3 is pressed, start the loop to set pin 9 low for 60 seconds, then high for 1 second
  44.   if (digitalRead(button3Pin) == LOW) {
  45.     while (digitalRead(button2Pin) == HIGH) {  // Keep looping until Button 2 is pressed
  46.       digitalWrite(pin9, LOW);  // Reverse signal
  47.       digitalWrite(ledPin, HIGH);  // LED stays ON
  48.       delay(60000);                // 60 seconds
  49.       digitalWrite(pin9, HIGH);  // Reverse signal
  50.       for (int i = 0; i < 2; i++) { // Blink LED every 0.5 seconds during the 1-second HIGH period
  51.         digitalWrite(ledPin, HIGH);
  52.         delay(250);
  53.         digitalWrite(ledPin, LOW);
  54.         delay(250);
  55.       }
  56.     }
  57.   }
  58.  
  59.   // If Button 4 is pressed, start the loop to set pin 9 low for 4 seconds, then high for 1 second
  60.   if (digitalRead(button4Pin) == LOW) {
  61.     while (digitalRead(button2Pin) == HIGH) {  // Keep looping until Button 2 is pressed
  62.       digitalWrite(pin9, LOW);  // Reverse signal
  63.       digitalWrite(ledPin, HIGH);  // LED stays ON
  64.       delay(4000);                 // 4 seconds
  65.       digitalWrite(pin9, HIGH);  // Reverse signal
  66.       for (int i = 0; i < 2; i++) { // Blink LED every 0.5 seconds during the 1-second HIGH period
  67.         digitalWrite(ledPin, HIGH);
  68.         delay(250);
  69.         digitalWrite(ledPin, LOW);
  70.         delay(250);
  71.       }
  72.     }
  73.   }
  74.  
  75.   // If no buttons are pressed, keep the LED off
  76.   digitalWrite(ledPin, LOW);
  77. }
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement