#include <Servo.h>
int ledPin = 2; // Pin number for the LED
int digitalPin = 8; // Pin number for digital input (connected to a sensor)
int val = 0; // Variable to store the digital input value
Servo myservo; // Create a Servo object
int servoPin = 9; // Pin number for the Servo motor
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(digitalPin, INPUT); // Set the digital input pin
Serial.begin(9600); // Start serial communication for debugging
myservo.attach(servoPin); // Attach the servo to its pin
}
void loop() {
val = digitalRead(digitalPin); // Read the digital input value from pin 8
Serial.print("val = "); // Print a message to the serial monitor
Serial.println(val); // Print the value of the variable val
if (val == 0) { // If the digital input is LOW (0), object detected
digitalWrite(ledPin, LOW); // Turn off the LED
myservo.write(90); // Set the servo to its middle position (or adjust as needed)
} else {
digitalWrite(ledPin, HIGH); // If the digital input is HIGH (1), turn on the LED
myservo.write(0); // Set the servo to a different position (or adjust as needed)
}
delay(500); // Delay for 500 milliseconds before the next iteration
}