#include <TM1637Display.h>
#include <Servo.h>
#define CLK 6
#define DIO 7
TM1637Display display(CLK, DIO);
const int selectPins[] = {2, 3, 4, 5};
const int potPins[] = {A1, A2, A3, A4};
const int SIG_PIN = 9;
Servo myServo;
// Variables for fluid movement
int currentAngle = 0;
int stepDirection = 1; // 1 for forward, -1 for backward
void setup() {
display.setBrightness(0x0f);
for (int i = 0; i < 4; i++) pinMode(selectPins[i], OUTPUT);
myServo.attach(SIG_PIN);
}
void loop() {
int realAddress = 0;
long displayValue = 0;
// 1. Check the Potentiometers (Always responsive!)
for (int i = 0; i < 4; i++) {
if (analogRead(potPins[i]) > 512) {
bitSet(realAddress, i);
if (i == 0) displayValue += 1;
if (i == 1) displayValue += 10;
if (i == 2) displayValue += 100;
if (i == 3) displayValue += 1000;
}
}
// 2. Show Binary on Display
display.showNumberDec(displayValue, true);
// 3. Select the Mux Address
for (int i = 0; i < 4; i++) {
digitalWrite(selectPins[i], bitRead(realAddress, i));
}
// 4. Fluid Movement (The "Smart" Way)
if (realAddress < 6) {
myServo.write(currentAngle);
// Increment or decrement the angle
currentAngle += stepDirection;
// Reverse direction at the limits
if (currentAngle >= 180 || currentAngle <= 0) {
stepDirection *= -1;
}
// This delay controls the speed. 15-20ms is standard for "smooth"
delay(20);
}
}