Button problems

Status
Not open for further replies.
It seems pushing in a false bounce keypress could interfere with the real user choice? If they pushed alternate command button - the next scan there would be two hits to deal with and one would be 'forced'. Just using a state variable to track last seen button would be easier to change state based on the next true button press.
 
OK. Remember whe I said I needed to look at the examples more. this could be one ogf those times.

After many fruitless searches, I decided to look at the examples and found the rebounce() example.........

Code:
#include <Bounce.h>

// As long as the button is held down, the LED will blink
// Build the circuit indicated here: http://arduino.cc/en/Tutorial/Button

#define BUTTON 2
#define LED 13

// A variable to store the current LED state
int ledState = LOW;

// Instantiate a Bounce object with a 5 millisecond debounce time
Bounce bouncer = Bounce( BUTTON,5 ); 

void setup() {
  pinMode(BUTTON,INPUT);
  pinMode(LED,OUTPUT);
}

void loop() {
  
 // Update and monitor a change of input
  if ( bouncer.update() ) {
    
    // Get the state of the button
   int value = bouncer.read();
 
   // Toggle the LED if the button is held
   if ( value == HIGH) {
     // Make the button retrigger in 500 milliseconds
     bouncer.rebounce(500);
     if ( ledState == LOW ) {
       ledState = HIGH;
     } else {
       ledState = LOW;
     }
   } else {
     ledState = LOW;
   }
 
    digitalWrite(LED, ledState );
    
  }
 
 
 
}

looks like I can use this for what I need.



Edit:: so I would replace bounce.fallingEdge() with bounce.rebounce then do the states and add the drawBitMap instead digitalwrite()

led state would be button state?
 
Last edited:
Simple fix. I played around for a while but the easiest thing to do was just to

ButtonUp.rebounce(10); in the void loop

Think I might try to make a header file at some point. That way there's not all that extra stuff in the sketch. But for now I still have graphical issues that need fixing.
 
Last edited:
ok so what I want to do is add a begin function to the bounce header files that declares the pinmodes in void set up.

this is the original begin.......
Code:
oid 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;

but when I call bounce. or Bounce. begin I get errors

Code:
Arduino: 1.8.1 (Windows 10), TD: 1.36, Board: "Teensy 3.6, Serial, 180 MHz, Faster, US English"

Gameriot_demo_all: In function 'void setup()':
Gameriot_demo_all:395: error: expected unqualified-id before '.' token
   Bounce.begin();

         ^

Multiple libraries were found for "SD.h"
 Used: C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\SD
 Not used: C:\Program Files (x86)\Arduino\libraries\SD
Multiple libraries were found for "Bounce.h"
 Used: C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Bounce
 Not used: C:\Users\chuck\OneDrive\Documents\Arduino\libraries\Bounce1
expected unqualified-id before '.' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

here is my begin for bounce...
Code:
void Bounce::begin() 
{

  pinMode(buttonUp, INPUT_PULLUP);
   pinMode(buttonDown, INPUT_PULLUP);
   pinMode(buttonLeft, INPUT_PULLUP);
   pinMode(buttonRight, INPUT_PULLUP);
   pinMode(buttonS, INPUT_PULLUP);
   pinMode(buttonX, INPUT_PULLUP);
   pinMode(buttonY, INPUT_PULLUP);
   pinMode(buttonA, INPUT_PULLUP);
   pinMode(buttonB, INPUT_PULLUP);
   pinMode(buttonT, INPUT_PULLUP); 
}

Edit:::

Ok so I figured it out but it didn't have the effect I wanted. I'll deal with that later.
 
Last edited:
Ok I finally got the Arduino version of tetris to work for my teensy using my llibrary but once again I'm having trouble with controls. The set up shouldn't be that hard. But its written for joystick. I can get one button working which is set up to work on a digital pin. The other two move functions X and Y are analog. I've tried naming two pins on my teensy inhabited by button wires A whatever but it still wont move. I'm not understanding something here.........

here is the joystick file.....
Code:
/*
    Arduino Tetris
    Copyright (C) 2015 João André Esteves Vilaça 
    
    https://github.com/vilaca/Handheld-Color-Console

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef JOYSTICKCPP
#define JOYSTICKCPP

#include <Arduino.h>

// analog pins

#define XPIN        0
#define YPIN        1

// digital pin

#define FIREPIN     2

// joystick center for both axis

#define CENTER      512

class Joystick
{
  public:

    // joystick position constants 
    
    static const int NEUTRAL = 0;
    static const int SOFT = 1;
    static const int HARD = 2;
    static const int HARDER = 3;

    static void init ()
    {
      pinMode ( FIREPIN, INPUT_PULLUP );
    }

    static int getX()
    {
      return getPosition(XPIN) * -1;
    }

    static int getY()
    {
      return getPosition(YPIN) * -1;
    }

    static boolean fire()
    {
      return digitalRead(FIREPIN) == LOW;
    }

    static void waitForRelease()
    {
      while (fire());
    }

    static void waitForRelease(int howLong)
    {
      int c = 0;
      do
      {
        delay (10);
        c += 10;
      }
      while ((fire() || getY() != 0 || getX() != 0) && c < howLong);
    }

    static void waitForClick()
    {
      while (!fire());
    }

  private:

    static int getPosition (int pin)
    {
      const int n = analogRead(pin) - CENTER;

      return n / 128;
    }
};

#endif

Here is the user input code
Code:
void userInput(unsigned long now)
  {
    unsigned long waited = now - lastInput;

    int jx = Joystick::getX();    

    int move = INPUT_WAIT_MOVE / jx;

    if ( jx < Joystick::NEUTRAL && waited > -move)
    {
      if  (x > 0 && !touches(-1, 0, 0))
      {
        x--;
        lastInput = now;
        draw();
      }
    }
    else if ( jx > Joystick::NEUTRAL && waited > move )
    {
      if ( x < BOARD_WIDTH && !touches(1, 0, 0))
      {
        x++;
        lastInput = now;
        draw();
      }
    }
    
    if ( Joystick::fire() )
    {
      while ( !touches(0, 1, 0 ))
      {
        y++;
      }
      lastInput = now;
      draw();
    }

    int my = Joystick::getY();
    if (( my == -Joystick::HARD && waited > INPUT_WAIT_ROT ) || ( my == -Joystick::HARDER && waited > INPUT_WAIT_ROT / 2 ))
    {
      if (Joystick::getY() < 0 && !touches(0, 0, 1))
      {
        rot++;
        rot %= shapes[current];
        lastInput = now;
        draw();
      }
    }
  }

Why can't I use the analog pins when I name them either in actual PIN number or A16
 
Last edited:
What values are you joysticks returning? i.e have you simply split out your code into a dummy sketch and see what values getX and getY are returning? If they don't look right, look at the function they are calling getPosition. What values is it returning? If not looking good, then maybe have that function print out what the analogRead is returning, does it change when you move the joystick? Does the pin number passed in look correct? ...
 
I just got everything up and running with Arduino and teensy. Started with a fresh copy of my grafx file, then added the missing functions that are needed by the tetris program to show the score and type centered print. and compiled my Dune demo and every thing works. After that I started with the old fresh copy of the tetris program and stripped it of its sound stuff that's not needed right now and can easily be added later.

I removed tftv2.cpp and h files as well. Then I included the Grafx files <> so it points to the only Grafx file unzipped on my computer and in my documents/Arduino/libraries folder. Now I can compile again use one button on the program when it boots. BUUUUUUUUUUTTT,,, this where my problems start........

The controls as discussed are joystick. So the analog pin x judges the signal right or left by this set of defines.

Code:
 static const int NEUTRAL = 0;
    static const int SOFT = 1;          /////// joystick readings
    static const int HARD = 2;
    static const int HARDER = 3;


#define XPIN        0

 static int getX()
    {
      return getPosition(XPIN) * -1;
    }

Get position is the position of the joystick left or right. Then uses the user inputs here to move the blocks left or right....

Code:
void userInput(unsigned long now)
  {
    unsigned long waited = now - lastInput;

    int jx = Joystick::getX();    

    int move = INPUT_WAIT_MOVE / jx;

    if ( jx < Joystick::NEUTRAL && waited > -move) 
    {
      if  (x > 0 && !touches(-1, 0, 0))
      {
        x--;
        lastInput = now;
        draw();
      }
    }
    else if ( jx > Joystick::NEUTRAL && waited > move )
    {
      if ( x < BOARD_WIDTH && !touches(1, 0, 0))
      {
        x++;
        lastInput = now;
        draw();
      }
    }
    
    if ( Joystick::fire() )
    {
      while ( !touches(0, 1, 0 ))
      {
        y++;
      }
      lastInput = now;
      draw();
    }

    int my = Joystick::getY();
    if (( my == -Joystick::HARD && waited > INPUT_WAIT_ROT ) || ( my == -Joystick::HARDER && waited > INPUT_WAIT_ROT / 2 ))
    {
      if (Joystick::getY() < 0 && !touches(0, 0, 1))
      {
        rot++;
        rot %= shapes[current];
        lastInput = now;
        draw();
      }
    }
  }

The first two parts move the blocks position left or right and the third input rotes the block. Fire drops the brick and starts the game. Fire works on digital pin and works. its the left, right and rotate functions that need help.

I tried something like this.....

Code:
void userInput(unsigned long now)
  {

//   if (ButtonA.update());
   if (ButtonLeft.update());
   if (ButtonRight.update());

    unsigned long waited = now - lastInput;

    int jx = Joystick::getX();    

    int move = INPUT_WAIT_MOVE / jx;

    if ( jx < (ButtonRight.fallingEdge()) > -move);
    {
      if  (x > 0 && !touches(-1, 0, 0))
      {
        x--;
        lastInput = now;
        draw();
      }
    }
     if ( jx > (ButtonLeft.fallingEdge()) > move )
    {
      if ( x < BOARD_WIDTH && !touches(1, 0, 0))
      {
        x++;
        lastInput = now;
        draw();
      }
    }
    
    if ( Joystick::fire() )
    {
      while ( !touches(0, 1, 0 ))
      {
        y++;
      }
      lastInput = now;
      draw();
    }

    int my = Joystick::getY();
    if (( my == -Joystick::HARD && waited > INPUT_WAIT_ROT ) || ( my == -Joystick::HARDER && waited > INPUT_WAIT_ROT / 2 ))
    {
      if (Joystick::getY() < 0 && !touches(0, 0, 1))
      {
        rot++;
        rot %= shapes[current];
        lastInput = now;
        draw();
      }
    }
  }

When I try to run the above in my code it starts giving me the multiple functions errors so I dont know if it works or not. which is weird because I degugged other stuff in the file like the draw commands and they don't give me the problem.

I'm including the zip its literally 4 short files with the tetris.cpp and joystick.cpp being the problem files.

Or visit me here.......

https://github.com/Duhjoker/GamerRIot-master
 

Attachments

  • Tetris_teensy.zip
    7.1 KB · Views: 104
Last edited:
My guess is these 2 lines are probably causing your troubles:

Code:
#include "joystick.cpp"
#include "tetris.cpp"

Arduino is compiling these files, since they have a .cpp extension.

But then you're including their full text into your .ino file, which Arduino converts to a .cpp and compiles. So of course anything in those two .cpp files is getting compiled twice. That's very likely the cause of the duplicate conflicts.

The most common practice is to create a .h file corresponding to each .cpp file. The .h code declares all the stuff the .cpp has that's meant to be accessed from the rest of your project. The .cpp file actually defines it with the real code. This way the .cpp file and all your other files can include the .h file, so all parts of your project have a consistent declaration of whatever code the .cpp file provides.

Of course there are many other ways you could solve this, without going to all the trouble to create "proper" .h files. Hopefully just understanding what's going on helps?
 
Ok well that's counterproductive. I was looking at other OG Gamebuino game code and noticed that none of them use a .cpp except as the graphics files .cpp. From what I can see the Tetris.cpp is written like any other ino file. Would making the file an .ino file be better? Would that work?

Also what's a better Compiler?
 
Paul of course noted the issue. The IDE already takes liberties in the INO file as noted recently to deal with missing prototypes in the INO.

What generally works for any use of compiled "C" is having the sources (code or data) included to the build from a single instance of the file - if you need to refer to shared data or functions then put Prototypes in a .H header file, and include the header file in all the source files that define or refer to those shared function or data items.

Looking at example Teensy libraries will show that being done. Or any of the multiple file sample sketches - like for the Audio Tutorial where 'sound' data is in a CPP file and prototyped in a header that goes into the INO file.

C files are compiled to create source for any element defined in them. For elements not defined in that file the required prototype gives it a placeholder telling it what to expect to find from outside source at link time. Having the source code/data in two places makes two copies confusing the linker. Not having the prototype for undefined elements causes the compile to fail as it doesn't know how to address it.
 
Last edited:
Ok so I made two .h files for the .cpp files. I think I finally have it but I get this single error.....

Code:
c:\program files (x86)\arduino\hardware\tools\arm\bin\../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/bin/ar.exe: unable to rename 'C:\Users\chuck\AppData\Local\Temp\arduino_build_871848/core/core.a'; reason: File exists

Sorry. Edited the code block to specific about the problem
 
Last edited:
Bounce and SD should come from the Teensy install tree - or your SKETCHBOOK copy of them.

This is showing a mysterious copy of each that should be removed and the IDE restarted:

Multiple libraries were found for "SD.h"
Used: C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\SD << Good
Not used: C:\Program Files (x86)\Arduino\libraries\SD << REMOVE or ignore as the right one was selected

Multiple libraries were found for "Bounce.h"
Used: C:\Users\chuck\Documents\Arduino\libraries\Bounce << Not the Teensy copy,
Not used: C:\Program Files (x86)\Arduino\hardware\teensy\avr\libraries\Bounce << Should use this one?

Error is from including non-T_3.6 version of the code that should come from :: I:\arduino-1.8.1\hardware\teensy\avr\libraries\Bounce\Bounce.h

What version of the IDE and TeensyDuino are in use?
 
Those don't seem to be the problem. They always do that compiling or not. The first, SD is coming from both Arduino's library and the other from the Teensy AVR library. Bounce is the version I downloaded and the version from the teensyduino installed library.

This is not the multiple thing I was point out, it is the first line that's giving me the problem. I will edit this for the games new repository.

https://github.com/Duhjoker/Tetris
 
Last edited:
ok since I'm having trouble coming up with a way to separate the button functions using the bounce method,Yet... I want to use the button theme that came with the OG Gamebuino libaray.

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_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;
    
}

the h file looks like this.

Code:
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef BUTTONS_H
#define	BUTTONS_H


#include <Arduino.h>

//number of buttons
#define NUM_BTN         10
//buttons ID
#if DISPLAY_ROT == 0
	#define BTN_UP      1
	#define BTN_RIGHT   2
	#define BTN_DOWN    3
	#define BTN_LEFT    0
#elif DISPLAY_ROT == ROTCCW
	#define BTN_UP      2
	#define BTN_RIGHT   3
	#define BTN_DOWN    0
	#define BTN_LEFT    1
#elif DISPLAY_ROT == ROT180
	#define BTN_UP      3
	#define BTN_RIGHT   0
	#define BTN_DOWN    1
	#define BTN_LEFT    2
#elif DISPLAY_ROT == ROTCW
	#define BTN_UP      0
	#define BTN_RIGHT   1
	#define BTN_DOWN    2
	#define BTN_LEFT    3
#endif
#define BTN_A           4
#define BTN_B           5
#define BTN_X           6
#define BTN_Y           7

#define BTN_S           8
#define BTN_T           9
//buttons pins
#define BTN_UP_PIN      33
#define BTN_RIGHT_PIN   17
#define BTN_DOWN_PIN    38
#define BTN_LEFT_PIN    35

#define BTN_A_PIN       28
#define BTN_B_PIN       30
#define BTN_X_PIN       26
#define BTN_Y_PIN       32

#define BTN_S_PIN       21
#define BTN_T_PIN       4


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 */


when I use bounce the input is instantaneous. To test the buttons library I ran this sketch...

Code:
#include <SPI.h>
#include <GrafxT3.h>
#include <SD.h>


#define TFT_DC  9
#define TFT_CS 10
#define TFT_RST 7
#define TFT_SCK 13
#define TFT_MISO 39
#define TFT_MOSI 11

GrafxT3 tft = GrafxT3(TFT_CS, TFT_DC, TFT_RST, TFT_MOSI, TFT_SCK, TFT_MISO, &SPIN);

uint8_t use_fb = 1;
uint8_t use_clip_rect = 0;
uint8_t use_set_origin = 0;



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

 int x=-0,y=0;

const byte square[] PROGMEM = {32,32,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111,
B11111111,B11111111,B11111111,B11111111};




void setup() {
  Serial.begin(38400);
  long unsigned debug_start = millis();
  while (!Serial && ((millis() - debug_start) <= 5000));
  Serial.println("serial ok, testing lib...");
  tft.begin();
  tft.setFrameRate(62);
  tft.persistence = false;
   
    SPI.setMISO(39);
  //     tft.useFrameBuffer(use_fb);
}

/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
void loop(void) {
  tft.fillScreen(BLACK);
  tft.updateAll();
//updates the GameR (the display, the sound, the buttons, everyyhing)
  //returns true when it's time to render a new frame (20 times/second)
 if(tft.buttons.repeat(BTN_UP,1)){
       tft.drawBitmap(player_x, player_y, square,32,32,RED);
       player_direction = 1;
    }
   

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

  if(tft.buttons.repeat(BTN_DOWN,1)){

       tft.drawBitmap(player_x, player_y, square,32,32,GREEN);
       player_direction = 2;
    }
   

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


if(tft.buttons.repeat(BTN_LEFT,1)){
       tft.drawBitmap(player_x, player_y, square,32,32,BLUE);
       player_direction = 3;
    }
   

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

if(tft.buttons.repeat(BTN_RIGHT,1)){
       tft.drawBitmap(player_x, player_y, square,32,32,YELLOW);
       player_direction = 4;
    }
   

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

if(tft.buttons.repeat(BTN_S,1)){
       tft.drawBitmap(player_x, player_y, square,32,32,PURPLE);
       player_direction = 5;
    }
   

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

 if(tft.buttons.repeat(BTN_X,1)){
       tft.drawBitmap(player_x, player_y, square,32,32,PINK);
       player_direction = 6;
    }
   

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

if(tft.buttons.repeat(BTN_Y,1)){
       tft.drawBitmap(player_x, player_y, square,32,32,LIGHTBROWN);
       player_direction = 7;
    }
   

   /////////////////////////////////////////////////////////////
   ////////////////////////////////////////////////////////////
if(tft.buttons.repeat(BTN_A,1)){
       tft.drawBitmap(player_x, player_y, square,32,32,LIGHTGREY);
       player_direction = 8;
    }
   

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

if(tft.buttons.repeat(BTN_B,1)){
       tft.drawBitmap(player_x, player_y, square,32,32,CYAN);
       player_direction = 9;
    }
   

   ////////////////////////////////////////////////////////////
   ////////////////////////////////////////////////////////////
   
if(tft.buttons.repeat(BTN_T,1)){
       tft.drawBitmap(player_x, player_y, square,32,32,MAGENTA);
       player_direction = 10;
    }
   

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

    if (player_direction == 1){
   tft.drawBitmap(player_x, player_y, square,32,32,RED);
   }

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

 else if (player_direction == 2){
   tft.drawBitmap(player_x, player_y, square,32,32,GREEN);
   }
   
   ////////////////////////////////////////////////////////////
   ////////////////////////////////////////////////////////////

 else if (player_direction == 3){
   tft.drawBitmap(player_x, player_y, square,32,32,BLUE);
   }

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

 else if (player_direction == 4){
   tft.drawBitmap(player_x, player_y, square,32,32,YELLOW);
   }

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

 else if (player_direction == 5){
   tft.drawBitmap(player_x, player_y, square,32,32,PURPLE);
   }

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

 else if (player_direction == 6){
   tft.drawBitmap(player_x, player_y, square,32,32,PINK);
   }

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

 else if (player_direction == 7){
   tft.drawBitmap(player_x, player_y, square,32,32,LIGHTBROWN);
   }

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

 else if (player_direction == 8){
   tft.drawBitmap(player_x, player_y, square,32,32,LIGHTGREY);
   }

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

 else if (player_direction == 9){
   tft.drawBitmap(player_x, player_y, square,32,32,CYAN);
   }

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

 else if (player_direction == 10){
   tft.drawBitmap(player_x, player_y, square,32,32,MAGENTA);
   }
 tft.updateScreen();
   }

Now the sketch above uses the buttons library. if I press abxy I get a color change, most show the correct color, but up down and left do nothing. Further more if I use the buttons library in my game I get no play on the dpad buttons.

What could be causing the problem? We know the buttons work but why only 7 out of 10 using the buttons library over all with the bounce library?

Edit::

I fixed it.. there were two problems. the first was a declaration of of rotation of the device to change the rotation of the dpad.

next I removed this line from update() which was bringing the input down when not in use. for some reason the teensy didn't like it!

Code:
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 // duhjoker comment this out
    }
	

}
 
Last edited:
Status
Not open for further replies.
Back
Top