Cleaning up code, and reducing amount of typing

I am probably on a global shit list, so no one replies to any of my posts, but i'll try one last time,

i have been playing about making an UNO into a USB HID joystick, it's taken me a couple of days of playing around... half of that was time taken to put the UNO into DFU more, flash the 16u2 chip, unplug and re-plug the usb cable, open a joystick test proggy, find it's not working, UNO into DFU mode, flash back to serial.... repeat about 200 times.

But i have finally got a matrix keypad connected to an UNO, sending individual keypad buttons as USB joystick buttons, my script is:

/* USB HID stuff written by Darran Hunt,
hex file for flashing  the 16u2 chip is found on his site http://hunt.net.nz/users/darran/
*/

#include <Keypad.h> //keypad library

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
//define characters for the buttons of the keypad
char keys[ROWS][COLS] = {
  {'1', '2', '3'},
  {'4', '5', '6'},  //electrical keypad layout
  {'7', '8', '9'},
  {'A', '0', 'B'}
};
byte rowPins[ROWS] = {5, 4, 3, 2,}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);


#define NUM_BUTTONS	40    //lovely buttons
#define NUM_AXES	8      //annoying axis not needed  

typedef struct joyReport_t {
  int16_t axis[NUM_AXES];
  uint8_t button[(NUM_BUTTONS + 7) / 8]; // 8 buttons per byte
} joyReport_t;

joyReport_t joyReport;

void setup()
{ keypad.addEventListener(keypadEvent); //to allow button press and release
  Serial.begin(115200); //talk to the pooter as fast as you can
  delay(200);


  for (uint8_t ind = 0; ind < 8; ind++) {
    joyReport.axis[ind] = ind * 1000;      //axis maths
  }

  for (uint8_t ind = 0; ind < sizeof(joyReport.button); ind++) {    //buttons maths
    joyReport.button[ind] = 0;
  }
}

// Send an HID report to the USB interface
void sendJoyReport(struct joyReport_t *report)
{
  Serial.write((uint8_t *)report, sizeof(joyReport_t));
}

// turn a button on
void setButton(joyReport_t *joy, uint8_t button)
{
  uint8_t index = button / 8;
  uint8_t bit = button - 8 * index;

  joy->button[index] |= 1 << bit;
}

// turn a button off
void clearButton(joyReport_t *joy, uint8_t button)
{
  uint8_t index = button / 8;
  uint8_t bit = button - 8 * index;

  joy->button[index] &= ~(1 << bit);
}

uint8_t button = 0;	// current button




void loop() {
  char key = keypad.getKey();    //read which key is pressed
}

void keypadEvent(KeypadEvent key) {    //read key as pressed or released

  switch (keypad.getState()) {

 // read keys, send joystick buttons
    case PRESSED:
      if (key == '1')  {
        setButton(&joyReport, button = 0);
      }
      if (key == '2')  {
        setButton(&joyReport, button = 1);
      }
      if (key == '3')  {
        setButton(&joyReport, button = 2);
      }
      if (key == '4')  {
        setButton(&joyReport, button = 3);
      }
      if (key == '5')  {
        setButton(&joyReport, button = 4);
      }
      if (key == '6')  {
        setButton(&joyReport, button = 5);
      }
      if (key == '7')  {
        setButton(&joyReport, button = 6);
      }
      if (key == '8')  {
        setButton(&joyReport, button = 7);
      }
      if (key == '9')  {
        setButton(&joyReport, button = 8);
      }
      if (key == 'A')  {
        setButton(&joyReport, button = 9);
      }
      if (key == '0')  {
        setButton(&joyReport, button = 10);
      }
      if (key == 'B')  {
        setButton(&joyReport, button = 11);
      }
      break;

    case RELEASED:
      if (key == '1')  {
        clearButton(&joyReport, button = 0);
      }
      if (key == '2')  {
        clearButton(&joyReport, button = 1);
      }
      if (key == '3')  {
        clearButton(&joyReport, button = 2);
      }
      if (key == '4')  {
        clearButton(&joyReport, button = 3);
      }
      if (key == '5')  {
        clearButton(&joyReport, button = 4);
      }
      if (key == '6')  {
        clearButton(&joyReport, button = 5);
      }
      if (key == '7')  {
        clearButton(&joyReport, button = 6);
      } if (key == '8')  {
        clearButton(&joyReport, button = 7);
      }
      if (key == '9')  {
        clearButton(&joyReport, button = 8);
      }
      if (key == 'A')  {
        clearButton(&joyReport, button = 9);
      }
      if (key == '0')  {
        clearButton(&joyReport, button = 10);
      }
      if (key == 'B')  {
        clearButton(&joyReport, button = 11);
      }

      break;

  }
  sendJoyReport(&joyReport);  //send it via usb to the pooter
  delay(10);

}

This is only for a 12 button keypad, i want to use a 36 button keypad with it, and i really don't fancy adding all the extra 'if key == xx' bits manually,

i've often seen people do something that adds the numbers up by one as the code progresses, so a single line of code replaces many lines, otent to turn on led's on pins 2 to 12 etc.

Would that work in this case?

Also, are there any glaring errors in my script?? things i don't need or could do differently,
wondering about the use of only 2 'break' commands, should i break after each button test? only one button will be pressed down at a time, and another input is not needed until the original button is released and a new one pressed,

And i hope this script can help anyone else trying to turn an UNO R3 into a USB joystick, as i found no completed scripts to do this for a 40 button joystick, only few for PS3 sticks,

To add numbers up in a loop do the following:

int something = 0;

for ( something < 20; something++) { 
    // turn the pin on:
    digitalWrite(thisPin, HIGH);   
    delay(timer);                  
    // turn the pin off:
    digitalWrite(thisPin, LOW);    
  }

This will turn the pin off 20 times. Another variation would be this:

int something = 0;
int this = 20;

for ( something < 20; something++) { 
    int this = this + 10
  }

This would make the variable this start from 20 and then go up in tens.

Output = 20, 30, 40, 50, 60

Hopefully this helps

I am probably on a global shit list, so no one replies to any of my posts

No. It's that you are doing something that doesn't attract a lot of interest.

bushn:
To add numbers up in a loop do the following:

Your for lop examples will not compile. I think you will have more success with something like this:

for (int something = 0; something < 20; something++)
{
    ...

Use a lookup table for the key values with a pair of parallel arrays.

char PROGMEM keys[] = { '0', '1', '2', '3', '4', .... 'B' };
char PROGMEM values[] = {0, 1, 2, 3, 4, ... 12 };

When you get a button pressed, search through the first array until you find the value, then use that index for the second array to get the number of the button.

ahh well, i can't be doing things with arduino's everyone has an interest in i guess,
Tho this time a 40 button usb joystick i thought would be an interesting thing, google is full of people asking how to do it, but none seem to give a completed example.

Of course now i have it working, i find out the sodding bus simulator only accepts directX inputs, so 32 buttons per joystick, FFS, 8 buttons i can't use, but need to.

So now i am looking for a way to run 2 instances of the 40 button joystick code at once on the uno, only reduced to 32 buttons,

There are just enough pins to run an 8x8 martix, 16 pins out of the 20, of which 3 can't be used anyway (0 and 1 because they are tied to the serial port, and pin 13 because of the onboard led),