Thread: joystick program

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    joystick program

    I'm creating a joystick program. I plug in the joystick, run the program, and numbers are printed out based on what buttons are being pressed on the joystick. This is going to be used for a robot.
    However, I'm having a problem with it. I want the program to work so that I don't have to hold down the buttons when the numbers are being printed.

    Code:
    //--- Run joystick session
    	running = true;
    	while (running) {
    		//--- Clear memory, load data structure
    		memset(&jixStick, 0x000, sizeof(JOYINFOEX));
    		jixStick.dwSize = sizeof(JOYINFOEX);
    		jixStick.dwFlags = JOY_RETURNALL;
    		joyGetPosEx(JOYSTICKID1, &jixStick);
    		
    		numZpos = jixStick.dwZpos / 524;
    		numXpos = jixStick.dwXpos / 524;
    
    //If none of the joystick buttons are pressed
    		if (!(jixStick.dwButtons & JOY_BUTTON5) && !(jixStick.dwButtons & JOY_BUTTON6) && !(jixStick.dwButtons & JOY_BUTTON4) && !(jixStick.dwButtons & JOY_BUTTON1)) {
    			cout << "The value of the Z position is: " << "  " << numZpos << endl;
    		}
    
    		if ((jixStick.dwButtons & JOY_BUTTON5)) {
    			reverse = myArray[0] / 524;
    			cout << "Moving backwards..." << "   " << reverse << endl;
    		}
    }
    So if the 5th button on the joystick is pressed, the "Moving backwards" value is printed. However it only prints when I'm holding the button. I just want to press the button once, and then it prints when I'm not holding the button. Any ideas on how I can do this?

  2. #2
    Banned
    Join Date
    May 2008
    Location
    Four Dots Planet
    Posts
    72
    set a flag when the button is down update based on the flag only reset the flag when the button up was received that should be working for you.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    4
    Quote Originally Posted by (::) View Post
    set a flag when the button is down update based on the flag only reset the flag when the button up was received that should be working for you.
    So you're saying that I should set an integer and then give it a value when the button is pressed? Then when the button is not pressed, give it another value?

    Code:
    bool zCheck = false;
    if ((jixStick.dwButtons & JOY_BUTTON5)) {
    zCheck = true;
    }
    else {
    zCheck = false;
    }
    I'm not sure if that would work or not since the button will still have to be pressed in order for it to be true.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM