I got the stable values. However, i want to modify the code to detect the turning direction but was not able to do that.
Please see the code below. Your help would be much appreciated.
#include <ResponsiveAnalogRead.h>
const int sensorPin = A0;
int sensorBe4;
int sensorAft;
int moveDetect;
ResponsiveAnalogRead pot(sensorPin, true);
void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
}
void loop() {
pot.update();
sensorBe4 = pot.getValue();
sensorBe4 = map(sensorBe4, 0, 1023, 0, 250);
delay(20);
sensorAft = pot.getValue();
sensorAft = map(sensorAft, 0, 1023, 0, 250);
delay(5);
moveDetect = sensorAft -sensorBe4;
delay(5);
// POT TURNING DIRECTION
if (moveDetect >0){
Serial.println("ClockWise Direction:");
delay(100);
}
if (moveDetect < 0) {
Serial.println("Counter Clockwise Direction:");
delay(100);
}
if (moveDetect == 0) {
Serial.println("No Turn");
delay(100);}
}
You do that exactly the same as when you use the millis function. Subtract the new value from the previous value. You MUST define in your mind and in your code what a negative difference and what a positive answer is. The sign will tell you the direction of the change.
When done, then move the new value to the previous value so you are set for the next time.
Thanks a lot Ruilviana, i'm appreciated.
It works but not very responsive. I adjusted the sensitivity number but the result not as expected.
I'm trying to make the ResponsiveAnalogRead library to work with the millis(). I don't know how to combine them together yet.
Does anyone know how to view / modify the update() and getValue() ?
Thanks
int potValue, oldPotValue, potPin = A0;
void setup() {
Serial.begin(115200);
Serial.println("Center potentiometer around 512.");
while (analogRead(potPin) < 500 || analogRead(potPin) > 524); // deadband, start
}
void loop() {
potValue = analogRead(potPin); // read current pot value
if (oldPotValue != potValue) { // compare to stored pot value
if (oldPotValue > potValue) // if old value is greater than new value
Serial.println("<- CCW"); // pot was turned "left"
else if (oldPotValue < potValue) { // but if old value is less than new value
Serial.println("CW -->"); // pot was turned "right"
}
oldPotValue = potValue; // store current pot value
}
}