B4R Question Joystick KY023 strange result

Discussion in 'B4R Questions' started by lucdrb, Oct 27, 2019.

  1. lucdrb

    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

    Arduino
    Stick move to the left

    B4R
    A
    Arduino
    Same strange behavior with the other position
    Thanks for your help.

    The code are:
    B4R
    Code:
    '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(2Switch.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
    Code:
    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);
    }
     
  2. Erel

    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.
     
  3. lucdrb

    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.

    Code:
    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);
    }
    Code:
    '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(2Switch.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
     
  4. thetahsk

    thetahsk Active Member Licensed User

    What happend if you connect to analog ports A1 and A2 ?

    Code:
    VRx.Initialize(VRx.A1, VRx.AnalogRead)
    VRy.Initialize(VRy.A2, VRy.AnalogRead)
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    It should be:
    Code:
    VRx.Initialize(VRx.A0, VRx.MODE_INPUT)
    VRy.Initialize(VRy.A1, VRy.MODE_INPUT)
     
  6. lucdrb

    lucdrb Member Licensed User

    Many thank Erel Everything is OK Now :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice