//Declare pins
int led1=2;
int led2=3;
int led3=4;
int button1=5;
int speedUpButton=6;
int speedDownButton=7;
bool ledState = false;
bool buttonState1 = false;
int button4=8;
int brightness = 0;
int fadeAmount = 5;
bool buttonState = false;
bool lastButtonState = false;
const int MIN_BLINK_SPEED = 50;
const int MAX_BLINK_SPEED = 1000;
// Initial blink speed
int blinkSpeed = 500;
void setup() {
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(button1, INPUT);
pinMode(speedUpButton, INPUT);
pinMode(speedDownButton, INPUT);
pinMode(button4, INPUT);
}
void loop() {
// button 1
buttonState = digitalRead(button1);
// Check if the button is pressed (LOW)
if (buttonState == HIGH) {
// Check the current state of the LED
if (ledState == LOW) {
// If the LED is currently off, turn it on
digitalWrite(led1, HIGH);
// Update the LED state variable to reflect the change
ledState = true;
} else {
// If the LED is currently on, turn it off
digitalWrite(led1, LOW);
// Update the LED state variable to reflect the change
ledState = false;
}
}
//button 4
buttonState = digitalRead(button4);
// check if the button is pressed
if (buttonState == HIGH && lastButtonState == LOW) {
// if the button is pressed, start increasing the brightness
for (int i = 0; i <= 255; i += fadeAmount) {
analogWrite(led3, i);
delay(100);
}
// then decrease the brightness back to 0
for (int i = 255; i >= 0; i -= fadeAmount) {
analogWrite(led3, i);
delay(100);
}
brightness = 0;
}
lastButtonState = buttonState;
// Check if increase button is pressed and adjust blink speed
if (digitalRead(speedUpButton) == LOW) {
blinkSpeed = min(blinkSpeed + 50, MAX_BLINK_SPEED);
delay(100); // debounce
}
// Check if decrease button is pressed and adjust blink speed
if (digitalRead(speedDownButton) == LOW) {
blinkSpeed = max(blinkSpeed - 50, MIN_BLINK_SPEED);
delay(100); // debounce
}
// Blink the LED
digitalWrite(led2, HIGH);
delay(blinkSpeed / 2);
digitalWrite(led2, LOW);
delay(blinkSpeed / 2);
}