Hi, i'm having a problem with Potentiometer on Arduino. my problem is the Potentiometer values is running continuously which is why my arduino is running slow. what i meant is on "running continuously" is that it reads the value of the potentiometer continuously and it's not stopping. it is supposed to be like: when i move the potentiometer then i should get the value and if i didn't touched or moved the potentiometer it should not print any values on the serial monitor. i'm using a slide potentiometer, Thanks in advance!
Please post the test code.
Please read the forum guidelines to see how to properly post code and some information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in a code block.
Please post a schematic. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.
You need to only print when the value changes. Retain the last value and only update when different, then save new value for next pass.
yeah, how should i do that?
could you give me a code?
What you got so far? Please post using the < code > button!
int sensorValue = 0;
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop()
{
// read the input on analog pin 0:
sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(10); // Delay a little bit to improve simulation performance
}
Dude, Thank you so much for your help. i've been having this issue for a couple of years now haha Thank you so much!!!
int sensorValue = 0;
int oldSensorValue = 0; // last reading
void setup()
{
// pinMode(A0, INPUT); // not needed as A0 defauts to input
Serial.begin(115200); // set ide to match!
}
void loop()
{
// read the input on analog pin 0:
sensorValue = analogRead(A0);
if (oldSensorValue != sensorValue)
{
// print out the value you read:
Serial.println(sensorValue);
oldSensorValue = sensorValue;
}
// no longer needed delay(10); // Delay a little bit to improve simulation performance
}
You can also cut it down more, if you need, by only printing if it is enough different, you choose how much is enough:
if (abs(oldSensorValue - sensorValue) > 20) {
"if the new value is more than 20 units away from the old value…"
It's a common pattern and uses the abs() function. Absolute value like from maths class.
Fancier yet: print the new value if it is different enough. Print no matter what every 700 milliseconds (or however often).
Just an idea.
a7
@derekronquillo Don't forget to mark you topic as solved
What are you doing with that value.
If it's used to write to a PWM pin (0-255), then use this code.
It has hysteresis, so it doesn't jump.
Leo..
int rawValue, oldValue;
byte potValue;
void setup() {
Serial.begin(115200);
}
void loop() {
rawValue = analogRead(A0); // read
if (abs(rawValue - oldValue) >= 4) { // hysteresis
oldValue = rawValue; // remember the change
potValue = oldValue >> 2; // 10-bit to 8-bit
Serial.println(potValue); // print changes
}
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.