I thought i'd put together something simple for my daughter, upgrade her toy kitchen with some real dials.
I've hooked up a 50k potentiometer and TM1637 to a Nano clone, and got it all working, there's just one thing, it never sits still.
when you're not moving the pot, the last digit is bouncing up and down by +/- 1.
I tried recording the last known state of the pot, and differencing this from the current, to see if enough of a delta had taken place, but couldn't get it to work.
can anyone suggest anything?
#include <TM1637Display.h>
#define CLK 9
#define DIO 8
TM1637Display display(CLK, DIO);
int potPin = A0; // select the input pin for the potentiometer
int potVal = 0;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
display.setBrightness(3);
}
void loop() {
potVal = map(analogRead(potPin), 1023, 0, 0, 1000);
display.showNumberDec(potVal, true);
delay(100);
}
You can try putting a 0.1uf cap from the analog input to ground (low pass filter). Another thing to try is averaging several readings and display the average.
Try and make many measrements and divide the sum of the measurements with the number of measrements making an average.
As already said, +/-1 is good.
Or You can add 1, divide by 2 and then map the value to desired range.
This can always happen with analog-to-digital conversion because you can be on the "hairy edge" between two values and there's always some noise. (It doesn't always happen but it can always happen.)
Filtering (analog or [u]digital[/u]) can help or at least slow it down but it won't always eliminate it completely.
You can add some logic or [u]hysteresis[/u] to your software, so for example your display-count doesn't change unless the actual reading changes by 2 (or more) counts.
And you can add some "timing" to that so once the numbers start changing you can dial-in the exact number and then after it's stable (within one count) for awhile it "locks in" and get's resistant to change again...
Heating & cooling systems have hysteresis (or "swing") so the system doesn't turn on & off 10 times a second... The heater might stay-on 'till you're one degree over the target temperature, then after it shuts-off it doesn't turn-on again until you're one degree below the target.
Do you want such a high resolution that every number between 0 and 1000 is selectable or would it also be OK to see a series displayed as the potentiometer spindle is turned:
Not easy to get 1001 stable values from a 10-bit A/D.
You could have used a rotary encoder (digital) instead of a pot (analogue).
Attached is code I wrote for 101 values (0-100%) with a pot.
It chops off the bad extremes of the pot, and uses hysteresis windows to lock onto one number.
Leo..
// converts the position of a 10k lin(B) pot to 0-100%
// pot connected to A0, 5volt and ground
int rawValue;
int oldValue;
byte potPercentage;
byte oldPercentage;
void setup() {
Serial.begin(115200); // set serial monitor to this baud rate, or change the value
}
void loop() {
// read input twice
rawValue = analogRead(A0);
rawValue = analogRead(A0); // double read
// ignore bad hop-on region of a pot by removing 8 values at both extremes
rawValue = constrain(rawValue, 8, 1015);
// add some deadband
if (rawValue < (oldValue - 4) || rawValue > (oldValue + 4)) {
oldValue = rawValue;
// convert to percentage
potPercentage = map(oldValue, 8, 1008, 0, 100);
// Only print if %value changes
if (oldPercentage != potPercentage) {
Serial.print("Pot percentage is: ");
Serial.print(potPercentage);
Serial.println(" %");
oldPercentage = potPercentage;
}
}
}
Thanks all, @wawa s code seemed to do the trick, but i'll adjust it a bit more to see if i can get accuracy on dial motion.
i don' thave a .1uf cap on hand, only a 10uf, so couldn't see whether that acted as a LPF enough to make it work.
thanks all for advice, i think i need to do some more learning around hysteresis
Try this, without a 10uF capacitor, which might make things worse. Especially if on USB supply.
Dumb way without hysteresis, but it might just be enough.
As said, it's not easy to get 1001 stable values if you only have 1024 to begin with.
Leo..
void loop() {
unsigned int potVal = 0; // reset
for (int i = 0; i < 64; i++) { // 64 analogue readings
potVal += analogRead(A0); // add each value
}
potVal = map(potVal, 65472, 0, 0, 1000); // pot wires reversed ?
// potVal = map(potVal, 0, 65472, 0, 1000); // use this if connected 'normal'
display.showNumberDec(potVal, true);
delay(100);
}