Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Using Joystick code without Arduino IDE?

  1. #1
    Junior Member
    Join Date
    Mar 2016
    Posts
    3

    Using Joystick code without Arduino IDE?

    I want to do a custom Joystick project (following Kenton's guide, and maybe referencing some Djinny's work) without the Arduino IDE. I want to dive into more behind-the-scenes C code and get some practice with that world. The problem is of course that things get really complicated quite quickly.

    I've got a project built off the teensy template here: https://github.com/apmorton/teensy-template
    And I added the Teensy basic code to main.cpp.
    But when I try to make, I get an error about the Joystick variable.

    My question - what does selecting Tools > USB Type > Joystick actually *do*, and how do I replicate it sans the Arduino IDE?

    I've looked through the source for hours without much luck. I tried doing #define USB_HID, thinking that would do it, but no luck so far. Help appreciated.

    My main file looks like:

    Code:
    #include "WProgram.h"
    #define USB_HID
    
    extern "C" int main(void)
    {
    #ifdef USING_MAKEFILE
    
    	// To use Teensy 3.0 without Arduino, simply put your code here.
    	// For example:
    
    	pinMode(13, OUTPUT);
    	pinMode(0, INPUT_PULLUP);
    	pinMode(1, INPUT_PULLUP);	
    
    	while (1)
    	{
    		// read analog inputs and set X-Y position
    		Joystick.X(analogRead(3));
    		Joystick.Y(analogRead(2));
    
    		// read the digital inputs and set the buttons
    		Joystick.button(1, digitalRead(0));
    		Joystick.button(2, digitalRead(1));
    	}
    
    
    #else
    	// Arduino's main() function just calls setup() and loop()....
    	setup();
    	while (1) {
    		loop();
    		yield();
    	}
    #endif
    }
    The error I get it is

    Code:
    MacBook-Pro-2:teensy Andrew$ make
    [CXX]	src/main.cpp
    src/main.cpp: In function 'int main()':
    src/main.cpp:18:3: error: 'Joystick' was not declared in this scope
       Joystick.X(analogRead(3));
       ^
    make: *** [/Users//Dropbox/andrew//teensy/build/src/main.o] Error 1

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,206
    Maybe try with the Makefile that code with the code?

    https://github.com/PaulStoffregen/co...ensy3/Makefile

  3. #3
    Junior Member
    Join Date
    Mar 2016
    Posts
    3
    Quote Originally Posted by PaulStoffregen View Post
    Maybe try with the Makefile that code with the code?

    https://github.com/PaulStoffregen/co...ensy3/Makefile
    Hi Paul - thanks for the rapid response. I went back to the drawing board, using your bare Teensyduino materials (instead of the teensy-template). Still, I get the same problem.

    Code:
    /Applications/Arduino.app/Contents/Java/hardware/tools/arm/bin/arm-none-eabi-g++ -std=gnu++0x -felide-constructors -fno-exceptions -fno-rtti -Wall -g -Os -mcpu=cortex-m4 -mthumb -MMD -DF_CPU=48000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH -DUSING_MAKEFILE -D__MK20DX256__ -DARDUINO=10600 -DTEENSYDUINO=121 -I.  -c -o main.o main.cpp
    main.cpp: In function 'int main()':
    main.cpp:29:3: error: 'Joystick' was not declared in this scope
       Joystick.X(analogRead(3));
       ^
    make: *** [main.o] Error 1
    My code now looks like this.

    Code:
    #define USB_HID
    #include "WProgram.h"
    
    extern "C" int main(void)
    {
    #ifdef USING_MAKEFILE
    
    	// To use Teensy 3.0 without Arduino, simply put your code here.
    	// For example:
    
    	//blink test (detect something happened)
    	pinMode(13, OUTPUT);
    	int i = 3;
    	while (i > 0) {
    		digitalWriteFast(13, HIGH);
    		delay(500);
    		digitalWriteFast(13, LOW);
    		delay(500);
    		i--;
    	}
    
    	//now do joystick things?
    	pinMode(0, INPUT_PULLUP);
    	pinMode(1, INPUT_PULLUP);	
    
    	while (1)
    	{
    		// read analog inputs and set X-Y position
    		Joystick.X(analogRead(3));
    		Joystick.Y(analogRead(2));
    
    		// read the digital inputs and set the buttons
    		Joystick.button(1, digitalRead(0));
    		Joystick.button(2, digitalRead(1));
    	}
    
    #else
    	// Arduino's main() function just calls setup() and loop()....
    	setup();
    	while (1) {
    		loop();
    		yield();
    	}
    #endif
    }
    My question remains the same... what does selecting Tools > USB Type > Joystick actually *do*, and how do I replicate it sans the Arduino IDE?

    Would really appreciate your help!

  4. #4
    Junior Member
    Join Date
    Mar 2016
    Posts
    6
    Still investiagting, but so far i can tell you joystick is a class that need to be included, and its not the only one.
    In the arduino ide this done for you and u dobt see. I am also still figuring this out

  5. #5
    Junior Member
    Join Date
    Mar 2016
    Posts
    6
    see https://forum.pjrc.com/threads/33450...-ide-teensy3-2
    If you install the eclipse ide you will find a bunch of files that are required and loaded including the Joystick class file that has the functions.
    So if your makefile doesnt include this and other files you will get the errors.

  6. #6
    Junior Member
    Join Date
    Mar 2016
    Posts
    3
    Got it figured out!

    Re: includes - the #include "WProgram.h" includes all the libraries in the teensy3 folder already!

    The solution: for some reason #define USB_HID doesn't work. You have to change the Makefile includes from USB_SERIAL to USB_HID. The configurable options will look like this:

    Code:
    # configurable options
    #OPTIONS = -DUSB_SERIAL -DLAYOUT_US_ENGLISH
    #OPTIONS = -DUSB_HID -DLAYOUT_US_ENGLISH
    OPTIONS = -DUSB_SERIAL_HID -DLAYOUT_US_ENGLISH

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •