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

Thread: Teensy 2.0 INPUT_PULLUP and controller buttons

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

    Teensy 2.0 INPUT_PULLUP and controller buttons

    I got a question related to pull up/down on input pins.

    My project consists of 1 or 2 SNES pads connected to a teensy 2.0. They are communicating with the teensy via the SNES's simplistic serial protocol. From searching online I know that reading the input pin signals LOW for button presses (active low, I think is the term).

    I was having issues yesterday when I unplugged the controller. The teensy would just start sending keyboard reports as if all buttons are pressed. It didn't dawn on me until later that the reason maybe that my input pin was registering LOW for an unconnected controller.

    Am I on the right track here? And is the fix as simple as making sure my pinMode() for the input pin is INPUT_PULLUP?. That is, ensure that the pin always reads HIGH in all cases except when the button is pressed?

    Thanks

  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    19
    Here is the code I am working with. Originally it the data pins were set to just INPUT. I changed them in preparation for tonight's testing.

    Code:
    void setup() {
    	// Set pin assignments
    	// Pull up data pins
    	pinMode(PIN_CLOCK, OUTPUT);
    	pinMode(PIN_LATCH, OUTPUT);
    	pinMode(PIN_DATA1, INPUT_PULLUP);
    	pinMode(PIN_DATA2, INPUT_PULLUP);
    	
    	// Initialize clock and latch
    	digitalWrite(PIN_CLOCK, HIGH);
    	digitalWrite(PIN_LATCH, HIGH);
    }
    I read the pins and reverse the read values so that 0 becomes 1 and vice versa.

    Code:
    void readControllers() {
    	int i;
    
    	// Reset controller read state
    	digitalWrite(PIN_CLOCK, LOW);
    	digitalWrite(PIN_LATCH, LOW);
    	
    	// Latch
    	digitalWrite(PIN_LATCH, HIGH);
    	delayMicroseconds(12);
    	digitalWrite(PIN_LATCH, LOW);
    	delayMicroseconds(6);
    	
    	// Cycle 0 - Available immediately
    	rData1[0] = !digitalRead(PIN_DATA1);
    	rData2[0] = !digitalRead(PIN_DATA2);
    	
    	// Cycles 1 - 15 - Pulse clock then read
    	for (i = 1; i < 16; i++) {
    		digitalWrite(PIN_CLOCK, HIGH);
    		delayMicroseconds(6);
    		digitalWrite(PIN_CLOCK, LOW);
    		delayMicroseconds(6);
    		rData1[i] = !digitalRead(PIN_DATA1);
    		rData2[i] = !digitalRead(PIN_DATA2);
    	}
    	
    	// Handle debounce
    	for (i = 0; i < 16; i++) {
    		// Player 1
    		if (rData1[i] == cData1[i] && cCount1[i] > 0) {
    			cCount1[i]--;
    		}
    		if (rData1[i] != cData1[i]) {
    			cCount1[i]++;
    		}
    		if (cCount1[i] >= dbTime) {
    			cCount1[i] = 0;
    			cData1[i] = rData1[i];
    		}
    		// Player 2
    		if (rData2[i] == cData2[i] && cCount2[i] > 0) {
    			cCount2[i]--;
    		}
    		if (rData2[i] != cData2[i]) {
    			cCount2[i]++;
    		}
    		if (cCount2[i] >= dbTime) {
    			cCount2[i] = 0;
    			cData2[i] = rData2[i];
    		}
    	}
    }
    My problem is that if the data pins are disconnected from the controller, they are essentially "floating" and reading them produces undefined results. My question was if I change the pin mode to INPUT_PULLUP, would they now read HIGH when the controllers were disconnected.
    Last edited by joshuass; 08-14-2013 at 09:04 PM.

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,663
    Quote Originally Posted by joshuass View Post
    My question was if I change the pin mode to INPUT_PULLUP, would they now read HIGH when the controllers were disconnected.
    Yes, that's exactly how it works.

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    19
    Awesome Paul. It definitely fixed my issue!

Posting Permissions

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