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

Thread: Second keyboard/mouse for a machine I built running Mach 3 on Win 7

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    5

    Second keyboard/mouse for a machine I built running Mach 3 on Win 7

    I have three known locations on a display monitor (Mach3 CNC controller) that I want to be able to press a button at a different location than that of the display, keyboard and mouse to indicate that I want the action at that location to be acted upon. Two of these actions are to set the Mach 3 controllers axis position to zero. The third will be to tell the controller to test for the Z axis zero position which will run a script macro to accomplish that.
    These are pretty straight forward mouse move to absolute positions and right click once there. Although, I am not sure about absolute positioning of the mouse as somewhere I read that the mouse movements were all relative to its current position.
    That requires 3 momentary inputs.
    The final need is to again operate momentary inputs (6) to output the following keyboard characters:
    KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT, KEY_PAGEUP, KEY_PAGEDOWN.
    All of these key presses may be momentary or repeated by holding down the switch, their purpose is to cause the movement of an axis as needed for fine positioning.
    This box will be a second keyboard/mouse with these functions. I purchased the Teensy 3.2 to accomplish this task and researched the different possible libraries. I am new to this and have not done any significant engineering or even programming in maybe 30 years, so am at a loss as to exactly what some things mean. I read that Paul redid the library for the 3.2 from the 2.0. I downloaded the Teensyduino libraries and installed them, but cannot find the USB_Keyboard library to include. I think I am just confused due to my lack of experience.
    If someone could point me in the right direction, I would very much appreciate the assistance.

    I used a numeric keypad recently on the machine (I built the CNC machine from plans) and it had some drawbacks, one was it seemed that the num lock would always be the opposite the main keypad on the keyboard and I always had to push it and when I returned to the main keyboard, I had to push it as well. I am not going to use num lock, instead input those keypresses as indicated above. Should eliminate that problem, I hope. Secondly, the keypad did not have the additional 3 abilities as indicated in the first paragraph of mouse moves and right clicks.
    Lastly, as a 70-year-old geezer, it is a new challenge that will be a joy to complete.
    Thank you,

    woodcrafterClick image for larger version. 

Name:	USB Logic for Direction Control and Axis  Zeroing - Page 1.jpg 
Views:	18 
Size:	90.1 KB 
ID:	12440
    Last edited by woodcrafter; 01-02-2018 at 01:59 AM. Reason: add a flowchart of what I think the logic should be.

  2. #2
    Junior Member
    Join Date
    Dec 2017
    Posts
    5
    If I understand all the technical stuff, the teensy 3.2 actually connects to the PC as a USB Keyboard and Mouse if that mode is selected, and therefore I can just write code to read inputs and determine what outputs may be necessary.

  3. #3
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,572
    That's pretty much it, if you have not already installing Arduino, Teensyduino and having a look at the keyboard examples is probably where you want to be. Limits here are:
    How far away you can be from the PC and still get USB to work
    Since it is just a keyboard there is no feedback - if a popup happens on screen your keypresses go to the wrong program

    If the first is a problem then you can run the keypad on a longer wire, or even use a teensy pair to get a longer distance
    For the second how much this matters depends on if you can see the screen etc from the control location - full solution would be to use serial into Mach-3 but that would be a lot more work.

    edit - mach 3 docs do not seem to mention serial jog control as an option but do support joystick (or more normally gamepad) input so this may actually give you more control, at the cost of more complex code and hardware to support the wider range of options.
    Last edited by GremlinWrangler; 01-02-2018 at 02:38 AM.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    16,205
    In Arduino, click File > Examples > Teensy > USB_Keyboard > Buttons for an example which should get you started.

    You'll probably want to change the code from Keyboard.print() to Keyboard.press() and Keyboard.release(), for the specific keys you wanted. Details here: (scroll down to "Individual Key Press / Release")

    https://www.pjrc.com/teensy/td_keyboard.html

    But first I highly recommend just running the example and touch a wire or paperclip between GND and the nearby pins, if you don't have buttons wired up. The best way to get started on this sort of project is from a working example.

    Even if you don't go with that example, I highly recommend using the Bounce library (as shown in that example) to read the buttons. You can use digitalRead to directly access the pins, but Bounce handles the mechanical chatter of the buttons for you. If you want to get things working easily, Bounce will save you a lot of time & frustration.

  5. #5
    Junior Member
    Join Date
    Dec 2017
    Posts
    5
    Thank you, both for the responses. I will follow your advice. I will keep you informed as to my successes, again thank you.

  6. #6
    Junior Member
    Join Date
    Dec 2017
    Posts
    5
    I followed your advice and used the USB_Keyboard example. That worked fine, I added the LED blink routine to show me that the program actually downloaded and started. as I was not running the MACH 3 in this 0 degree weather in my shop. I changed to Keyboard.press for outputting the keyboard characters that I needed to emulate and then added the USB mouse commands to move and set the mouse per predetermined locations on the screen. It appears that when I keep a button down, that I get repetitive output which was a concern as these the keyboard outputs represent jog requests. Until I can get to the machine and test, I won't know exactly how this will fly.

    Again thank you for pointing me in the right direction

  7. #7
    Junior Member
    Join Date
    Dec 2017
    Posts
    5
    I haven't tested this yet as it is still very cold in my workshop where my CNC machine is located, but I am wondering about timing for mouse moves and mouse clicks. my code is:
    if (button6.fallingEdge()) {
    digitalWrite(21, HIGH);
    Mouse.moveTo(1290, 118);
    delay(150);
    Mouse.set_buttons(1, 0, 0);
    delay(500);
    Mouse.set_buttons(0, 0, 0);

    I assumed that I need some time to allow the mouse to move, but I am not sure that is true, I also assumed to hold the button down some finite time for the click to take effect, not sure that is true either. This is one of three mouse moves and left clicks that would be instigated by a button push away from the location of the mouse and keyboard with known screen locations.
    The entire program follows:

    int count = 0;
    #include <Bounce.h>
    // Create Bounce objects for each button. The Bounce object
    // automatically deals with contact chatter or "bounce", and
    // it makes detecting changes very simple.

    Bounce button0 = Bounce(0, 10);
    Bounce button1 = Bounce(1, 10); // 10 = 10 ms debounce time
    Bounce button2 = Bounce(2, 10); // which is appropriate for
    Bounce button3 = Bounce(3, 10); // most mechanical pushbuttons
    Bounce button4 = Bounce(4, 10);
    Bounce button5 = Bounce(5, 10); // if a button is too "sensitive"
    Bounce button6 = Bounce(6, 10); // to rapid touch, you can
    Bounce button7 = Bounce(7, 10); // increase this time.
    Bounce button8 = Bounce(8, 10);
    void setup() {
    // Configure the pins for input mode with pullup resistors.
    // The pushbuttons connect from each pin to ground. When
    // the button is pressed, the pin reads LOW because the button
    // shorts it to ground. When released, the pin reads HIGH
    // because the pullup resistor connects to +5 volts inside
    // the chip. LOW for "on", and HIGH for "off" may seem
    // backwards, but using the on-chip pullup resistors is very
    // convenient. The scheme is called "active low", and it's
    // very commonly used in electronics... so much that the chip
    // has built-in pullup resistors!

    Mouse.screenSize(1680, 1050); // configure screen size My CNC Machine
    pinMode(0, INPUT_PULLUP);
    pinMode(1, INPUT_PULLUP);
    pinMode(2, INPUT_PULLUP);
    pinMode(3, INPUT_PULLUP);
    pinMode(4, INPUT_PULLUP);
    pinMode(5, INPUT_PULLUP);
    pinMode(6, INPUT_PULLUP); // X zero
    pinMode(7, INPUT_PULLUP); // Y zero
    pinMode(8, INPUT_PULLUP); // ZED zero select
    pinMode(15, OUTPUT); // LED Indicators for switch pressed Clear
    pinMode(16, OUTPUT); // LED Indicators for switch pressed Clear
    pinMode(17, OUTPUT); // LED Indicators for switch pressed Yellow
    pinMode(18, OUTPUT); // LED Indicators for switch pressed Yellow
    pinMode(19, OUTPUT); // LED Indicators for switch pressed Blue
    pinMode(20, OUTPUT); // LED Indicators for switch pressed Blue
    pinMode(21, OUTPUT); // LED Indicators for switch pressed Red
    pinMode(22, OUTPUT); // LED Indicators for switch pressed Red
    pinMode(23, OUTPUT); // LED Indicators for switch pressed Red
    }

    void loop() {
    // Update all the buttons. There should not be any long
    // delays in loop(), so this runs repetitively at a rate
    // faster than the buttons could be pressed and released.

    button0.update();
    button1.update();
    button2.update();
    button3.update();
    button4.update();
    button5.update();
    button6.update();
    button7.update();
    button8.update();

    // Check each button for "falling" edge.
    // Indicated key press on the Keyboard when each button presses
    // or mouse move and click for button 6 through 8
    // falling = high (not pressed - voltage from pullup resistor)
    // to low (pressed - button connects pin to ground)

    if (button0.fallingEdge()) {
    Keyboard.press(KEY_UP);
    digitalWrite(15, HIGH);
    }
    if (button1.fallingEdge()) {
    Keyboard.press(KEY_DOWN);
    digitalWrite(16, HIGH);
    }
    if (button2.fallingEdge()) {
    Keyboard.press(KEY_LEFT);
    digitalWrite(17, HIGH);
    }
    if (button3.fallingEdge()) {
    Keyboard.press(KEY_RIGHT);
    digitalWrite(18, HIGH);
    }
    if (button4.fallingEdge()) {
    Keyboard.press(KEY_PAGE_UP);
    digitalWrite(19, HIGH);
    }
    if (button5.fallingEdge()) {
    Keyboard.press(KEY_PAGE_DOWN);
    digitalWrite(20, HIGH);
    }
    if (button6.fallingEdge()) {
    digitalWrite(21, HIGH);
    Mouse.moveTo(1290, 118);
    delay(150);
    Mouse.set_buttons(1, 0, 0);
    delay(500);
    Mouse.set_buttons(0, 0, 0);
    }
    if (button7.fallingEdge()) {
    digitalWrite(22, HIGH);
    Mouse.moveTo(1290, 159);
    delay(150);
    Mouse.set_buttons(1, 0, 0);
    delay(500);
    Mouse.set_buttons(0, 0, 0);
    }
    if (button8.fallingEdge()) {
    digitalWrite(23, HIGH);
    Mouse.moveTo(375, 57);
    delay(150);
    Mouse.set_buttons(1, 0, 0);
    delay(500);
    Mouse.set_buttons(0, 0, 0);
    }

    // Check each button for "rising" edge
    // Type a message on the Keyboard when each button releases.
    // For many types of projects, you only care when the button
    // is pressed and the release isn't needed.
    // rising = low (pressed - button connects pin to ground)
    // to high (not pressed - voltage from pullup resistor)

    if (button0.risingEdge()) {
    digitalWrite(15, LOW);
    }
    if (button1.risingEdge()) {
    digitalWrite(16, LOW);
    }
    if (button2.risingEdge()) {
    digitalWrite(17, LOW);
    }
    if (button3.risingEdge()) {
    digitalWrite(18, LOW);
    }
    if (button4.risingEdge()) {
    digitalWrite(19, LOW);
    }
    if (button5.risingEdge()) {
    digitalWrite(20, LOW);
    }
    if (button6.risingEdge()) {
    digitalWrite(21, LOW);
    }
    if (button7.risingEdge()) {
    digitalWrite(22, LOW);
    }
    if (button8.risingEdge()) {
    digitalWrite(23, LOW);
    }
    }

Posting Permissions

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