I modified potentiometers so they can rotate indefinitely, and instead of an output between 0 & 1023
I get +1 when I rotate it to the right, -1 when I rotate it to the left.
The modification consists in removing a little bit of plastic, but when the pot goes above maximum value (1023)
and before 0, I get erratic values.
Does anyone know a way I could avoid them?
Here is the code
int sensorPin0 = A0;
int sensorPin1 = A1;
int sensorPin2 = A2;
int sensorPin3 = A3;
int p0 = 0;
int p1 = 0;
int p2 = 0;
int p3 = 0;
int prevp0 = 0;
int prevp1 = 0;
int prevp2 = 0;
int prevp3 = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
p0 = analogRead(A0);
int diff0 = ( p0 - prevp0 ) ;
if ( p0 != prevp0 )
{
if ( abs(diff0) > 5 )
{
if (p0 < prevp0)
{
Serial.println("r0 -1");
}
else { Serial.println("r0 1");}
prevp0 = p0 ;
}
}
p1 = analogRead(A1);
int diff1 = ( p1 - prevp1 ) ;
if ( p1 != prevp1 )
{
if ( abs(diff1) > 5 )
{
if (p1 < prevp1)
{
Serial.println("r1 -1");
}
else { Serial.println("r1 1");}
prevp1 = p1 ;
}
}
p2 = analogRead(A2);
int diff2 = ( p2 - prevp2 ) ;
if ( p2 != prevp2 )
{
if ( abs(diff2) > 5 )
{
if (p2 < prevp2)
{
Serial.println("r2 -1");
}
else { Serial.println("r2 1");}
prevp2 = p2 ;
}
}
p3 = analogRead(A3);
int diff3 = ( p3 - prevp3 ) ;
if ( p3 != prevp3 )
{
if ( abs(diff3) > 5 )
{
if (p3 < prevp3)
{
Serial.println("r3 -1");
}
else { Serial.println("r3 1");}
prevp3 = p3 ;
}
}
}
Modifying a conventional 270 deg rotation pot for 360 deg 'sensing' cannot work as the resistance element isn't a continuous stripe it starts @ 0 deg and stops @ 270 deg of rotation... Thus the little bit of plastic you removed was the rotation limiter
when the wiper of the pot is not in contact with the resistive strip or element it is an open circuit and therefore undefined.
360 deg pots are available (Very Expensive). However it is possible to use a multiturn pot and define the 0 and 359 deg points manually. This will achieve the same effect without any undefined operation.
Right the pot doesn't go above 1023... ( nor it goes under 0 )
What I meant is whet the pot gets in between its former maximum position and its minimum position
I get random values, jumping from 100 to 23 to 236 to 10 etc...
My guess is the potentiometer's ground is disconnected when set on a position between its Maximum and Minimum.
I'm looking for some programming manner to avoid this noise.
My guess is the potentiometer's ground is disconnected when set on a position between its Maximum and Minimum.
I'm looking for some programming manner to avoid this noise.
The ground isn't. The input is. And, there is no way to distinguish no/bad input from legitimate input. The usual advice, to not read a disconnected pin, applies in this case. The device you have is useless for your purposes. A rotary encoder is cheap, and seems to be what you are trying to create.
There is a way to do this programmatically but it will cost you two extra digital pins.
Connect the two digital pins to the ends of the pot and set them to be outputs.
Then put both to zero and measure the input, then put both to one and measure the input. If you get zero and 1023 for those two tests then you are in the contact zone so set one output to high and the other to low and take the measurement as normal. Then reverse the high and low and measure again. The measurements should be the mirror image of each other, that is measurement one should be the same as 1023 minus measurement two.