Analog joystick code ?

tmack

Member
I am trying to interface with an analog joystick. I have to code as :

main:
readadc 4,b1
if b1<200 then flsh
goto main

flsh:high 1
pause 1000
low 1
pause 1000
goto main

flsh2: high 1
pause 100
low 1
pause 100
goto main


That seems to be working good when I push the lever 1/2 way my led is flashing "flsh ". I have noticed at about <120 there could be another "cutoff" like make the led flash faster "flsh2" when the lever is all of the way up.My question is how do I get the code to read if it is <120 but not between< 120 and <200 . So when the lever reached the first point "<200" the led would flash slow as in "flsh", when I continued pushing the lever up to "<120" it would make my led flash more quicklyas in "flsh2". Thanks for your help.
 

Technical

Technical Support
Staff member
Code:
main:
readadc 4,b1
[B]if b1<120 then flsh2 'less than 120
if b1<200 then flsh ' less than 200 but greater than 120[/B]
goto main

flsh:high 1
pause 1000
low 1 
pause 1000
goto main

flsh2: high 1
pause 100
low 1 
pause 100
goto main
 

tmack

Member
WHen I tried that code it seems to get "stuck" in the <200 flsh and flashes slowly even when I move the lever to the top"<120 Position"
 

Technical

Technical Support
Staff member
You could use this

If b1<200 and b1 >120 then flsh
if b1<120 then flsh2

however this
if b1<120 then flsh2
If b1<200 then flsh

does the same thing, because if it less than 120 it will always use the first line, so to get to the second line it must already be bigger than 120!
 

tmack

Member
Seems like I have to use the alternate method to get it to work right for some reason. I used :

If b1<200 and b1 >120 then flsh
if b1<120 then flsh2,

and now it is working fine , Thanks Tech!
 

Technical

Technical Support
Staff member
If it got stuck in 200 loop you incorrectly had

If b1<200 then flsh
if b1<120 then flsh2

rather than

if b1<120 then flsh2
If b1<200 then flsh

It makes a big difference, read line by line carefully!
 

tmack

Member
Do
b0 = "0"
readadc 4,b1
If b1>55 Then : b0 = "1" : End If
If b1<45 Then : b0 = "2" : End If

SerOut 0, N2400, (b0)
Pause 100
Loop

The above code is working for one axis of the joystick sending out rs232 but the next isn't working for two axis' can anyone help me see the error in my ways?

Do
b0 = "0"
readadc 4,b1
readadc 1,b2
If b1>55 Then : b0 = "1" : End If
If b1<45 Then : b0 = "2" : End If
If b2>100 Then : b0 = "3" : End If
If b2<45 Then : b0 = "4" : End If
SerOut 0, N2400, (b0)
Pause 100
Loop


AS ALWAYS, Thanks for any help.
 

inglewoodpete

Senior Member
You'r overwriting your variable when analysing the second axis. Eg if b2 is over 100 or under 45, it will overwite the result of the b1 test.

Try the following:
Code:
Do
    b0 = "0"
    b3 = "0"
    readadc 4, b1
    readadc 1, b2
    If b1 > 55 Then 
        b0 = "1"
    ElseIf b1 < 45 Then
        b0 = "2"
    End If
    If b2 > 100 Then
        b3 = "3"
    ElseIf b2 < 45 Then
        b3 = "4"
    End If
    SerOut 0, N2400, (b0, b3)
    Pause 100
Loop
 

hippy

Technical Support
Staff member
Not sure exactly what you are trying to achieve, but if you want to determine which quadrant the joystick is in -

Code:
 0 | 1 | 2
---+---+---
 3 | 4 | 5
---+---+---
 6 | 7 | 8
You could use something like this ...

Code:
Do
  ReadAdc 4, b1
  ReadAdc 1, b2
  Select case b1
    case < 45  : b0 = "0"
    case < 55  : b0 = "3"
    else       : b0 = "6"
  End Select 
  Select case b2
    case < 45  : b0 = b0 + 0
    case < 101 : b0 = b0 + 1
    else       : b0 = b0 + 2
  End Select 
  SerOut 0, N2400, (b0)
  Pause 100
Loop
 
Last edited:
Top