UnoJoy flight simulator controls?

Hello everyone!

I am building a flight simulator controls, based on UnoJoy, with 6 axis and 12 buttons. The unojoy sample, is the basic, and works like ordinary joystick. When you press button the function is activated, and when it's not pressed it does nothing :slight_smile: Thats perfect, but for some funtions I need some other tricks, like for example landing gear or lights control. For this one I want to use a fixed switches, that when I will switch it on, and the gear will go down or up. But the problem is that if I am holding the switch on, the button is switching on an off.
I need to make it somehow, that it will activate the function only once for a moment, when it is switched on. I hope I explaned everything understandable :slight_smile: If someone has any idea what the code for this job should look like, I would enjoy to have a look at it :slight_smile:
Thanks!

Have a look at http://www.arduino.cc/en/Tutorial/Switch

Thanks, looks like this one will do the job. But I am still fresh in programing, so a little bit help would be usefull.
Here’s the code of a joystick. How I should this function to the joystic and where it should be?

#include “MegaJoy.h”

void setup(){
setupPins();
setupMegaJoy();
}

void loop(){
// Always be getting fresh data
megaJoyControllerData_t controllerData = getControllerData();
setControllerData(controllerData);
}

void setupPins(void){
// Set all the digital pins as inputs
// with the pull-up enabled, except for the
// two serial line pins
for (int i = 2; i <= 54; i++){
pinMode(i, INPUT);
digitalWrite(i, HIGH);
}
}

megaJoyControllerData_t getControllerData(void){

// Set up a place for our controller data
// Use the getBlankDataForController() function, since
// just declaring a fresh dataForController_t tends
// to get you one filled with junk from other, random
// values that were in those memory locations before
megaJoyControllerData_t controllerData = getBlankDataForMegaController();
// Since our buttons are all held high and
// pulled low when pressed, we use the “!”
// operator to invert the readings from the pins
for (int i = 2; i < 54; i++){
controllerData.buttonArray[(i - 2) / 8] |= (!digitalRead(i)) << ((i - 2) % 8);
}

// Set the analog sticks
// Since analogRead(pin) returns a 10 bit value,
// we need to perform a bit shift operation to
// lose the 2 least significant bits and get an
// 8 bit number that we can use
controllerData.analogAxisArray[0] = analogRead(A0);
controllerData.analogAxisArray[1] = analogRead(A1);
controllerData.analogAxisArray[2] = analogRead(A2);
controllerData.analogAxisArray[3] = analogRead(A3);
controllerData.analogAxisArray[4] = analogRead(A4);
controllerData.analogAxisArray[5] = analogRead(A5);

// And return the data!
return controllerData;
}

Can I suggest that you edit your post and put the code in code tags ?
Highlight it and click the # button above the smileys.

It prevents the code being mangled, unless you really do have a smiley in it !

Yop, you are right, sorry, my mistake, here’s the corect code.

#include "MegaJoy.h"

void setup(){
  setupPins();
  setupMegaJoy();
}

void loop(){
  // Always be getting fresh data
  megaJoyControllerData_t controllerData = getControllerData();
  setControllerData(controllerData);
}

void setupPins(void){
  // Set all the digital pins as inputs
  // with the pull-up enabled, except for the
  // two serial line pins
  for (int i = 2; i <= 54; i++){
    pinMode(i, INPUT);
    digitalWrite(i, HIGH);
  }
}

megaJoyControllerData_t getControllerData(void){
 
  // Set up a place for our controller data
  //  Use the getBlankDataForController() function, since
  //  just declaring a fresh dataForController_t tends
  //  to get you one filled with junk from other, random
  //  values that were in those memory locations before
  megaJoyControllerData_t controllerData = getBlankDataForMegaController();
  // Since our buttons are all held high and
  //  pulled low when pressed, we use the "!"
  //  operator to invert the readings from the pins
  for (int i = 2; i < 54; i++){
    controllerData.buttonArray[(i - 2) / 8] |= (!digitalRead(i)) << ((i - 2) % smiley-cool;
  }
 
  // Set the analog sticks
  //  Since analogRead(pin) returns a 10 bit value,
  //  we need to perform a bit shift operation to
  //  lose the 2 least significant bits and get an
  //  8 bit number that we can use
  controllerData.analogAxisArray[0] = analogRead(A0);
  controllerData.analogAxisArray[1] = analogRead(A1);
  controllerData.analogAxisArray[2] = analogRead(A2);
  controllerData.analogAxisArray[3] = analogRead(A3);
  controllerData.analogAxisArray[4] = analogRead(A4);
  controllerData.analogAxisArray[5] = analogRead(A5);
 
  // And return the data!
  return controllerData;
}

That code does not compile or auto format

I think it’s better you’ll take at a look at a full files, couse this was only the part of the code.

MegaJoyArduinoSample.ino (2.03 KB)

MegaJoy.h (9.14 KB)