Main > Raspberry Pi & Dev Board

mini outrun arcade built from scratch wheel question

<< < (2/3) > >>

PL1:

--- Quote from: oatcakeandy on April 06, 2023, 05:03:45 pm ---am i right in thinking once the pro micro has been programmed i can connect it via usb to the raspberry pi and it would recognise it as an input device

--- End quote ---
Yes, if you use the right sketch/firmware.

The "HID" description I used earlier stands for "Human Interface Devices".
- When you plug a HID into your computer, the computer sees it as a keyboard, mouse, gamepad, or composite device. (i.e. keyboard + mouse, two gamepads, etc.)

The Leonardo series of Arduinos, which includes the Pro Micro, are designed to be easily used as a HID that works with the default drivers in your Linux or Windows operating system.


--- Quote from: oatcakeandy on April 06, 2023, 05:03:45 pm ---would it be better to output to the gpio pins on the pi ?

--- End quote ---
Pro:
- You don't need the Pro Micro to act as the encoder.

Con:
- You need to know how to use the GPIO pins as inputs.  I know it can be done, but can't help you much with the process and/or code.
- Not sure how much lag, if any, you'll have with this setup.   :dunno

Based on the questions you're asking, it would probably be easier for you to use the Pro Micro approach.


Scott

oatcakeandy:
Thanks for the help mate..I'll let you know how I get on and hopefully show the final build.

oatcakeandy:
hi, me again.
im struggling with some of the code still.
i've amended some code i found on the net which had what i was looking for, pretty straightforward, nothing too complicated, i can get a pc to recognise the board and all inputs respond as should, however when its plugged into an rpi400 ( i have found that the board will only get recognised on usb3 input and an rpi400 is all i have at the minute) and go through the controller setup the buttons are accepted but not the potentiometer, sometimes only get Axis0- but never Axis 0+.
i then run Jstest-gtk which again sees all inputs and responds to the pot as should.
Any help would be most appreciated, i feel im going in circles.

this is the code i have been using....


//Arduino Joystick 2.0 Library, by MHeironimus (https://github.com/MHeironimus)
//Beginners Guide, by Daniel Cantore
//Example Code (Oct 2020), with in-depth commenting

//Initial Definitions and Setup
//Libary Inclusion
#include <Joystick.h>

//Define and Allocate Input Pins to memorable names
#define joyX A0
#define joyButton1 2
#define joyButton2 3
#define joyButton3 4
#define joyButton4 5
#define joyButton5 8
#define joyButton6 7

//Initializing Axis as Integers, at a 0 default value
int xAxis_ = 0;

//Setting up Buttons
//Updating a static variable gives greater stability than reading directly from the digital pin.
//Giving Default Values to the Buttons for later use
  int lastButton1State = 0;
  int lastButton2State = 0;
  int lastButton3State = 0;
  int lastButton4State = 0;
  int lastButton5State = 0;
  int lastButton6State = 0;

//Defining the Joystick
//The Joystick is defined in the following setup:
//Joystick(Joystick HID ID, Joystick Type, Button Count, Hat Switch Count, Include X, Include Y, Include Z, Include Rx, Include Ry, Include Rz, Include Rudder, Include Throttle, Include Accelerator, Include Brake, Include Steering
//Joystick HID ID: A Hex value identifier for HID Device Recognition (default: 0x03). DO NOT USE 0x01 or 0x02
//Joystick type: Define the type of joystick from the types supported. Types: DEFAULT Joystick (0x04 or JOYSTICK_TYPE_JOYSTICK), Gamepad (0x05 or JOYSTICK_TYPE_GAMEPAD), Multi-Axis Controller (0x08 or JOYSTICK_TYPE_MULTI_AXIS)
//Button Count: Number of Buttons shown to HID system (default: 32)
//Hat Switch Count: Number of Hat Switches, max 2. (default:2)
//Include X Axis: Determines whether the X axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Y Axis: Determines whether the Y axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Z Axis: Determines whether the Z axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rx Axis: Determines whether the X Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Ry Axis: Determines whether the Y Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rz Axis: Determines whether the Z Rotational axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Rudder: Determines whether a Rudder axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Throttle: Determines whether a Throttle axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Accelerator: Determines whether an Accelerator axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Brake: Determines whether a Brake axis is avalible for used by the HID system, defined as a bool value (default:true)
//Include Steering: Determines whether a Steering axis is avalible for used by the HID system, defined as a bool value (default:true)

Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 6, 0,true,false,false,false,false,false,false,false,false,false,false);

//Set Auto Send State
//Enables Auto Sending, allowing the controller to send information to the HID system, rather than waiting to be asked.
const bool initAutoSendState = true;

void setup() {
  //Initialize Buttons
  //Buttons set up between Digital Pin and Ground, following pin allocations from earlier on
  pinMode(joyButton1, INPUT_PULLUP);
  pinMode(joyButton2, INPUT_PULLUP);
  pinMode(joyButton3, INPUT_PULLUP);
  pinMode(joyButton4, INPUT_PULLUP);
  pinMode(joyButton5, INPUT_PULLUP);
  pinMode(joyButton6, INPUT_PULLUP);


  //Start Joystick - Needed to start the Joystick function libary
  Joystick.begin();
}

void loop() {
 
  //Axis Reading during Runtime
  //Setting Read functions for each axis and parsing correctly. The X axis will be used as an example for explanation

  //Reading the X Axis analog pin to the xAxis_ variable for processing
  xAxis_ = analogRead(joyX);
  //Mapping the X Axis data from a 0-1023 to 0-255 range for a smoother action
  xAxis_ = map(xAxis_,0,1023,0,255);
  //Set the Joystick X Axis value as the new, smoother, value
  Joystick.setXAxis(xAxis_);

 
  //Button Reading during Runtime
  //Setting Read functions for each button, using a state value for memory. Button 1 will be used as an example for explanation

  //Reading the current Button digital pin to the Current Button State for processing
 
  int currentButton1State = !digitalRead(joyButton1);
  //If loop - Check that the button has actually changed.
  if (currentButton1State != lastButton1State){
    //If the button has changed, set the specified HID button to the Current Button State
    Joystick.setButton(0, currentButton1State);
    //Update the Stored Button State
    lastButton1State = currentButton1State;
  }

    int currentButton2State = !digitalRead(joyButton2);
  if (currentButton2State != lastButton2State){
    Joystick.setButton(1, currentButton2State);
    lastButton2State = currentButton2State;
  }
    int currentButton3State = !digitalRead(joyButton3);
  if (currentButton3State != lastButton3State){
    Joystick.setButton(2, currentButton3State);
    lastButton3State = currentButton3State;
  }
  int currentButton4State = !digitalRead(joyButton4);
  if (currentButton4State != lastButton4State){
    Joystick.setButton(3, currentButton4State);
    lastButton4State = currentButton4State;
  }

    int currentButton5State = !digitalRead(joyButton5);
  if (currentButton5State != lastButton5State){
    Joystick.setButton(4, currentButton5State);
    lastButton5State = currentButton5State;
  }
    int currentButton6State = !digitalRead(joyButton6);
  if (currentButton6State != lastButton6State){
    Joystick.setButton(5, currentButton6State);
    lastButton6State = currentButton6State;
  }

//Pole Delay/Debounce
//To reduce unessecary processing, the frequency of the reading loop is delayed. The value(in ms) can be changed to match requirement
delay(10);
}

oatcakeandy:
Quick update.

i've followed this site and got something cobbled together and working

https://www.instructables.com/Zero-Delay-USB-Encoder-True-Analog-Joystick-Modifi/

However, i'd still like to know why i conna get the arduino to work for future builds and my sanity

Andy.

MrThunderwing:
I'm keeping an eye on this thread, be interested to see how it all turns out 👍

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version