byte ledPin = 3, potentiometerPin = A1;
int oldPotVal, pwm;
unsigned long timer = 0, oldTime = 0, interval = 100; // reading interval in milliseconds
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Move potentiometer.");
}
void loop() {
readPotentiometer(); // read the sensor without using delay();
}
void readPotentiometer() {
int newPotVal = analogRead(potentiometerPin);
if (millis() - oldTime > interval) { // compare event duration/difference to interval
oldTime = millis(); // store the new event start
if (newPotVal != oldPotVal) {
analogWrite(ledPin, map(newPotVal, 0, 1023, 0, 255)); // PWM for LED
Serial.print("AIN:"); // show the reading
pad(newPotVal); // show the reading
Serial.print("PWM:"); // show the reading
pwm = map(newPotVal, 0, 1023, 0, 255);
pad(pwm); // show the reading
for (int i = 0; i < pwm / 5; i++)
Serial.print("*");
Serial.println();
}
oldPotVal = newPotVal;
}
}
void pad(int value) {
if (value < 1000) Serial.print(" ");
if (value < 100) Serial.print(" ");
if (value < 10) Serial.print(" ");
Serial.print(value);
Serial.print(" ");
}