Perhaps this is simple but, i'm hoping to find a code that simply detects if my pot is actually being twisted. The value is not important. I'd like have a true/false type of code. Essentially, if it's in rotation it's true and if not, false.
I'm not sure how to achieve this and any advice would be greatly appreciated.
Compare 2 successive readings and if they differ then it's moving.
In setup() do an analogRead() into say oldReading. Then in loop() take a currentReading and compare to oldReading. Decide if moving or not. Then save currentReading as oldReading, rinse and repeat.
This example sketch outputs a 0-100% value, but only when the pot is rotated more than 1%.
Maybe you can adapt it to your needs.
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;
}
}
}