Wondering about turning adapting instructable to make a joystick with buttons

Really good and it works great but I now want to add a few buttons to the thing as well. I know nothing about Arduino beyond making this following the guide but could do with some help adding buttons to it if that is at all possible.

Also the guide there only works with an old version of Arduino, so could anyone help to update it?

Thanks for reading.

Please post your code here. And please post the code from your project, NOT from the Instructables website.

Please also use the </> button

so that your code looks like this

...R

Thanks for the speedy response, here is the code that I currently have uploaded:

ThumbState_t thumbSt;

const bool DEBUG = false;  // set to true to debug the raw values

int xPin = A2;
int yPin = A3;
int xZero, yZero;
int xValue, yValue;
int deadzone = 5;  // smaller values will be set to 0

void setup(){
 	pinMode(xPin, INPUT);
 	pinMode(yPin, INPUT);

 	if(DEBUG) {
 		Serial.begin(9600);
 	}

	// calculate neutral position
	xZero = analogRead(xPin);
	yZero = analogRead(yPin);

	thumbSt.xAxis = 0;
	thumbSt.yAxis = 0;
}

void loop(){
	xValue = analogRead(xPin) - xZero;
	yValue = analogRead(yPin) - yZero;

	if(abs(xValue) < deadzone) {
		xValue = 0;
	}
	if(abs(yValue) < deadzone) {
		yValue = 0;
	}

	thumbSt.xAxis = map(xValue, 400, -400, -32768, 32768);  // here the axis is inverted
	thumbSt.yAxis = map(yValue, -400, 400, -32768, 32768);

 	if(DEBUG) {
 		Serial.print("X: ");
		Serial.println(xValue);
 		Serial.print("Y: ");
		Serial.println(yValue);
	}

	// Send to USB
	Thumbstick.setState(&thumbSt);
}

There seems to be stuff missing at the start of your code.

Also, can you explain what the different parts of the program do ?

Is the code for your buttons included in this ?

You haven't said what you want the buttons to do !

...R

That is everything in the Arduino file before I uploaded it to the Pro Micro board.

I cannot explain what the program does as I know nothing about Arduino coding and simply followed the guide linked to make it work with Arduino version 1.0.5 r2 and have not written any code for buttons to work.

With my (relatively) uneducated eyes the code appears to first set the dead zone for the thumbstick, then check to see if the current position is within the dead zone, then actually check where the thumbstick is in the X and Y axis. Finally it throws the data out of the USB to my PC. Everything else is beyond me currently.

What I want the buttons to do is to act as extra buttons for my joystick which the existing mini joystick is connected to (which provides me with thumb control over thrusters for Elite Dangerous and Star Citizen). Perhaps toggle switches or to act as standard push button switches like you get as standard on joysticks.

Thanks for the help.

adding the code for the buttons is straight forward:
1- add the setup code for the buttons
2- add the:

 buttonA= digitalRead(button_A);

3-add the IF statements for the buttons.

reading buttons

Ok so would I be right with something like this:

const int buttonPin = 2;

int buttonState = 0;        

void setup() {
  pinMode(buttonPin, INPUT);     
}

void loop(){
  buttonState = digitalRead(buttonPin);
  Serial.println(buttonState);
  delay(1); 
}

Am I using the right code to push the info back to the PC? Using thiswhich I may be mistaken about?

First coding I have done in about a decade, so making a lot of assumptions...

I'm H/W. A S/W person will have to answer that.

Very simple servo button code.

//zoomkat servo button test 7-30-2011
//Powering a servo from the arduino usually *DOES NOT WORK*.

#include <Servo.h>
int button1 = 4; //button pin, connect to ground to move servo
int press1 = 0;
Servo servo1;

void setup()
{
  pinMode(button1, INPUT);
  servo1.attach(7);
  digitalWrite(4, HIGH); //enable pullups to make pin high
}

void loop()
{
  press1 = digitalRead(button1);
  if (press1 == LOW)
  {
    servo1.write(160);
  }
  else {
    servo1.write(20);
  }
}

Vindicore:
That is everything in the Arduino file before I uploaded it to the Pro Micro board.

I don't believe that can be true.

When I try to compile it I get the expected error

ScratchPad.ino:1: error: ‘ThumbState_t’ does not name a type

(ScratchPad.ino is the name of the file I use for testing Forum code)

...R

Robin2:
I don't believe that can be true.

When I try to compile it I get the expected error

ScratchPad.ino:1: error: ‘ThumbState_t’ does not name a type

(ScratchPad.ino is the name of the file I use for testing Forum code)

...R

Well that is all the code I have in my arduino file however it is with an old version of arduino and if you check out the instructable I did replace a couple of files as well, no clue what they do though!

Vindicore:
if you check out the instructable I did replace a couple of files as well, no clue what they do though!

I am not going to take the time to study the instructables - I will use that time to help someone else.
I am not going to guess what files you replaced

If you want help YOU need to help us to help you. Make it so we know all about this project that you know.

...R

Shrugs

I simply followed the guide through that link as I have stated and now want to adapt it and hopefully learn a bit about using Arduino in the process; if you don't want to follow the link I can copy and paste the thing here, see the relevant part below:

Make backups of your original HID.cpp and USBAPI.h files inside your arduino/hardware/arduino/cores/arduino folder and replace them with the two files from this Instructable. (Move the backup files out of the original folder or Arduino will complain if they have .cpp or .h as the extension!).

In the file HID.cpp are three lines starting at line 27.

// #define KBAM_ENABLED
// #define JOYSTICK_ENABLED
#define THUMBSTICK_ENABLED
In my version of the file, #define THUMBSTICK_ENABLED is uncommented which is fine for our two axis joystick.

If you later want to use one of the other settings for a project, just uncomment the line you need (only one at a time!).

Download thumbstick.ino and open it with the Arduino IDE set the board to SparkFun Pro Micro 5V/16MHz and upload the sketch.

Depending on the orientation of the joystick you might have to swap the second and third parameters to the map() functions from "400, -400" to "-400, 400", this inverts the axes.

Now Windows should have a new entry named "USB IO Board" under game controllers.

The HDD.cpp and USBAPI.h files can be found through those links.

Vindicore:
Shrugs

Shrugs Shrugs

The ball is in your court. Post all of your code here. We need to see the code YOU have used - not the code on the Instructables website.

...R

Hi, I too am trying to follow this tutorial. I get this error:

thumbstick:1: error: 'ThumbState_t' does not name a type
thumbstick.ino: In function 'void setup()':
thumbstick:23: error: 'thumbSt' was not declared in this scope
thumbstick.ino: In function 'void loop()':
thumbstick:38: error: 'thumbSt' was not declared in this scope
thumbstick:49: error: 'Thumbstick' was not declared in this scope

This code I am running is below, this is as provided in the Instructable. As mentioned in the posts above the code requires USBAPI.h and HID.CPP files to be placed in the Arduino Cores folder. I assume they're slightly amended versions of basic files required to allow the Pro Micro board to read as a USB controller device. The code allows for 1 PSP-1000 analogue thumbstick to be connected to the board and read as an analogue input. The thumbstick (two potentiometers) provide analogue signal to A2 and A3 pins, other two connections are Ground and VCC.

The code is as follows:

ThumbState_t thumbSt;

const bool DEBUG = false;  // set to true to debug the raw values

int xPin = A2;
int yPin = A3;
int xZero, yZero;
int xValue, yValue;
int deadzone = 5;  // smaller values will be set to 0

void setup(){
 	pinMode(xPin, INPUT);
 	pinMode(yPin, INPUT);

 	if(DEBUG) {
 		Serial.begin(9600);
 	}

	// calculate neutral position
	xZero = analogRead(xPin);
	yZero = analogRead(yPin);

	thumbSt.xAxis = 0;
	thumbSt.yAxis = 0;
}

void loop(){
	xValue = analogRead(xPin) - xZero;
	yValue = analogRead(yPin) - yZero;

	if(abs(xValue) < deadzone) {
		xValue = 0;
	}
	if(abs(yValue) < deadzone) {
		yValue = 0;
	}

	thumbSt.xAxis = map(xValue, 400, -400, -32768, 32768);  // here the axis is inverted
	thumbSt.yAxis = map(yValue, -400, 400, -32768, 32768);

 	if(DEBUG) {
 		Serial.print("X: ");
		Serial.println(xValue);
 		Serial.print("Y: ");
		Serial.println(yValue);
	}

	// Send to USB
	Thumbstick.setState(&thumbSt);
}

I can not figure out what is causing the error, the 'thumbSt' points to something in the HID.CPP file as far as I can tell, however am very new to this and am trying to learn how this works,

Thanks :slight_smile:

HID.cpp (19 KB)

USBAPI.h (6.3 KB)

There needs to be something before this line

ThumbState_t thumbSt;

in order for the compiler to know what sort of thing ThumbState is.

Often the missing something is a reference to a library - for example #include

...R

All a computer program is, are descriptions of things. There is a -small- list of things that a compiler already understands. The other 99% of it, is YOUR code describing the rest. What it is, what to do with it. No magic, that's it, that's all code is.

The compiler errors you are seeing is it getting worked up because your code is calling for stuff that is not described. It saying "Look! Look! No one told me what this word means!"

When an old coder looks at your code the FIRST thing they see is : ThumbState_t. What the heck is this? Well obviously its not described in here, so there must be some files missing.

And, when for the 5,865,986,876 time the coder tries to point this out. They get back "I know nothing! I'm a noob! Make it work! I copied it from the internet. Arn't I cute? --shrug*-- "

The old coder hasn't had his coffee yet, there's no cigarettes anymore, everyone wants to code in C#. Does all this make him want to help you for free?

P.S. I see he did. I'm impressed.

-jim lee

Thanks, Jim. When I help people out with technical advice on the internet, yeah, I do it for free, particularly for noobs. It's why the internet is so great. "Arn't (sic) I cute? --shrug*--"... what gives?

ThumbState_t appears to be a state referenced in the HID.cpp, and a term created by a typedef in the USBAPI.h - I think.

Robin2, thanks, I understand that, is a .h file a library? Is this different to a header file?

Just old and cranky.

-jim lee

@alexkyriac Library files typically come in pairs. The .h file defines, for both the code of your program and the code of the library .cpp file, things that both need. Basically the interface between the two.

The .cpp file or .c file typically holds code (what to do) and declarations that your program doesn't need to know about.

The .h file is the header file for both your program and the library code.

-jim lee