I wrote this simple program to diplay values through the serial connection when (and only when) the wiper of a potentiometer is rotated.
void setup(){
Serial.begin(9600);
}
void loop(){
int first = analogRead(3);
delay(50);
int second = analogRead(3);
if (first != second){ //i.e if the value has changed
int mapped = map(second,512,1024,3,250);
Serial.println(mapped);
}
}
However, the arduino seems to be picking up a LOT of noise and therefore, continuously spits out values, even when the pot is not being rotated.
My circuit is as follows
What's wrong? (If I remember correctly, I have used the same program before.. so that makes it even more puzzling. )
EDIT: The 3 to 250 in the map statement was just an arbitrary combination..
Could it just be an imprecise pot? What's the variance between first and second? It may just be one, which is an error of less than 0.1%. I'm not certain the A/D circuit is that real-world accurate.
Hm.. Looks like a suitable fix would be to have a threshold for the difference between two adjacent readings. What kind of value should I have for the threshold?
Grumpy_Mike:
What value of pot are you using? It should be no greater than 10K.
A 0.1uF capacitor from analogue input to ground also often helps.
The pot is a 10K one.. I had the same problem with a 1k one as well. However, the difference of 1 unit is about 4.8 millivolts.. That shouldn't be too much of a problem. I'll try your capacitor solution too..