/*
Input / Output Demo
Demonstrates reading a button, a switch, and a potentiometer.
The behavior of the output LEDs is controlled by those inputs.
9/26/25
*/
const int NUM_LEDS = 8;
const int BTN_PIN = 4;
const int SW_PIN = 3;
const int POT_PIN = A0;
const int LED_PINS[] = {12, 11, 10, 9, 8, 7, 6, 5};
int count = 0;
int oldBtnState = 1; // pin idles HIGH
bool checkButton() {
bool wasPressed = false;
int btnState = digitalRead(BTN_PIN); // read the pin
if (btnState != oldBtnState) { // if it changed
oldBtnState = btnState; // remember the state
if (btnState == LOW) { // was just pressed
wasPressed = true;
//Serial.println("Button Pressed");
} else { // was just released
//Serial.println("Button Released");
}
delay(20); // short delay to debounce button
}
return wasPressed;
}
void sweepLeds(int dir, int speed) {
long sweepDelay = map(speed, 0, 1023, 1000, 100);
Serial.print("Sweep ");
Serial.print(dir ? "left to right" : "right to left");
Serial.print(", ");
Serial.print(sweepDelay);
Serial.println(" ms per step.");
if (dir) { // left to right
count = 0;
while (count < NUM_LEDS) {
// turn all LEDs off
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(LED_PINS[i], LOW);
}
// turn next LED on
digitalWrite(LED_PINS[count], HIGH);
count++;
delay (sweepDelay);
}
} else { // right to left
count = NUM_LEDS - 1;
while (count >= 0) {
for (int i = 0; i < NUM_LEDS; i++) {
digitalWrite(LED_PINS[i], LOW);
}
digitalWrite(LED_PINS[count], HIGH);
count--;
delay (sweepDelay);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(SW_PIN, INPUT_PULLUP);
for (int i = 0; i < NUM_LEDS; i++) {
pinMode(LED_PINS[i], OUTPUT);
}
Serial.println("Ready!\n");
}
void loop() {
if (checkButton()) {
int direction = digitalRead(SW_PIN);
int potVal = analogRead(POT_PIN);
sweepLeds(direction, potVal);
}
}
Start
Direction
Speed