// Arduino code for:
// Task 1: Change LED state when button is pressed
// Task 2: Blinking LED active only when Task 1 LED is off
// Pin definitions
const int buttonPin = 7; // Push button pin (changed to pin 7)
const int ledPin1 = 13; // LED for Task 1
const int ledPin2 = 12; // Blinking LED for Task 2
// Variables
int ledState1 = LOW; // Current state of LED 1
int previousButtonState = LOW; // Previous state of the button
unsigned long previousMillis = 0; // Last time LED 2 was updated
const long blinkInterval = 500; // Blink interval in milliseconds (0.5 seconds)
void setup() {
// Initialize pins
pinMode(buttonPin, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// Initialize LEDs to their starting states
digitalWrite(ledPin1, ledState1);
digitalWrite(ledPin2, LOW);
// For debugging
Serial.begin(9600);
Serial.println("Program started");
}
void loop() {
// Read the current button state
int currentButtonState = digitalRead(buttonPin);
// Task 1: Check if button was pressed (LOW to HIGH transition)
if (currentButtonState == HIGH && previousButtonState == LOW) {
// Toggle LED state
ledState1 = !ledState1;
digitalWrite(ledPin1, ledState1);
// Debug info
Serial.print("Button pressed! LED1 is now: ");
Serial.println(ledState1 ? "ON" : "OFF");
// Small delay to avoid button bounce
delay(50);
}
// Update previous button state
previousButtonState = currentButtonState;
// Task 2: Blink LED2 only when LED1 is off
if (ledState1 == LOW) {
// Current time
unsigned long currentMillis = millis();
// Check if it's time to blink
if (currentMillis - previousMillis >= blinkInterval) {
previousMillis = currentMillis;
// Toggle LED2
int ledState2 = digitalRead(ledPin2);
digitalWrite(ledPin2, !ledState2);
}
} else {
// If LED1 is on, make sure LED2 is off
digitalWrite(ledPin2, LOW);
}
}