I bought an arduino recently and its a real pleasure to play with :) And its way funnier when its with freebasic ! But there's something I don't get right now :0
I'm using Two potentiometers to control the position of a circle in a fbgfx graphic screen (left pot to move along x coord and right one along y coord).
To communicate with FB, I "asked" my arduino to send two values : "L" with the left value (0 to 1024) and "R" with the right value (same range) which I get in my program.
The communication seems to be ok (the circle moves on the coords I want it to be) but there's a glitch ! The Xpos and Ypos values seem to have a low value sometimes even if I don't touch the pots. Can it be an electronic problem ? (precision's missing ? bad quality pots ?) or is there something wrong in my code ?
FB code :
Code: Select all
Screenres 640,480,32,2
Const XDiv = 640/1024
Const YDiv = 480/1024
Const TRESH = 50
OPEN COM "COM3:9600,N,8,1,RS,CS,DS,CD" for input AS #1
Dim as integer Xpos,Ypos,oldxpos,oldypos
Dim as string inv
Do : screenlock : cls
Line Input #1, InV
oldxpos = xpos
oldypos = ypos
If left(InV,1) = "L" then
XPos = Val(right(InV,Len(Inv)-1))
end if
If left(InV,1) = "R" then
YPos = Val(right(InV,Len(Inv)-1))
end if
Circle(XPos*XDiv,Ypos*YDiv),10,rgb(255,155,155),,,,f
Screenunlock : sleep 1,1
Loop until multikey(&h01)
Close #1
Code: Select all
int potPin = 2;
int potPin2 = 3;
int val = 0;
int val2 = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin);
val2 = analogRead(potPin2);
Serial.print("L");
Serial.println(val);
Serial.print("R");
Serial.println(val2);
delay(50);
}
Thanks !
NB : both pots are the same values but one is logarithmic and the other is linear. But I tried to exchange both and it didn't change the behavior