B4R Question Joystick KY023 strange result

lucdrb

Member
Licensed User
Hi,

The result from the joystick aren't the same with the arduino IDE program code and the B4R IDE program code.

josystick position normal:

B4R

X = 3
Y = 551
Button = 1
X = 3
Y = 550
Button = 1
X = 3
Y = 551
Button = 1
Arduino
x: 525 Y: 511 Z: 1
x: 525 Y: 511 Z: 1
x: 524 Y: 512 Z: 1
Stick move to the left

B4R
A
X = 546
Y = 349
Button = 1
X = 545
Y = 349
Button = 1
Arduino
x: 1023 Y: 512 Z: 1
x: 1023 Y: 512 Z: 1
Same strange behavior with the other position
Thanks for your help.

The code are:
B4R
B4X:
'WIRE LEGEND for KY-023
'GND = GND
'+5V = 5V
'VRy = A0
'VRx = A1
'SW = D2
'*************************
'***     BOARD TYPE    ***
'***    Arduino All    ***
'*************************

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
 
    Public Serial1 As Serial
    Private Switch, VRx, VRy As Pin
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
 
    Switch.Initialize(2, Switch.MODE_INPUT_PULLUP)
    VRx.Initialize(VRx.A0, VRx.AnalogRead)
    VRy.Initialize(VRy.A1, VRy.AnalogRead)
     AddLooper("ReadJoystick")
End Sub

Sub ReadJoystick
    Log("X = ", VRx.AnalogRead)
    Log("Y = ", VRy.AnalogRead)
    Log("Button = ", Switch.DigitalRead)
    Delay(250)
End Sub
Arduino
B4X:
const int sw_pin = 4;
const int x_pin = 0;
const int y_pin = 1;

void setup() {
 pinMode(sw_pin, INPUT);
 digitalWrite(sw_pin, HIGH);
 Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.print("x: ");
Serial.print(analogRead(x_pin));
Serial.print("  Y: ");
Serial.print(analogRead(y_pin));
Serial.print("  Z: ");
Serial.println(digitalRead(sw_pin));
delay(250);
}
 

Erel

Administrator
Staff member
Licensed User
It is a mistake to use Delay here. You should use a timer instead.

Why are you using 0 and 1 for the pins in the Arduino code? This is not the same pins as A0 and A1.
 

lucdrb

Member
Licensed User
I've change the delay by a timer and numbered the pin A0 and A01 (the pin were always A0 and A01 into the arduino uno it was and error in writing) but the result is the same as previously

Here the new codes.

B4X:
const int sw_pin = 2;
const int x_pin = A0;
const int y_pin = A1;

void setup() {
 pinMode(sw_pin, INPUT);
 digitalWrite(sw_pin, HIGH);
 Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.print("x: ");
Serial.print(analogRead(x_pin));
Serial.print("  Y: ");
Serial.print(analogRead(y_pin));
Serial.print("  Z: ");
Serial.println(digitalRead(sw_pin));
delay(250);
}
B4X:
'WIRE LEGEND for KY-023
'GND = GND
'+5V = 5V
'VRy = A0
'VRx = A1
'SW = D2
'*************************
'***     BOARD TYPE    ***
'***    Arduino All    ***
'*************************

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
 
    Public Serial1 As Serial
    Private Switch, VRx, VRy As Pin
    Public timer1 As Timer
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
 
    Switch.Initialize(2, Switch.MODE_INPUT_PULLUP)
    VRx.Initialize(VRx.A0, VRx.AnalogRead)
    VRy.Initialize(VRy.A1, VRy.AnalogRead)
    timer1.Initialize("timer1_Tick", 250)
    timer1.Enabled = True
End Sub

Sub Timer1_tick
    Log("X = ", VRx.AnalogRead)
    Log("Y = ", VRy.AnalogRead)
    Log("Button = ", Switch.DigitalRead)
End Sub
 

thetahsk

Active Member
Licensed User
I've change the delay by a timer and numbered the pin A0 and A01 (the pin were always A0 and A01 into the arduino uno it was and error in writing) but the result is the same as previously
What happend if you connect to analog ports A1 and A2 ?

B4X:
VRx.Initialize(VRx.A1, VRx.AnalogRead)
VRy.Initialize(VRy.A2, VRy.AnalogRead)
 

Erel

Administrator
Staff member
Licensed User
It should be:
B4X:
VRx.Initialize(VRx.A0, VRx.MODE_INPUT)
VRy.Initialize(VRy.A1, VRy.MODE_INPUT)
 
Top