|
Post by pt3r on Nov 14, 2021 11:16:48 GMT
There is a problem with a potmeter connected the analog pin of an arduino. if you read its value using following example: void setup() { Serial.begin(9600); } void loop(){ int readValue = analogRead(A3); Serial.println(readValue); } If you check the read values in the serial monitor you will see that its not a constant value even it you don't move the potmeter. A solution I like to use is to scale down the read value to the range that is useful for me since most of the times you don't need the range of 0-1023 in your code. If for example you need values in a range of 0-128 then you can just scale down the read value by dividing the read value by 8.
int readValue = analogRead(A3)/8;
or by shifting the bits of the read value 3 positions to the left thus dropping the 3 least significant bytes which equals the whole division by 8.
int readValue = analogRead(A3)>>3; If you run the modified code you will notice that there is significant less jitter on the read values.
|
|
|
Post by robertlanger on Nov 14, 2021 15:19:33 GMT
Definitely recommended! But, be aware that when the pot is at the edge between two different output values, the result still can jump with a bitshift only. In this case, implementing a so called hysteresis is the best way; meaning a change is only detected when the ADC value has changed by more than 1, e.g. by 4. For Arduino there are lots of examples available.
|
|
|
Post by pt3r on Nov 14, 2021 19:18:18 GMT
|
|