Button problems

Status
Not open for further replies.

Duhjoker

Well-known member
[Solved] Button problems

Quick catch up.... I'm building a multi-MCU arduino driven game console with an easy to use library that allows any one to build and play games on a DIY hand held console. But it hasn't been easy getting it going. The preferred MCU is teensy3.x but almost any hoard can be used as well as esp8266 and esp32 being completely experimental.

Any way now that I have a stable TFT, MCU and power I now need to add the button functions. The button library I have, works. It's been thoroughly tested and used, that's no problem. The problem I think lies in the button pad but I'm not sure.

I made the pad from regular breadboard and installed the 6x6x5 tactile switch buttons. I then went across the pad soldering a pin, pin to pin completing a common ground. Next I soldered a wire to each buttons empty pin.

So when I upload my sketch every thing loads just fine and my Sprite appears. When I hit any dpad button I get nothing. If I hold start the player moves left. If I hold start and hit either up or down on the dpad the Sprite moves up and down the screen.

I double and triple checked the board it seems I have it hooked up right. The weird thing is that I don't actually have start or select with a given function yet. I'm just testing up down left and right.

Edit::::

I just removed the button pad and re-checked it a few more times over. The wires go to empty pins on the buttons.

I dont think you can see any thing especially since i wired it to be super secure for easy removal and manipulation.




Edit::

Hooked up to my 3.2 for a test over the 3.6 and same glitch. So i moved it over to the esp32 and still the glitch. If i had soldered a button lead to a ground the player sprite would move automatically in the direction of the grounded lead.

Also checked using a old pad i made a few months ago that was working and still no go.

What this is telling me is that its not getting enough signal to the button from the pin.
 
Last edited:
Guess here would be that you have buttons in series that you didn't intend.

Suggested route if the board can be unplugged would be to get a meter and make sure your wiring really does go short circuit when you expect it to. If you have it soldered in then with the power on clip one meter lead to gnd, and then probe around with the other. Open buttons should have gnd on one side and 3.3V from the pullups on the other, and closed both sides should be 0v. If both sides go 3.3V when pressed you have a busted gnd of some sort. Is it possible one or more button is in rotate 90 degrees, giving some always closed connections and possible a break in the gnd path where you are going through switchs.
 
As @GremlinWrangler mentioned. Check your wiring... Should be easy with multi-meter.

Also you did not show your code so hard to comment there.

But from your comments, your wiring is all of the buttons are connected with common ground and then each of the buttons are connected to their own IO pin? Since you did not mention any Pullup resistors, is your code using INPUT_PULLUP ?

Have you tried doing any simple debugging?

Things like:
Code:
#define MY_BUTTON 0
uint8_t save_button_state = 1;   
void setup() {
    while (!Serial) ;   // wait until serial port is connected
    Serial.begin(115200);

    pinMode(MY_BUTTON, INPUT_PULLUP);
}

void loop() {
    uint8_t button_state = digitalRead(MY_BUTTON);
    if (button_state != save_button_state) {
        save_button_stat = button_state;
        if (button_state) Serial.println("Button Released");
        else Serial.println("Button Pressed");
        delay(5); 
    }
}

Does it work? Note: Above assumes that the buttons you used are Normally Open (NO) and not Normally Closed(NC). If NC then the messages would be reversed. Also I would have a tendency in NC case to use common 3.3v instead of GND...
 
ok so I haven't soldered the boards this time around. So I was able to test all my boards including my esp32. which all do the same thing. I also checked a couple button pads I made months ago for the 2.2inch tft console and it still gives me what I explained earlier. I checked the main grafx files and noticed that the update functions did not include the buttons.update or buttons.begin so I changed it thinking it might help but no.

ok here are button library files....

buttons.h
Code:
#ifndef BUTTONS_H
#define	BUTTONS_H

#include <Arduino.h>
#include "settings.c"

class Buttons {
public:
    void begin();
    void update();
    boolean pressed(uint8_t button);
    boolean released(uint8_t button);
    boolean held(uint8_t button, uint8_t time);
    boolean repeat(uint8_t button, uint8_t period);
    uint8_t timeHeld(uint8_t button);
    uint8_t pins[NUM_BTN];
    uint8_t states[NUM_BTN];

};

#endif	/* BUTTONS_H */

.cpp I uncoomented the button states thinking it might help
Code:
#include "Buttons.h"

void Buttons::begin() {
    pins[BTN_LEFT] = BTN_LEFT_PIN;
    pins[BTN_UP] = BTN_UP_PIN;
    pins[BTN_RIGHT] = BTN_RIGHT_PIN;
    pins[BTN_DOWN] = BTN_DOWN_PIN;
    pins[BTN_A] = BTN_A_PIN;
    pins[BTN_B] = BTN_B_PIN;
	pins[BTN_X] = BTN_X_PIN;
	pins[BTN_Y] = BTN_Y_PIN;
	pins[BTN_S] = BTN_S_PIN;
	pins[BTN_T] = BTN_T_PIN;

    states[BTN_LEFT] = 0;
    states[BTN_UP] = 0;
    states[BTN_RIGHT] = 0;
    states[BTN_DOWN] = 0;
    states[BTN_A] = 0;
    states[BTN_B] = 0;
    states[BTN_X] = 0;
    states[BTN_Y] = 0;
    states[BTN_S] = 0;
    states[BTN_T] = 0;
}

/*
 * reads each button states and store it
 */
void Buttons::update() {
    for (uint8_t thisButton = 0; thisButton < NUM_BTN; thisButton++) {
        pinMode(pins[thisButton], INPUT_PULLUP); //enable internal pull up resistors
        if (digitalRead(pins[thisButton]) == LOW) { //if button pressed
            states[thisButton]++; //increase button hold time
        } else {
            if (states[thisButton] == 0)//button idle
                continue;
            if (states[thisButton] == 0xFF)//if previously released
                states[thisButton] = 0; //set to idle
            else
                states[thisButton] = 0xFF; //button just released
        }
        pinMode(pins[thisButton], INPUT); //disable internal pull up resistors to save power
    }
	

}

/*
 * Returns true when 'button' is pressed.
 * The button has to be released for it to be triggered again.
 */
boolean Buttons::pressed(uint8_t button) {
    if (states[button] == 1)
        return true;
    else
        return false;
}

/*
 * return true if 'button' is released
 */
boolean Buttons::released(uint8_t button) {
    if (states[button] == 0xFF)
        return true;
    else
        return false;
}

/**
 * returns true ONCE when 'button' is held for 'time' frames
 * @param button The button's ID
 * @param time How much frames button must be held, between 1 and 254.
 * @return true when 'button' is held for 'time' frames
 */
boolean Buttons::held(uint8_t button, uint8_t time){
    if(states[button] == (time+1))
        return true;
    else
        return false;
}

/**
 * returns true every 'period' frames when 'button' is held
 * @param button The button's ID
 * @param period How much frames button must be held, between 1 and 254.
 * @return true if the button is held for the given time
 */
boolean Buttons::repeat(uint8_t button, uint8_t period) {
    if (period <= 1) {
        if ((states[button] != 0xFF) && (states[button]))
            return true;
    } else {
        if ((states[button] != 0xFF) && ((states[button] % period) == 1))
            return true;
    }
    return false;
}

/**
 * 
 * @param button The button's ID
 * @return The number of frames during which the button has been held.
 */
uint8_t Buttons::timeHeld(uint8_t button){
    if(states[button] != 0xFF)
        return states[button];
    else
        return 0;
    
}

here is my sketch. its the esp32 since it was already open because it was the last thing I tested, it should work just remove the esp32 bits and add the teensy bits. Then name your pins. This is normally done by the settings.c file.

Code:
////////////////////////////////
#include <Grafx_esp.h>
#include "SPI.h"

#define TFT_SCK 18
#define TFT_MISO 19
#define TFT_MOSI 23
#define TFT_DC 5
#define TFT_CS 22
#define TFT_RST 17
Grafx_esp tft = Grafx_esp(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCK, TFT_MISO);
//Adafruit_GFX_Button button;
uint8_t use_clip_rect = 0;
uint8_t use_set_origin = 0;
uint8_t use_fb = 0;


int player_x = 160;
 int player_y = 110;
 int player_direction = 2;


int x=-50,y=50;


//////////Paul_Atreades
//////////paul front

const byte paul_frontblack[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x02,0x40,0x02,0x30,0x0c,0x38,0x1c,0x4c,0x32,0x4f,0xf2,0x39,0x9c,0x16,0x68,0x11,0x88,0x08,0x10,0x0e,0x70};

const byte paul_frontblue[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x40,0x02,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x01,0x80,0x06,0x60,0x06,0x60,0x00,0x00};

const byte paul_frontbrown[] PROGMEM = {16,16,
0x0e,0x70,0x1b,0xd8,0x16,0x68,0x19,0x98,0x20,0x04,0x16,0x68,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_frontgreen[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_frontgrey[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x08,0x10,0x00,0x00,0x00,0x00};

const byte paul_frontpink[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x60,0x0f,0xf0,0x29,0x94,0x3d,0xbc,0x0d,0xb0,0x07,0xe0,0x33,0xcc,0x30,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_frontred[] PROGMEM = {16,16,
0x01,0x80,0x04,0x20,0x09,0x90,0x20,0x04,0x10,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0,0x00,0x00,0x00,0x00};

////////paul_front_walk_1

const byte paul_frontw1black[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x02,0x40,0x02,0x30,0x0e,0x38,0x1a,0x24,0x3c,0x27,0x98,0x19,0x68,0x0e,0x88,0x0e,0x10,0x00,0x10,0x00,0x70};

const byte paul_frontw1blue[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x40,0x02,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x80,0x00,0x60,0x00,0x60,0x00,0x60,0x00,0x00};

const byte paul_frontw1brown[] PROGMEM = {16,16,
0x0e,0x70,0x1b,0xd8,0x16,0x68,0x19,0x98,0x20,0x04,0x16,0x68,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_frontw1green[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_frontw1grey[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_frontw1pink[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x60,0x0f,0xf0,0x29,0x94,0x3d,0xbc,0x0d,0xb0,0x07,0xe4,0x1b,0xc0,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_frontw1red[] PROGMEM = {16,16,
0x01,0x80,0x04,0x20,0x09,0x90,0x20,0x04,0x10,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

////////paul_front_walk_2

const byte paul_frontw2black[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x02,0x40,0x02,0x70,0x0c,0x58,0x1c,0x3c,0x24,0x19,0xe4,0x16,0x98,0x11,0x70,0x08,0x70,0x08,0x00,0x0e,0x00};

const byte paul_frontw2blue[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x40,0x02,0x40,0x00,0x00,0x00,0x00,0x06,0x00,0x01,0x60,0x06,0x00,0x06,0x00,0x06,0x00,0x00,0x00};

const byte paul_frontw2brown[] PROGMEM = {16,16,
0x0e,0x70,0x1b,0xd8,0x16,0x68,0x19,0x98,0x20,0x04,0x16,0x68,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_frontw2green[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_frontw2grey[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_frontw2pink[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x60,0x0f,0xf0,0x29,0x94,0x3d,0xbc,0x0d,0xb0,0x27,0xe0,0x03,0xd8,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_frontw2red[] PROGMEM = {16,16,
0x01,0x80,0x04,0x20,0x09,0x90,0x20,0x04,0x10,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

////////paul left

 const byte paul_leftblack[] PROGMEM = {16,16,
0x00,0x00,0x00,0x0,0x00,0x00,0x00,0x00,0x10,0x00,0x14,0x00,0x10,0x00,0x10,0x00,0x08,0x60,0x07,0xe0,0x00,0x20,0x03,0x20,0x04,0xe0,0x04,0x20,0x01,0xc0,0x03,0xc0};

const byte paul_leftblue[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x00,0x00};

const byte paul_leftbrown[] PROGMEM = {16,16,
0x0e,0xd8,0x0d,0xa8,0x1b,0x74,0x3e,0xec,0x01,0xd8,0x00,0xb4,0x00,0x88,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_leftgreen[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_leftgrey[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0xc0,0x00,0x00,0x00,0x00};

const byte paul_leftpink[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0x00,0x0b,0x00,0x0b,0x60,0x0f,0xe0,0x07,0x80,0x00,0x00,0x00,0xc0,0x00,0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_leftred[] PROGMEM = {16,16,
0x01,0x20,0x12,0x54,0x24,0x88,0x01,0x10,0x00,0x24,0x00,0x48,0x00,0x10,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

///////paul_left_walk

const byte paul_leftwblack[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x14,0x00,0x10,0x00,0x10,0x00,0x08,0x60,0x04,0xe0,0x1f,0x98,0x24,0x94,0x13,0xe4,0x12,0x44,0x02,0x24,0x0e,0x18};

const byte paul_leftwblue[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x08,0x08,0x18,0x0c,0x38,0x0c,0x18,0x00,0x00};

const byte paul_leftwbrown[] PROGMEM = {16,16,
0x0e,0xe8,0x0d,0xd8,0x1b,0xb4,0x3f,0x6c,0x01,0xd8,0x00,0xb4,0x00,0x88,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_leftwgreen[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_leftwgrey[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_leftwpink[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0e,0x00,0x0b,0x00,0x0b,0x60,0x0f,0xe0,0x07,0x80,0x00,0x00,0x00,0x60,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_leftwred[] PROGMEM = {16,16,
0x01,0x10,0x12,0x24,0x24,0x48,0x00,0x90,0x00,0x24,0x00,0x48,0x00,0x10,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

////////paul_rear

 const byte paul_rearblack[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x02,0x40,0x02,0x30,0x0c,0x38,0x1c,0x49,0x92,0x5e,0x7a,0x39,0x9c,0x16,0x68,0x11,0x80,0x08,0x10,0x0e,0x70};

const byte paul_rearblue[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x60,0x01,0x80,0x06,0x60,0x01,0x80,0x06,0x60,0x06,0x60,0x00,0x00};

const byte paul_rearbrown[] PROGMEM = {16,16,
0x0d,0xb0,0x0b,0xd0,0x1d,0xb8,0x1a,0x58,0x37,0xec,0x0d,0xb0,0x0b,0xd0,0x02,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_reargrey[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x10,0x08,0x10,0x00,0x00,0x00,0x00};

const byte paul_rearpink[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x04,0x30,0x0c,0x0c,0x30,0x07,0xe0,0x30,0x0c,0x20,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rearred[] PROGMEM = {16,16,
0x02,0x40,0x14,0x28,0x02,0x40,0x25,0xa4,0x08,0x10,0x12,0x48,0x04,0x20,0x01,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

///////paul_rear_walk1

 const byte paul_rearw1black[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x02,0x40,0x02,0x30,0x0e,0x3c,0x1a,0x49,0x9c,0x4e,0x78,0x33,0xe8,0x0e,0x88,0x0e,0x10,0x00,0x10,0x00,0x70};

const byte paul_rearw1blue[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x60,0x01,0x80,0x0c,0x00,0x00,0x60,0x00,0x60,0x00,0x60,0x00,0x00};

const byte paul_rearw1brown[] PROGMEM = {16,16,
0x0d,0xb0,0x0b,0xd0,0x1d,0xb8,0x1a,0x58,0x37,0xec,0x0d,0xb0,0x0b,0xd0,0x02,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rearw1grey[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rearw1pink[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x04,0x30,0x0c,0x0c,0x30,0x03,0xe4,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rearw1red[] PROGMEM = {16,16,
0x02,0x40,0x14,0x28,0x02,0x40,0x25,0xa4,0x08,0x10,0x12,0x48,0x04,0x20,0x01,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

///////paul_rear_walk2

const byte paul_rearw2black[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x02,0x40,0x02,0x70,0x0c,0x58,0x1c,0x39,0x92,0x1e,0x72,0x17,0x4c,0x11,0x70,0x08,0x70,0x08,0x00,0x0e,0x00};

const byte paul_rearw2blue[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x60,0x01,0x80,0x00,0x30,0x06,0x00,0x06,0x00,0x06,0x00,0x00,0x00};

const byte paul_rearw2brown[] PROGMEM = {16,16,
0x0d,0xb0,0x0b,0xd0,0x1d,0xb8,0x1a,0x58,0x37,0xec,0x0d,0xb0,0x0b,0xd0,0x02,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rearw2grey[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rearw2pink[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x04,0x30,0x0c,0x0c,0x30,0x27,0xc,0x00,0x0c,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rearw2red[] PROGMEM = {16,16,
0x02,0x40,0x14,0x28,0x02,0x40,0x25,0xa4,0x08,0x10,0x12,0x48,0x04,0x20,0x01,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

////////paul_right

 const byte paul_rightblack[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x28,0x00,0x08,0x00,0x08,0x06,0x10,0x07,0xe0,0x04,0x00,0x04,0xc0,0x07,0x20,0x04,0x20,0x03,0x80,0x03,0xc0};

const byte paul_rightblue[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0x00,0x00,0x00,0x40,0x00,0x40,0x00,0x40,0x00,0x00};

const byte paul_rightbrown[] PROGMEM = {16,16,
0x1b,0x70,0x15,0xb0,0x2e,0xd8,0x37,0x7c,0x1b,0x80,0x2d,0x00,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rightgrey[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x03,0x80,0x00,0x00,0x00,0x00};

const byte paul_rightpink[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0xd0,0x06,0xd0,0x07,0xf0,0x01,0xe0,0x00,0x00,0x03,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rightred[] PROGMEM = {16,16,
0x04,0x80,0x2a,0x48,0x11,0x24,0x08,0x80,0x24,0x00,0x12,0x00,0x08,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

////////paul_right_walk

 const byte paul_rightwblack[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x28,0x00,0x08,0x00,0x08,0x06,0x10,0x07,0x20,0x19,0xf8,0x29,0x24,0x27,0xc8,0x22,0x48,0x24,0x40,0x18,0x70};

const byte paul_rightwblue[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0xc0,0x00,0x00,0x10,0x18,0x18,0x10,0x1c,0x30,0x18,0x30,0x00,0x00};

const byte paul_rightwbrown[] PROGMEM = {16,16,
0x17,0x70,0x1b,0xb0,0x2d,0xd8,0x36,0xfc,0x1b,0x80,0x2d,0x00,0x11,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rightwgrey[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xc0,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rightwpink[] PROGMEM = {16,16,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0xd0,0x06,0xd0,0x07,0xf0,0x01,0xe0,0x00,0x00,0x06,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

const byte paul_rightwred[] PROGMEM = {16,16,
0x08,0x80,0x24,0x48,0x12,0x24,0x09,0x00,0x24,0x00,0x12,0x00,0x08,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

void setup() {
    Serial.begin(921600);
    long unsigned debug_start = millis();
    while (!Serial && (millis() < 4000)) ;
  tft.begin();
//   tft.titleScreen(F("Dune esp demo"));
  tft.setFrameRate(62);
  tft.persistence = false;
  tft.setRotation(1);
  tft.fillScreen(BLACK);
  }

//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

void loop(void) {
//updates the GameRIot (the display, the sound, the buttons, everyyhing)
  //returns true when it's time to render a new frame (20 times/second)
   if(tft.updateAll()){
    if (tft.buttons.repeat(BTN_RIGHT,1));//{X--:}
    if (tft.buttons.repeat(BTN_LEFT,1));//{X++:}
    if (tft.buttons.repeat(BTN_DOWN,1));//{Y--:}
    if (tft.buttons.repeat(BTN_UP,1));//{Y--:}

    if (tft.buttons.repeat(BTN_UP,1)){
       tft.drawBitmap1(player_x, player_y,paul_rearblack,16,16,BLACK);
         tft.drawBitmap1(player_x, player_y,paul_rearblue,16,16,BLUE);
          tft.drawBitmap1(player_x, player_y,paul_rearbrown,16,16,BROWN);
          tft.drawBitmap1(player_x, player_y,paul_reargrey,16,16,GREY);
           tft.drawBitmap1(player_x, player_y,paul_rearpink,16,16,PINK);
            tft.drawBitmap1(player_x, player_y,paul_rearred,16,16,YELLOW);

            player_direction = 1;
             player_y = player_y - 1;}
            if(player_y <= 0){
              player_y = 0;}

       if (tft.buttons.repeat(BTN_DOWN,1)){
        tft.drawBitmap1(player_x, player_y,paul_frontblack,16,16,BLACK);
         tft.drawBitmap1(player_x, player_y,paul_frontblue,16,16,BLUE);
          tft.drawBitmap1(player_x, player_y,paul_frontbrown,16,16,BROWN);
          tft.drawBitmap1(player_x, player_y,paul_frontgrey,16,16,GREY);
          tft.drawBitmap1(player_x, player_y,paul_frontgreen,16,16,GREEN);
           tft.drawBitmap1(player_x, player_y,paul_frontpink,16,16,PINK);
            tft.drawBitmap1(player_x, player_y,paul_frontred,16,16,RED);

            player_direction = 2;
             player_y = player_y + 1;}
            if(player_y <= 40){
              player_y = 40;}

       if (tft.buttons.repeat(BTN_LEFT,1)){
        tft.drawBitmap1(player_x, player_y,paul_leftblack,16,16,BLACK);
         tft.drawBitmap1(player_x, player_y,paul_leftblue,16,16,BLUE);
          tft.drawBitmap1(player_x, player_y,paul_leftbrown,16,16,BROWN);
          tft.drawBitmap1(player_x, player_y,paul_leftgreen,16,16,GREEN);
          tft.drawBitmap1(player_x, player_y,paul_leftgrey,16,16,GREY);
           tft.drawBitmap1(player_x, player_y,paul_leftpink,16,16,PINK);
            tft.drawBitmap1(player_x, player_y,paul_leftred,16,16,RED);

            player_direction = 3;
             player_x = player_x - 1;}
            if(player_x <= -2){
              player_x = -2;}

        if (tft.buttons.repeat(BTN_RIGHT,1)){
        tft.drawBitmap1(player_x, player_y,paul_rightblack,16,16,BLACK);
         tft.drawBitmap1(player_x, player_y,paul_rightblue,16,16,BLUE);
          tft.drawBitmap1(player_x, player_y,paul_rightbrown,16,16,BROWN);
          tft.drawBitmap1(player_x, player_y,paul_rightbrown,16,16,GREY);
           tft.drawBitmap1(player_x, player_y,paul_rightpink,16,16,PINK);
            tft.drawBitmap1(player_x, player_y,paul_rightred,16,16,RED);

            player_direction = 3;
            player_x = player_x - 1;}
           if(player_x <= -2){
              player_x = -2;}

       /////////////////PLAYER DIRECTION/////////////////

        if (player_direction == 1){
          tft.drawBitmap1(player_x, player_y,paul_rearblack,16,16,BLACK);
         tft.drawBitmap1(player_x, player_y,paul_rearblue,16,16,BLUE);
          tft.drawBitmap1(player_x, player_y,paul_rearbrown,16,16,BROWN);
          tft.drawBitmap1(player_x, player_y,paul_reargrey,16,16,GREY);
           tft.drawBitmap1(player_x, player_y,paul_rearpink,16,16,PINK);
            tft.drawBitmap1(player_x, player_y,paul_rearred,16,16,YELLOW);

       }

      else if (player_direction == 2){
         tft.drawBitmap1(player_x, player_y,paul_frontblack,16,16,BLACK);
         tft.drawBitmap1(player_x, player_y,paul_frontblue,16,16,BLUE);
          tft.drawBitmap1(player_x, player_y,paul_frontbrown,16,16,BROWN);
          tft.drawBitmap1(player_x, player_y,paul_frontgrey,16,16,GREY);
          tft.drawBitmap1(player_x, player_y,paul_frontgreen,16,16,GREEN);
           tft.drawBitmap1(player_x, player_y,paul_frontpink,16,16,PINK);
            tft.drawBitmap1(player_x, player_y,paul_frontred,16,16,RED);

              }


       else if (player_direction == 3){
         tft.drawBitmap1(player_x, player_y,paul_leftblack,16,16,BLACK);
         tft.drawBitmap1(player_x, player_y,paul_leftblue,16,16,BLUE);
          tft.drawBitmap1(player_x, player_y,paul_leftbrown,16,16,BROWN);
          tft.drawBitmap1(player_x, player_y,paul_leftgreen,16,16,GREEN);
          tft.drawBitmap1(player_x, player_y,paul_leftgrey,16,16,GREY);
           tft.drawBitmap1(player_x, player_y,paul_leftpink,16,16,PINK);
            tft.drawBitmap1(player_x, player_y,paul_leftred,16,16,RED);

            }


        else if (player_direction == 4){
          tft.drawBitmap1(player_x, player_y,paul_rightblack,16,16,BLACK);
         tft.drawBitmap1(player_x, player_y,paul_rightblue,16,16,BLUE);
          tft.drawBitmap1(player_x, player_y,paul_rightbrown,16,16,BROWN);
          tft.drawBitmap1(player_x, player_y,paul_rightbrown,16,16,GREY);
           tft.drawBitmap1(player_x, player_y,paul_rightpink,16,16,PINK);
            tft.drawBitmap1(player_x, player_y,paul_rightred,16,16,RED);

               }

            
       }
}
 
Last edited:
Code should cut and paste to TEENSY to be looked at.

The problem is likely in the button wiring - they either need to provide power when closed - or as KurtE suggested have an INPUT_PULLUP short to ground when closed.

I'm not sure if this might help you confirm your buttons are functionally wired? :: experiment-5-push-buttons
 
Yea sorry!!!

Ok I don't have many buttons left and I have no resistors and since radio shacked closed it might take a week or longer to order what I need.

So as experiment I took one the few buttons I had left and soldered 2 leads to the button then tested each button pin one by one. I was able to get the player Sprite to move on each one but one button made him go SW diagonal. Which is cool but it was supposed to right.

Also...... When the player Sprite moves, he leaves a trail like a light cycle. But doesn't stop him from going through it.

Edit::: did you see the way I'm building the player sprites? Now think about this. If I use pgmspace I have to do that at 40 more times plus say another 25 for map tiles. And another 35-40 times for monsters. RPG game if your wondering.

I tried to get the screen to show a terrain tile I made that was 230x220 and it says invalid conversion from my INT to some thing Else I can't remember right now. Either way I can't seem to give a background other than filling the screen with a color.

Edit::::

Just tried the other side of the button with the same results.

Ok before I start trying to resolder the button pad, when I was soldering every thing, a couple buttons had to be given an extra common ground. Can a 4pin tactile switch have two pins ground with one signal line. Or do I need to to make it where there is only one button pin being ground?

Edit:::

Cleaned the button pad of solder as best as I could then soldered a lead to an empty pin on the select button and the start button then joined two pins on each and added the ground. What happens now is it moves with out the button or buttons being pushed. I don't understand why I'm having so much trouble. I checked triple times before and after soldering. And I soldered normally from the back of the board instead of trying to be fancy.

I guess I'm gonna go ahead and order some wire and buttons and try again 1 by 1. Meanwhile I have some graphics issues to work on plus I lost all my old sprites and Sprite sheets so I had to make new NPC sprites that now need to be separated and split up.
 
Last edited:
If I'm understanding your wiring would suggest just getting a jumper wiring, and going from gnd to each of your button inputs and see what happens, preferably with debug code as described above without your debounce coding. Also would suggest commenting out your minmode input line, since it's not impossible that the time between setting pullup and reading the pin isn't long enough to actually have the pullup get a high level reliably.


Edit: once you get the reads working right, suggest looking at the code on the end of this https://forum.pjrc.com/threads/34800-Are-Teensy3-2-Pins-High-Low-and-Tri-State to truly disable the inputs, though that will take even longer to exit that state I suspect, so may not be useful during operation (might be worth it if having a sleep mode)
 
Last edited:
Sorry,

Again it would be better if you would report if your buttons are working properly with simple code like I mentioned and/or the standard button library. That way those who are trying to help you, have a clue if the issue is hardware and/or your code.

My gut tells me GremlinWrangler is correct the issue is with your code as you enable the INPUT_PULLUP resister the moment before you test the button. So probably still low when you test...

Again I am not a low power expert, but wonder how much power the input PU resistors use up when they are simply holding the IO pin HIGH? Do you actually save anything if you are constantly turning the PU on and off? i.e. not show how often you toggle them...

What happens if you comment out the pinMode(pin, INPUT)? such that the pins are left in INPUT_PULLUP mode? do you get proper button readings?
 
I highly recommend you use the Bounce library for buttons. With Teensy selected, look in File > Examples > Teensy > USB_Keyboard > Buttons for an example.

Even if you *really* want to build your own code with more features, the well tested Bounce library is great for troubleshooting.
 
Ok so my new buttons came today. I spent the extra $2 and got a pack of 50 plus some jumper wire.

I'm looking at the example you pointed me to and it looks pretty straight up I should be able to turn it into my button routine rather easily but it uses different names for functions. I don't really understand falling and raising edge but I assume they are like buttons pressed with rising edge being input pull-up and the reverse of that for falling edge.


What I can do instead of moving the character (for now), is have it open a different bitmap for each push. Once I get that straight I can add movement. Baby steps right?!!


Where and how do I name the pins I want to use
 
Last edited:
The 'Edge' is seen on a change from the current high or low state. A button pulled normally high with input_pullup will show a falling edge when pressed and grounded - then a rising edge when released as it returns high.
 
I'm looking at the example you pointed me to and it looks pretty straight up I should be able to turn it into my button routine rather easily but it uses different names for functions.
Not sure which example you are referring to ?
I don't really understand falling and raising edge but I assume they are like buttons pressed with rising edge being input pull-up and the reverse of that for falling edge.
As Defragster mentioned, Think about how the voltages are changing when you press the button, hold it, and release... If you are using a button that is using a PU resistor setting and the other side of the button is GND, then when you press the button, there will be an edge that is falling down to zero, it will stay zero as you hold the button and then when you release the button there will be an edge where it rises to the +3.3v... If your button is wired the opposite way than the Rise and Fall will have opposite meaning.

Where and how do I name the pins I want to use
Again not sure what you are referring to. If you are talking about the bounce library: https://www.pjrc.com/teensy/td_libs_Bounce.html
You specify the pin to the constructor of the bounce object.
 
Sorry I meant the example that paul listed in post #9. Which ill post now.......

Code:
/* Buttons to USB Keyboard Example

   You must select Keyboard from the "Tools > USB Type" menu

   This example code is in the public domain.
*/

#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);
Bounce button9 = Bounce(9, 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!
 }

void loop() { 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);  // Teensy++ LED, may need 1k resistor pullup
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);

  // 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();
  button9.update();

  // Check each button for "falling" edge.
  // Type a message on the Keyboard when each button presses
  // Update the Joystick buttons only upon changes.
  // falling = high (not pressed - voltage from pullup resistor)
  //           to low (pressed - button connects pin to ground)
  if (button0.fallingEdge()) {
    Keyboard.println("B0 press");
  }
  if (button1.fallingEdge()) {
    Keyboard.println("B1 press");
  }
  if (button2.fallingEdge()) {
    Keyboard.println("B2 press");
  }
  if (button3.fallingEdge()) {
    Keyboard.println("B3 press");
  }
  if (button4.fallingEdge()) {
    Keyboard.println("B4 press");
  }
  if (button5.fallingEdge()) {
    Keyboard.println("B5 press");
  }
  if (button6.fallingEdge()) {
    Keyboard.println("B6 press");
  }
  if (button7.fallingEdge()) {
    Keyboard.println("B7 press");
  }
  if (button8.fallingEdge()) {
    Keyboard.println("B8 press");
  }
  if (button9.fallingEdge()) {
    Keyboard.println("B9 press");
  }

  // 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()) {
    Keyboard.println("B0 release");
  }
  if (button1.risingEdge()) {
    Keyboard.println("B1 release");
  }
  if (button2.risingEdge()) {
    Keyboard.println("B2 release");
  }
  if (button3.risingEdge()) {
    Keyboard.println("B3 release");
  }
  if (button4.risingEdge()) {
    Keyboard.println("B4 release");
  }
  if (button5.risingEdge()) {
    Keyboard.println("B5 release");
  }
  if (button6.risingEdge()) {
    Keyboard.println("B6 release");
  }
  if (button7.risingEdge()) {
    Keyboard.println("B7 release");
  }
  if (button8.risingEdge()) {
    Keyboard.println("B8 release");
  }
  if (button9.risingEdge()) {
    Keyboard.println("B9 release");
  }
}


Ok so I was confused with this part because ive never seen anything in Arduino or teensy using pin 1-10, but I see now that the first number in the () is the pin number. I could prolly put these in my setting.c file that includes the buttons and pin numbers

Code:
 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);  // Teensy++ LED, may need 1k resistor pullup
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);

can this also be placed in the settings.c or would they be better in sketch or the grafx.h library
Code:
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);
Bounce button9 = Bounce(9, 10);

if I do so where would I include the bounce.h files correctly in my library or sketch so every thing will be used if I set it up like the above

then I stick the update functions in my begin and update functions in my grafx file where it updates the buttons and files which will update 20x a second.


ok now heres a stupid question........ I keep seeing things like printin() in functions but how exactly do I see these print ins?

Is there a way to shorten the 5 second boot time after powering up the teensy? It makes me do the jeopardy music while I count. Lol!!
 
Last edited:
As far as moving stuff around - give it a try - the compiler will die a horrible death of some sort if you get it wrong - and it might even be useful.

The pinmode()'s certainly should not be in loop(). Refer to the LIBRARY samples here? : pjrc.com/teensy/td_libs_Bounce.html

The samples are likely all single file - you'll need to make usable prototypes ( in header .h files ) for the compile/link to complete.

Serial.println() goes out USB to SerMon in IDE ( or TyCommander ). Alternatively Serial1.println() would go out Serial1 ( or other # ) to serial pins if you connected a second Teensy for Debug.
 
Sometimes wondering if you are pulling our legs or Joking around?

Never seen pins 1-10? Did you ever go through any of the simple example code that comes with Arduino?
For example DigitalInputPullup
Code:
void setup() {
  //start serial connection
  Serial.begin(9600);
  //configure pin2 as an input and enable the internal pull-up resistor
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);

}

void loop() {
  //read the pushbutton value into a variable
  int sensorVal = digitalRead(2);
  //print out the value of the pushbutton
  Serial.println(sensorVal);

  // Keep in mind the pullup means the pushbutton's
  // logic is inverted. It goes HIGH when it's open,
  // and LOW when it's pressed. Turn on pin 13 when the
  // button's pressed, and off when it's not:
  if (sensorVal == HIGH) {
    digitalWrite(13, LOW);
  } else {
    digitalWrite(13, HIGH);
  }
}
If you have questions about what different things do, like pinMode, look it up in the reference page... Not hard to do, go to help manual and click on Reference menu item. Also sometimes works with the help menu of find in reference...

println is part of the Serial class, and is same as print except it outputs an end of line character... (If you want to be more specific, it is part of the print class) and HardwareSerial class is a subclass of Stream, which is a sublass of print )

So if you look at the sources for print class you will see things like: size_t println(uint8_t b) { return print(b) + println(); }

So println of each type is simply a call to print of that type, plus println() which is:
Code:
size_t Print::println(void)
{
	uint8_t buf[2]={'\r', '\n'};
	return write(buf, 2);
}
i.e. it simply outputs CR and LF...

There is no real magic in using the bounce library. You use it like any other library. That is you include the header file where ever you are using a button class. If all of your code is in an .ino than put it there. If it is used only within some sub-section of code, like one class of your whole project, then include it in that source file. If you have a header file that includes a bounce object then include it in that header file. Again like any other class.

Note: I believe Teensyduino ships with two different bounce libraries. Bounce and Bounce2. Not sure if one is Bounce works better than Bounce2, but one thing I like about Bounce2 is you can setup which pin to use as part of setup instead as part of the constructor....
 
Maybe paul will jump in and clarify which library to use.

ok so I went ahead and did some reading about the serial monitor as ive only used it a couple times. I also made a new button pad and did a lil better solderin. I have a common ground from button to button with the ground wire in between both banks right and left. I didn't do anything fancy and soldered everything from the back like normal. I'm positive now that every button has a wire for pinning to he board.

so what I want to do is first use this sketch to test each button. I changed the keyboard to serial but cant seem to get anything on he serial monitor when I press a button. what I want is the serial monitor to tell me what button I'm pressing. Cant seem to get it working right tough.......

Code:
/* Buttons to USB Keyboard Example

   You must select Keyboard from the "Tools > USB Type" menu

   This example code is in the public domain.
*/

#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);
Bounce button9 = Bounce(9, 10);

void setup() {
  Serial.begin(38400);
  // 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!
  pinMode(33, INPUT_PULLUP);
  pinMode(34, INPUT_PULLUP);
  pinMode(35, INPUT_PULLUP);
  pinMode(36, INPUT_PULLUP);
  pinMode(38, INPUT_PULLUP);
  pinMode(25, INPUT_PULLUP);
  pinMode(26, INPUT_PULLUP);  // Teensy++ LED, may need 1k resistor pullup
  pinMode(27, INPUT_PULLUP);
  pinMode(28, INPUT_PULLUP);
  pinMode(30, INPUT_PULLUP);
}

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();
  button9.update();

  // Check each button for "falling" edge.
  // Type a message on the Keyboard when each button presses
  // Update the Joystick buttons only upon changes.
  // falling = high (not pressed - voltage from pullup resistor)
  //           to low (pressed - button connects pin to ground)
  if (button0.fallingEdge()) {
    Serial.println("B0 press");
  }
  if (button1.fallingEdge()) {
    Serial.println("B1 press");
  }
  if (button2.fallingEdge()) {
    Serial.println("B2 press");
  }
  if (button3.fallingEdge()) {
    Serial.println("B3 press");
  }
  if (button4.fallingEdge()) {
    Serial.println("B4 press");
  }
  if (button5.fallingEdge()) {
    Serial.println("B5 press");
  }
  if (button6.fallingEdge()) {
    Serial.println("B6 press");
  }
  if (button7.fallingEdge()) {
    Serial.println("B7 press");
  }
  if (button8.fallingEdge()) {
    Serial.println("B8 press");
  }
  if (button9.fallingEdge()) {
    Serial.println("B9 press");
  }

  // 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()) {
    Serial.println("B0 release");
  }
  if (button1.risingEdge()) {
    Serial.println("B1 release");
  }
  if (button2.risingEdge()) {
    Serial.println("B2 release");
  }
  if (button3.risingEdge()) {
    Serial.println("B3 release");
  }
  if (button4.risingEdge()) {
    Serial.println("B4 release");
  }
  if (button5.risingEdge()) {
    Serial.println("B5 release");
  }
  if (button6.risingEdge()) {
    Serial.println("B6 release");
  }
  if (button7.risingEdge()) {
    Serial.println("B7 release");
  }
  if (button8.risingEdge()) {
    Serial.println("B8 release");
  }
  if (button9.risingEdge()) {
    Serial.println("B9 release");
  }
}

any clues as to what I'm doing wrong?

I think I fixed the player movement as well. I had one of the buttons parameters filled wrong set wrong but I cant any thing from the buttons. I'm using the ground pin under pin 13 on the right side.
 
Scanning the code in setup() this catches my eye:
pinMode(33, INPUT_PULLUP);
pinMode(34, INPUT_PULLUP);
...
pinMode(30, INPUT_PULLUP);

Did that come with a cut and paste? Those pin numbers should equate to 0-9.

Those are valid pin numbers but are not 0-9.
 
Here's the sample code from the linked Bounce Lib page referred to above - try this code changing "buttonPin = 12" to a pin at a time for the pins used and recompile and test, it should work.

If using the IDE SerMon - make sure the Port selected has the Teensy on it, then open the SerMon window and the output should appear. Personally I use TyCommander as I find it to be a better Serial Monitor as it sees the Teensy for what they are - not through the Generic SerMon interface of a COM# port that can be moved.

<edit>: Also make sure you select 'USB Type: Serial' before doing the compile in the IDE - most other types don't usably provide code for USB data transfer.

Code:
#include <Bounce.h>

const int buttonPin = 12;
Bounce pushbutton = Bounce(buttonPin, 10);  // 10 ms debounce

void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  Serial.begin(57600);
  Serial.println("Pushbutton Bounce library test:");
}

byte previousState = HIGH;         // what state was the button last time
unsigned int count = 0;            // how many times has it changed to low
unsigned long countAt = 0;         // when count changed
unsigned int countPrinted = 0;     // last count printed

void loop() {
  if (pushbutton.update()) {
    if (pushbutton.fallingEdge()) {
      count = count + 1;
      countAt = millis();
    }
  } else {
    if (count != countPrinted) {
      unsigned long nowMillis = millis();
      if (nowMillis - countAt > 100) {
        Serial.print("count: ");
        Serial.println(count);
        countPrinted = count;
      }
    }
  }
}
 
Last edited:
Awesome thank you!!!!

I started off testing all the pins on the right side, 23-33, with a single button and all read as button pressed when ever I activated the tactile switch. Next I started testing the direction pad buttons one by one since they were on the right side. The select button works when plugged in and the right button send signal at the press but the other three buttons on that side only activate if pressing more than one button press at one time to send a signal even though they aren't hooked up.

so I did the same with the left side. The start button works but the others need more than one button to activate a button press.

Note:::: same behavior as with last button pad.
 
Last edited:
Sounds like you have the buttons mis-wired - with the ground routed through the other buttons?

I've never understood how to look at a button an KNOW which two are which - but four pin buttons have two common pairs that are connected on the button press. Check with a meter - continuity beep or resistance reading on an unconnected button - or if powered across a connected button watch for voltage. What you'll see is what the Teensy is seeing - the switches are not 1::1 switching to ground?
 
Ok got curious and dug an old button pad out that I had made a lil while back. It only has four buttons on it and soldered and grounded the same way. Just missing a metal clip so fixed that and plugged it in to test and every thing worked just fine.

My hypothesis is that I need to ground both sides separately with a ground wire for each side. I think having so many buttons on one ground is not letting the signal cross far enough to get to the ground on the T3.6. Will the analog ground work as a second ground input or should I use an interior pin?
 
So it was finding GND only when the other button closed. Not that you are using AGND elsewhere - but that shouldn't be needed or contaminated. As long as you have good connects and decent wire one GND should do it AFAIK. T_3.6 has three pin holes for GND. What resistance do you see on the farthest chained GND to the GND source? If very high step to each GND connect on the chain to find the weak one and resolder? Wouldn't hurt to GND both ends as it should be solid.
 
ok I machined out a new piece of perfboard and its ready to add buttons. just so theres no confusion how would you connect the dots?......

button_pad.png
 
GND in the middle and each end is a signal INPUT_PULLUP - and it doesn't matter how the switch is set in place - as long as the top and bottom are unconnected.
 
Status
Not open for further replies.
Back
Top