// Define pin numbers
const int switchPin1 = 8; // Switch Pin for decreasing the frequency
const int switchPin2 = 7; // Switch pin for increasing the frequency
const int ledPin = 6; // LED pin
// Define variables
int frequency = 1000; // Initial frequency (in milliseconds)
unsigned long previousMillis = 0; // Variable to store previous time
void setup() {
// Setting the LED pin as output
pinMode(ledPin, OUTPUT);
// Setting both switch pins as inputs with interupts
pinMode(switchPin1, INPUT_PULLUP);
pinMode(switchPin2, INPUT_PULLUP);
}
void loop() {
// Read the state of switch 1
if (digitalRead(switchPin1) == LOW) {
frequency = frequency*2; // Decrease the frequency by multiplying by 2
delay(200); // Debounce delay
}
// Read the state of switch 2
if (digitalRead(switchPin2) == LOW) {
frequency = frequency/2; // Increase the frequency by dividing by 2
delay(200); // Debounce delay
}
// Range of frequencies
if (frequency < 125) {
frequency = 125; // Maximum frequency (8 Hz)
}
if (frequency > 2000) {
frequency = 2000; // Minimum frequency (0.5 Hz)
}
// Check time when toggling LED switch
unsigned long currentMillis = millis(); // Get the current time
if (currentMillis - previousMillis >= frequency) {
// Save the last time the LED was toggled
previousMillis = currentMillis;
// Toggle the LED
digitalWrite(ledPin, !digitalRead(ledPin));
}
}