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

Thread: Need help with code and pin set up.

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    7

    Need help with code and pin set up.

    So I am trying to use relays to activate numbers 1-4 on my keyboard through the teensy 2.0. I looked up the code under file/ examples/ teensy/ keyboard/ buttons and tried to delete the release portion of the code but it kept coming up with errors. Can someone please help me figure out what needs to be deleted (and maybe added) from the code in the example. Then I am not understanding what pins the relays contacts need to be connected to in order for the project to work.

    Thanks in advance!

    Micah

  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    So after doing some research i have come up with the following as a code to use

    If (button0.fallingEdge) {
    usb_keyboard_press(KEY_1);
    }

    If (button1.fallingEdge) {
    usb_keyboard_press(KEY_H);
    }

    If (button2.fallingEdge) {
    usb_keyboard_press(KEY_ENTER);
    }

    However it keeps coming up with errors.

    I am trying to make it so that when the 0 pin is shorted to ground the 1 key is pressed and when the 1 pin is shorted to ground the H key is pressed and when the 2 pin is shorted to ground the ENTER key is pressed. again any help is appreciated.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,122
    Quote Originally Posted by Mbnunemaker View Post
    However it keeps coming up with errors.
    To get useful help, you need to post complete info, like the actual error message and the complete code (click "Go Advanced" and there's an option to attach files). It's important to post complete code, plus any other info needed to recreate the problem, so anyone reading your message could duplicate the situation on their computer!

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    This is the error that shows up when trying to verify.


    This report would have more information with
    "Show verbose output during compilation"
    enabled in File > Preferences.
    Arduino: 1.0.5 (Windows 7), Board: "Teensy 2.0"
    sketch_aug20a:1: error: expected constructor, destructor, or type conversion before '(' token
    sketch_aug20a:5: error: expected constructor, destructor, or type conversion before '(' token
    sketch_aug20a:9: error: expected constructor, destructor, or type conversion before '(' token

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,122
    To get useful help, you need to post complete info, like the actual error message and the complete code

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,122
    I really do want to help you with this, but I can't without seeing the exact code you're compiling. It's almost certainly a small syntax error. But I can't see your computer screen. I can't see exactly what you did.

    I can probably identify this type of error in far less time than I've already taken to write these 3 replies.

    Please, if you keep posting, do so with the complete code. Think "reproducible" problem.

  7. #7
    Senior Member
    Join Date
    Jan 2013
    Posts
    843
    C/C++ is case sensitive. So, if you really used 'If' in your code - that won't work...

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,122
    Wow, good catch! I didn't see that until you mentioned it.

    The way I work on these things is I copy the entire program into an Arduino window and click Verify. That's why I always ask people to post a complete program. I can resolve a lot of problems pretty quickly what I can reproduce the error here.

    I'm always amazed that people still ask about compile errors without posting the error message and the complete code that produces the error. We put this in the forum rules and on a sticky thread. I'm looking into changing the forums template file, this guideline will appear right above the box where you type a new thread, and maybe even a reminder right next to the button that submits the message. These threads without necessary info to solve the problem just waste everyone's time.

    Even now, we can only guess if the real problem was indeed those uppercase "I"s.

  9. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    ok so ive done some more research and now this is the code I have come up with


    button1=pin1
    button2=pin2
    button3=pin3


    void loop() {
    **// Update all the button objects.
    **button1.update(1,100);
    **button2.update(2,100);
    **button3.update(3,100);

    **// Check each button for "falling" edge.
    **// falling = high (not pressed - voltage from pullup resistor)
    **// to low (pressed - button connects pin to ground)
    **if (button1.fallingEdge(1)) {
    ****Keyboard.press("KEY_1");
    **}
    **if (button2.fallingEdge(2)) {
    ****Keyboard.press("KEY_H");
    **}
    **if (button3.fallingEdge(3)) {
    ****Keyboard.press("KEY_ENTER");
    **}
    and this is the error that comes up now

    This report would have more information with
    "Show verbose output during compilation"
    enabled in File > Preferences.
    Arduino: 1.0.5 (Windows 7), Board: "Teensy 2.0"
    sketch_aug20a:5: error: expected constructor, destructor, or type conversion before 'void'
    sketch_aug20a:9: error: expected constructor, destructor, or type conversion before 'void'
    sketch_aug20a:1: error: expected constructor, destructor, or type conversion before '=' token
    Again I know very little about code so any help is appreciated. I've already learned more in the last few days than in my basic programming class I took.

  10. #10
    Junior Member
    Join Date
    Jul 2013
    Posts
    19
    Your button assignments are not in a correct syntax:

    Code:
    button1=pin1
    button2=pin2
    button3=pin3
    The above code fails because the compiler has no idea what type button1, 2 and 3 variables are.

    I am going to assume you are trying to use pins 1, 2, and 3 to check for those relays being switched on/off. I am also going to assume that you want to use the Bounce library for debouncing those relays as well. Finally, I'm going to assume that when your relays are triggered, they connect input to ground. Thus, triggering HIGH to LOW. This is important.

    The first thing to do is ensure that you are using the correct core and board in the Arduino IDE:

    1. From the Menu, select Tools -> Board -> Teensy 2.0
    2. From the Menu, select Tools -> USB Type -> Keyboard + Mouse + Joystick


    Now onto the code. First, ensure you are using the Bounce library. You can confirm this by making sure you include it in your code like so:

    Code:
    #include <Bounce.h>
    Next, you must create your objects to handle the button detection. For pins 1, 2, and 3, it would look something like so:

    Code:
    #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.
    
    // First argument is the pin, second argument is the time (in ms) the pin must produce a
    // particular input to show a value change in the button.
    
    Bounce button1 = Bounce(1, 10);
    Bounce button2 = Bounce(2, 10);
    Bounce button3 = Bounce(3, 10);
    Next, comes the setup() routine. This method is only run once at start up. This would be where you setup your pins for input and do any other 1-time tasks. Here we will make sure the pins are set to input.

    Code:
    #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.
    
    // First argument is the pin, second argument is the time (in ms) the pin must produce a
    // particular input to show a value change in the button.
    
    Bounce button1 = Bounce(1, 10);
    Bounce button2 = Bounce(2, 10);
    Bounce button3 = Bounce(3, 10);
    
    void setup() {
    	// Configure our pins
    	// Note, teensy has built-in pullup resistors that can
    	// be activated by using INPUT_PULLUP instead of INPUT
    	pinMode(1, INPUT_PULLUP);
    	pinMode(2, INPUT_PULLUP);
    	pinMode(3, INPUT_PULLUP);
    }
    Above we are using INPUT_PULLUP so that we make sure the input pins will only read LOW when a relay is triggered. In all other cases, including disconnection of the relay from the Teensy will result in the pins reading HIGH.


    Finally, you build your loop() method. This method is run continuously. Here you would update your buttons, then check for falling and rising edges. This is where it might get tricky because it sort of depends on how you wired up your relays.

    • Falling Edge detects a change from HIGH to LOW on the input pin. For a key to be pressed using this method, your relays must change input from HIGH to LOW when triggered.
    • Rising Edge detects a change from LOW to HIGH on the input pin. For a key to be pressed using this method, your relays must change input from LOW to HIGH when triggered.


    According to the assumptions falling edge detects key presses and rising edges detects key releases. Note that simply checking for just the falling edge may not be enough. Sure, by checking this, whenever your relays are triggered, a keyboard press is sent. But, you must also be able to release those keys. Or else, your computer will think the keys are being held down!

    Thus your code might look like the following when you put it all together:


    Code:
    #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.
    
    // First argument is the pin, second argument is the time (in ms) the pin must produce a
    // particular input to show a value change in the button.
    
    Bounce button1 = Bounce(1, 10);
    Bounce button2 = Bounce(2, 10);
    Bounce button3 = Bounce(3, 10);
    
    void setup() {
    	// Configure our pins
    	// Note, teensy has built-in pullup resistors that can
    	// be activated by using INPUT_PULLUP instead of INPUT
    	pinMode(1, INPUT_PULLUP);
    	pinMode(2, INPUT_PULLUP);
    	pinMode(3, INPUT_PULLUP);
    }
    
    void loop() {
    	// Update our buttons
    	button1.update();
    	button2.update();
    	button3.update();
    
    	// Check for falling edges
    	// If a falling edge is detected, set key on the keyboard
    	// accordingly.
    	if (button1.fallingEdge()) {
    		Keyboard.set_key1(KEY_1);
    	}
    	if (button2.fallingEdge()) {
    		Keyboard.set_key2(KEY_H);
    	}
    	if (button3.fallingEdge()) {
    		Keyboard.set_key3(KEY_ENTER);
    	}
    	
    	// Check for rising edges
    	// If a rising edge is detected, set key on the keyboard
    	// to zero (key is no longer pressed).
    	if (button1.risingEdge()) {
    		Keyboard.set_key1(0);
    	}
    	if (button2.risingEdge()) {
    		Keyboard.set_key2(0);
    	}
    	if (button3.risingEdge()) {
    		Keyboard.set_key3(0);
    	}
    	
    	// Finally, send out the Keyboard's report to the computer
    	Keyboard.send_now();
    }
    Most of the above information was taken from the Buttons example for the teensy platform, with some minor differences. I chose to use set_key# instead of press because internally, using press causes the button press to be sent immediately, while set_key# requires you to manually "send" the key presses yourself. Basically instead of sending three individual presses, we send 1 group of 3.

    Also this is setup to constantly send out key presses as long as the relays are triggered. If all you wanted was a 1-time key press and release for each triggering of the relays, the following code could be used:

    Code:
    #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.
    
    // First argument is the pin, second argument is the time (in ms) the pin must produce a
    // particular input to show a value change in the button.
    
    Bounce button1 = Bounce(1, 10);
    Bounce button2 = Bounce(2, 10);
    Bounce button3 = Bounce(3, 10);
    
    void setup() {
    	// Configure our pins
    	// Note, teensy has built-in pullup resistors that can
    	// be activated by using INPUT_PULLUP instead of INPUT
    	pinMode(1, INPUT_PULLUP);
    	pinMode(2, INPUT_PULLUP);
    	pinMode(3, INPUT_PULLUP);
    }
    
    void loop() {
    	// Update our buttons
    	button1.update();
    	button2.update();
    	button3.update();
    
    	// Check for falling edges
    	// If a falling edge is detected, set key on the keyboard
    	// accordingly.
    	if (button1.fallingEdge()) {
    		Keyboard.set_key1(KEY_1);
    	}
    	if (button2.fallingEdge()) {
    		Keyboard.set_key2(KEY_H);
    	}
    	if (button3.fallingEdge()) {
    		Keyboard.set_key3(KEY_ENTER);
    	}
    	// Send out the Keyboard's report to the computer
    	// for the presses.
    	Keyboard.send_now();
    	
    	// Immediately send a blank report indicating that the keyboard
    	// has no buttons pressed
    	Keyboard.releaseAll();
    }
    Hope this helps. But, as Paul said, it really helps if you explain your what you are trying to do and the code you have as concisely and thoroughly as possible. I had to make assumptions because you didn't provide enough information on how you were connecting things to the Teensy, and what you wanted the result to be.

    I make no guarantees this code works or even compiles.
    Last edited by joshuass; 08-21-2013 at 03:34 PM. Reason: Grammar, Disclaimer

  11. #11
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    Joshuass you are the MAN!!!!! That works exactly how it is supposed to!!! I cant thank you enough!!

Posting Permissions

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