#define LED_BUT_PIN 21 // Define input 1 as LED button pin
#define SERVO_BUT_PIN 33 // Define input 2 as servo button pin
#define SERVO_PIN 13 // Define servo pin as D13
int ledState = 0; // Define the initial state of the LED button
int ledBrightness = 0; // Define the initial brightness of the LED (0-255)
#include <Servo.h> // Include the Servo library
Servo myServo; // Create a Servo object to control the servo
void setup() {
pinMode(LED_BUT_PIN, INPUT); // Set LED button pin as input
pinMode(SERVO_BUT_PIN, INPUT); // Set servo button pin as input
pinMode(22, OUTPUT); // Set D22 as output for LED
myServo.attach(SERVO_PIN); // Attach the servo to the pin
}
void loop() {
int ledButState = digitalRead(LED_BUT_PIN); // Read LED button state
int servoButState = digitalRead(SERVO_BUT_PIN); // Read servo button state
if (ledButState == HIGH) {
ledState++;
if (ledState == 1) {
ledBrightness = 0; // Turn off the LED
analogWrite(22, ledBrightness);
} else if (ledState == 2) {
ledBrightness = 64; // Set low brightness
analogWrite(22, ledBrightness);
} else if (ledState == 3) {
ledBrightness = 128; // Set medium brightness
analogWrite(22, ledBrightness);
} else {
ledBrightness = 255; // Set maximum brightness
analogWrite(22, ledBrightness);
ledState = 0;
}
delay(200); // Wait for button debouncing
}
if (servoButState == HIGH) {
for (int pos = 0; pos <= 180; pos += 1) { // Sweep the servo from 0 to 180 degrees
myServo.write(pos); // Set the servo position
delay(15); // Wait for the servo to move
}
for (int pos = 180; pos >= 0; pos -= 1) { // Sweep the servo from 180 to 0 degrees
myServo.write(pos); // Set the servo position
delay(15); // Wait for the servo to move
}
} else {
myServo.write(90); // Set the servo to 90 degrees
}
}