int ledPin = 13; // LED connected to Pin 13
int buttonPin = 2; // Button connected to Pin 2
int buttonState = 0; // Variable to store the button state
void setup() {
pinMode(ledPin, OUTPUT); // Set LED pin as output
pinMode(buttonPin, INPUT); // Set button pin as input
Serial.begin(9600); // Initialize serial communication for test output
}
void loop() {
// Simulating each test case
// Test Case 1: Button Not Pressed
digitalWrite(buttonPin, LOW); // Simulate button not pressed
buttonState = digitalRead(buttonPin);
Serial.print("Test Case 1 - Button Not Pressed: LED State = ");
Serial.println(digitalRead(ledPin)); // Expected: LOW (LED OFF)
delay(1000); // Delay for readability in serial monitor
// Test Case 2: Button Pressed
digitalWrite(buttonPin, HIGH); // Simulate button pressed
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on LED
}
Serial.print("Test Case 2 - Button Pressed: LED State = ");
Serial.println(digitalRead(ledPin)); // Expected: HIGH (LED ON)
delay(1000);
// Test Case 3: Button Pressed and Released
digitalWrite(buttonPin, HIGH); // Simulate button pressed
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on LED
}
delay(500); // Hold the button press for a moment
digitalWrite(buttonPin, LOW); // Simulate button released
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
digitalWrite(ledPin, LOW); // Turn off LED
}
Serial.print("Test Case 3 - Button Pressed and Released: LED State = ");
Serial.println(digitalRead(ledPin)); // Expected: LOW (LED OFF)
delay(1000);
}