Menu

Pro Micro / Leonardo

Andreas
2017-01-13
2017-01-16
  • Andreas

    Andreas - 2017-01-13

    Hey folks,

    are you able to rewrite: https://sourceforge.net/p/pydarts/source/ci/master/tree/arduino_uno_config/arduino_uno_config_8x10_sample/arduino_uno_config_8x10_sample.ino
    to a real keyboard output?
    I will try to port it in c# unity3d
    But to keep things easy, it would be nice to have the outputs as real keypresses.
    Is keypad.h or keyboard.h helpfully?

    I can use then a pro micro or an Leonardo as USB HID Interface thats identified as Keyboard.

     
  • poilou

    poilou - 2017-01-13

    I don't know how to do that for the moment...
    Good luck for portage ! You'll need some ;)

     
  • Andreas

    Andreas - 2017-01-13

    This should do the trick! :)

    #include keyboard.h
    #define NUM_ROWS 8
    #define NUM_COLS 8
    
    #define SHIFT_ROW 3
    #define SHIFT_COL 1
    
    #define RSHIFT_ROW 4
    #define RSHIFT_COL 6
    
    #define F7_ROW 7
    #define F7_COL 7
    
    #define DEBOUNCE_VALUE 100
    #define REPEAT_DELAY 500
    
    // Keymap for normal use
    
    byte keyMap[NUM_ROWS][NUM_COLS] =
    {
      {'1', '3', '5', '7', '9', '+', '$', KEY_BACKSPACE},
      {KEY_LEFT_ARROW, 'w', 'r', 'y', 'i', 'p', '*', KEY_RETURN},
      {'~', 'a', 'd', 'g', 'j', 'l', ';', KEY_LEFT_ARROW},
      {'~', 0  , 'x', 'v', 'n', ',', '/', KEY_UP_ARROW},
      {' ', 'z', 'c', 'b', 'm', '.', 0  , KEY_F1},
      {'~', 's', 'f', 'h', 'k',':', '=' , KEY_F3},
      {'q', 'e', 't', 'u', 'o', '@', KEY_UP_ARROW, KEY_F5},
      {'2', '4', '6', '8', '0', '-', '~', KEY_F7}
    };
    
    // Keymap if Shift is pressed
    
    byte keyMapShifted[NUM_ROWS][NUM_COLS] =
    {
      {'!', '#', '%', '\'', ')', '+', '$', KEY_BACKSPACE},
      {KEY_LEFT_ARROW, 'W', 'R', 'Y', 'I', 'P', '*', KEY_RETURN},
      {'~', 'A', 'D', 'G', 'J', 'L', ']', KEY_RIGHT_ARROW},
      {'~', 0  , 'X', 'V', 'N', '<', '?', KEY_DOWN_ARROW},
      {' ', 'Z', 'C', 'B', 'M', '>', 0  ,KEY_F2},
      {'~', 'S', 'F', 'H', 'K','[', '=', KEY_F4},
      {'Q', 'E', 'T', 'U', 'O', '@', KEY_UP_ARROW, KEY_F6},
      {'"', '$', '&', '(', '0', '-', '~', KEY_F8}
    };
    // Global Variables
    
    int debounceCount[NUM_ROWS][NUM_COLS];
    int altKeyFlag;
    bool serial_output;
    
    // Define the row and column pins
    
    byte colPins[NUM_COLS] = {0,1,2 ,3 ,4 ,5 ,6 ,7}; // A,B,C,D,E,F,G,H
    byte rowPins[NUM_ROWS] = {8,9,10,11,A0,A1,A2,A3};
    
    // SETUP
    
    void setup()
    {
      // Set all pins as inputs and activate pull-ups
      serial_output = false;
      for (byte c = 0 ; c < NUM_COLS ; c++)
      {
        pinMode(colPins[c], INPUT);
        digitalWrite(colPins[c], HIGH);
    
        // Clear debounce counts
    
        for (byte r = 0 ; r < NUM_ROWS ; r++)
        {
          debounceCount[r][c] = 0;
        }
      }
    
      // Set all pins as inputs
    
      for (byte r = 0 ; r < NUM_ROWS ; r++)
      {
        pinMode(rowPins[r], INPUT);
      }
    
      // Function key is NOT pressed
    
      altKeyFlag = ALT_KEY_OFF;
      pinMode(rowPins[F7_ROW], OUTPUT);
      if (digitalRead(colPins[F7_COL]) == LOW) serial_output = true;
      // Initialise the keyboard
      if (serial_output )
       {
        Serial.begin(9600);
       }
       else
       {
        Keyboard.begin();
       }
    }
    
    // LOOP
    
    void loop()
    {
      bool shifted = false;
      bool r_shifted = false;
      bool keyPressed = false;
    
      // Check for the Shift key being pressed
    
      pinMode(rowPins[SHIFT_ROW], OUTPUT);
      if (digitalRead(colPins[SHIFT_COL]) == LOW) shifted = true;
    
      pinMode(rowPins[RSHIFT_ROW], OUTPUT);
      if (digitalRead(colPins[RSHIFT_COL]) == LOW) shifted = true;
    
        pinMode(rowPins[SHIFT_ROW], INPUT);
        pinMode(rowPins[RSHIFT_ROW], INPUT);
    
        for (byte r = 0 ; r < NUM_ROWS ; r++)
        {
          // Run through the rows, turn them on
    
          pinMode(rowPins[r], OUTPUT);
          digitalWrite(rowPins[r], LOW);
    
          for (byte c = 0 ; c < NUM_COLS ; c++)
          {
            if (digitalRead(colPins[c]) == LOW)
            {
              // Increase the debounce count
    
              debounceCount[r][c]++;
    
              // Has the switch been pressed continually for long enough?
    
              int count = debounceCount[r][c];
              if (count == DEBOUNCE_VALUE)
              {
                // First press
    
                keyPressed = true;
                pressKey(r, c, shifted);
              }
              else if (count > DEBOUNCE_VALUE)
              {
                // Check for repeats
    
                count -= DEBOUNCE_VALUE;
                if (count % REPEAT_DELAY == 0)
                {
                  // Send repeat
    
                  keyPressed = true;
                  pressKey(r, c, shifted);
                }
              }
            }
            else
            {
              // Not pressed; reset debounce count
    
              debounceCount[r][c] = 0;
            }
          }
    
        // Turn the row back off
    
        pinMode(rowPins[r], INPUT);
        }
        digitalWrite(rowPins[RSHIFT_ROW], LOW);
        digitalWrite(rowPins[SHIFT_ROW], LOW);
    
    }
    
    void pressKey(byte r, byte c, bool shifted)
    {
      // Send the keypress
      if (serial_output)
        {
        Serial.print("|");Serial.print("\r\n");Serial.print("|");
        Serial.print(r);Serial.print(",");Serial.print(c);Serial.print(":");
        }
      byte key = shifted ? keyMapShifted[r][c] : keyMap[r][c];
    
      if (serial_output)
       {
       if (key > 0){ Serial.write(key);}
       }
       else
       {
       if (key > 0 ) Keyboard.write(key);
       }
    
    }
    
     
  • poilou

    poilou - 2017-01-14

    Did you successfully tested it ?
    Does it means that your arduino will be able to fake a Usb keyboard ?
    Great !

     
  • Andreas

    Andreas - 2017-01-14

    On pro micro, or leonardo is an ATmega32U4, this chip have USB HID on Board.
    I dont want to write the complete code new. So this is the easiest way to test some stuff :)

     
  • Andreas

    Andreas - 2017-01-14

    Ok here, is the tested sketch:
    So if anybody need a real keyboard input for a dart matrix he can use:

    #include <Keyboard.h>
    #define NUM_ROWS 8
    #define NUM_COLS 9
    #define DEBOUNCE_VALUE 100
    #define REPEAT_DELAY 500
    
    // Keymap for normal use
    
    byte keyMap[NUM_ROWS][NUM_COLS] = {
      {'0','1','2','4','5','6','7','8','9'},
      {'a','b','c','d','e','f','g','h','i'},
      {'j','k','l','m','n','o','p','q','r'},
      {'s','t','u','v','w','x','y','z','A'},
      {'B','C','D','E','F','G','H','I','J'},
      {'K','L','M','N','O','P','Q','R','S'},
      {'T','U','V','W','X','Y','Z','+','-'},
      {'*','/','?','!','&','$','@','#','_'}  
    };
    // Global Variables
    int debounceCount[NUM_ROWS][NUM_COLS];
    int altKeyFlag;
    bool serial_output;
    
    // Define the row and column pins
    
    byte colPins[NUM_COLS] = {A0,A1,A2,A3,15,14,16,10,1}; // A,B,C,D,E,F,G,H
    byte rowPins[NUM_ROWS] = {2,3,4,5,6,7,8,9};
    
    // SETUP
    
    void setup()
    {
      // Set all pins as inputs and activate pull-ups
      serial_output = false;
      for (byte c = 0 ; c < NUM_COLS ; c++)
      {
        pinMode(colPins[c], INPUT);
        digitalWrite(colPins[c], HIGH);
    
        // Clear debounce counts
    
        for (byte r = 0 ; r < NUM_ROWS ; r++)
        {
          debounceCount[r][c] = 0;
        }
      }
    
      // Set all pins as inputs
    
      for (byte r = 0 ; r < NUM_ROWS ; r++)
      {
        pinMode(rowPins[r], INPUT);
      }
    
      // Function key is NOT pressed
    
      // Initialise the keyboard
      if (serial_output )
       {
        Serial.begin(9600);
       }
       else
       {
        Keyboard.begin();
       }
    }
    
    // LOOP
    
    void loop()
    {
    
      bool keyPressed = false;
    
        for (byte r = 0 ; r < NUM_ROWS ; r++)
        {
          // Run through the rows, turn them on
    
          pinMode(rowPins[r], OUTPUT);
          digitalWrite(rowPins[r], LOW);
    
          for (byte c = 0 ; c < NUM_COLS ; c++)
          {
            if (digitalRead(colPins[c]) == LOW)
            {
              // Increase the debounce count
    
              debounceCount[r][c]++;
    
              // Has the switch been pressed continually for long enough?
    
              int count = debounceCount[r][c];
              if (count == DEBOUNCE_VALUE)
              {
                // First press
    
                keyPressed = true;
                pressKey(r, c);
              }
    
              else if (count > DEBOUNCE_VALUE)
              {
                // Check for repeats
    
                count -= DEBOUNCE_VALUE;
                if (count % REPEAT_DELAY == 0)
                {
                  // Send repeat
    
                  keyPressed = true;
                  pressKey(r, c );
                }
              }
            }
            else
            {
              // Not pressed; reset debounce count
    
              debounceCount[r][c] = 0;
            }
          }
    
        // Turn the row back off
    
        pinMode(rowPins[r], INPUT);
        }
    }
    
    void pressKey(byte r, byte c)
    {
      // Send the keypress
      if (serial_output)
        {
        Serial.print("|");Serial.print("\r\n");Serial.print("|");
        Serial.print(r);Serial.print(",");Serial.print(c);Serial.print(":");
        }
      byte key = keyMap[r][c];
    
      if (serial_output)
       {
       if (key > 0){ Serial.write(key);}
       }
       else
       {
       if (key > 0 ) Keyboard.write(key);
       }
    
    }
    

    Remember that you need a pro micro or leonardo, or another board with enough pins and a ATmega32U4 chip.
    I try to do a simple unity example in the next weeks :) I am short in time.

     
  • poilou

    poilou - 2017-01-14

    Andreas,
    Do you think that it would be possible, and interesting to use the input pins of a Raspberry instead of using an Arduino in the dart board ?

    If we can connect the slicks to the rasp, we might be able to get rid of the computer , don't we ?

     
  • Andreas

    Andreas - 2017-01-14

    We have 26 free GPIO on a Raspberry, and they can be extented if needed. I am sure that is possible.
    BUT raspberry is a complete other architecure. So you can not direct use the Arduino code from here for a raspberry.
    The golden way is to use Raspberry 0,1,2 or 3 with a connected Arduino Nano in the DartBoard. (The space is there for it :) ).
    Then u can connect keyboard to the raspberry, too. (for the zero u need a powered USB HUB, and a special GPIO Network card).
    I am currently work on it. And show you my succees :)

     
  • poilou

    poilou - 2017-01-15

    Andreas,
    I add this sketch to the project, for any purpose.

    I was pretty sure that the code from Arduino wouldn't be usable for a Rasp, but, it should be quite easy to write a piece of code for Rasp, don't you think ?

    Whatever, the main issue with Arduino was to make a sketch loop that can detect a very small amount of time for the change of sate of a switch. As far as I can remember, a dart planting on a board is far faster than any keyboard capabilities to detect a closing switch.

    But a soon as this issue is solved, it should be quite easy to write something for a Rasp ?

    I have to buy one, one day.

     
  • Andreas

    Andreas - 2017-01-16

    i am not sure that a raspberry GPIO can handle the presses that fast without a Arduino.
    the Atmega in the Arduino boards are cheap and do a good job.
    We should stay on this little friends. :)

     
  • Anonymous

    Anonymous - 2017-04-16

    Hi guys, my name is Matthew, I apologize for intruding, for two days I began this project in mind and yesterday I installed everything on a center Cyberdine (http://www.cyberdine.hr/),collegato directly to a arduino in 16x4, delay added to the code to not have double hits and lowered the DEBOUNCE_VALUE to 1 ..
    Then all worked perfectly in windows 7.
    MI think I understood from the translation you want to use Arduino as a real USB keyboard with all the function keys.
    With my old project I used arduino Leonardo in this way using the Arduino KEYCODE..tutti say they can not send keycode or at least not all, but if you are armed with patience just try the keycode 50 to 300 and ensured that buttons find them all.
    A single flaw, it seems every operating system reads the codes in their own way, so the code will have win7, win 8 others and 10 others.
    I have a small software found on the net to test areas and test the key code ..
    tested and working with this project ([http://www.yoresupply.com/BallaTron5000])
    I apologize but I had to understand the translated and then re-translate my message maybe some misunderstanding is understandable.
    Greetings from Italy,

    Matthew

     
  • poilou

    poilou - 2017-04-17

    Hi @Matthew, Hi @Andreas !

    To be honest I did not really understood the purpose of faking a keyboard with the Arduino. But if someone could explain me I would be interested !

    BTW, I am surprised and happy to discover this new project called BallaTRON ! Another great OpenSource project for playing darts ! As I can read, it fake a keyboard, maybe it can answer to my question above !

    Unfortunately, this software runs on windows exclusively for now, I cannot try it.

    Cheers to all !

     
  • Andreas

    Andreas - 2017-04-17

    The code above work for direct keyboard input, it is just a test for a unity project.

     

Anonymous
Anonymous

Add attachments
Cancel





Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.