Annoying behavior while interfacing with arduino :/

For issues with communication ports, protocols, etc.
Post Reply
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Annoying behavior while interfacing with arduino :/

Post by Hezad »

Heya everyone :) Long time no see !

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
Arduino's code :

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);
    
}
Any idea ?

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
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

I would suggest to not use line input as it will return what's in the buffer at that point even if all your charactes haven't been received yet. It would probably be best to have the Arduino code sent out a cr/lf code after the data and then use input# for the Freebasic program. Alternately, you could make sure that the arduino is always sending out fixed length strings and then check the buffer for the correct number of characters.


Also, I would probably move the screenlock to just before the circle command.
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Post by Hezad »

Thanks for your suggestions, I'll test it !
Dinosaur
Posts: 1481
Joined: Jul 24, 2005 1:13
Location: Hervey Bay (.au)

Post by Dinosaur »

Hi all

Equally it can be cheap or bad pot's.
I have a lot of experience with old or bad pots. The wiper contact integrity can be comprimised and even when no vibration is present will return a high resistance.
Also make sure you know (or measure) what current is going through the pot and match them. A low wattage pot will cause problems as well as a pot that has a much higher wattage then needed.
As a final cure, connect a 0.1 micro farad ceramic capacitor across the pot power lines to filter out spikes.(if it is a 3 wire pot, otherwise dont)

Regards
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Post by Hezad »

phishguy > Hum.. by the way the println instruction sends a cr/lf chr automatically at the end of the string :/

dinosaur > Thanks for your idea :) But I just tried and it didn't change anything :/
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

ok, what happens if you use input# instead of line input#?
Hezad
Posts: 469
Joined: Dec 17, 2006 23:37
Contact:

Post by Hezad »

well, if I keep println on arduino's side and use input# in my fb code, checking for ("L" or "R") and "E" to keep the good value, the behavior stays the same.

If I don't use println, the program doesn't answer anymore (?) and crashes.

I noticed the problem is way more present on the Left value than on the right one but I don't understand why (I modified the circuit, changed the pots, added a cond., inverted ground and vcc, nothing to do, everything leads to this behavior)
Post Reply