const int sensorPin = 2; // Push button connected to pin 2
const int ledPin = 13; // LED connected to pin 13
void setup() {
pinMode(sensorPin, INPUT); // Configure pin 2 as input
pinMode(ledPin, OUTPUT); // Configure pin 13 as output
digitalWrite(ledPin, LOW); // Turn off LED initially
Serial.begin(9600); // Begin serial communication
}
void loop() {
int sensorState = digitalRead(sensorPin); // Read the push button state
if (sensorState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on LED (object detected)
Serial.println("Object Detected!");
} else {
digitalWrite(ledPin, LOW); // Turn off LED (no object)
Serial.println("No Object");
}
delay(500); // Small delay for debouncing
}