Gamepad using TTP229 I2c help with programming

Hello

i was working on gamepad project but Instead of using pin as input button, i use TTP229 I2C for input, its more then 2 week I'm working on it but every time i fail in programing because I'm rookie in Arduino and programing

This is code i used

#include <Gamepad.h>
#define SCL 3
#define SDO 4
byte Key;

byte Read_TTP229_Keypad(void)
{
 byte Num;
 byte Key_State = 0;
 for(Num = 1; Num <= 16; Num++)
 {
   digitalWrite(SCL, LOW);
   if (!digitalRead(SDO))
     Key_State = Num;
     digitalWrite(SCL, HIGH);
 } 
 return Key_State;
}
int rightXcenter = 500;
int rightYcenter = 500;
int leftXcenter = 500;
int leftYcenter = 500;
double multiplierRX = 0.254; //127 / 500
double multiplierRY = 0.254;
double multiplierLX = 0.254;
double multiplierLY = 0.254;

Gamepad gp;

void setup() {

 pinMode(7, INPUT);
 pinMode(6, INPUT);
 pinMode(5, INPUT);
 pinMode(4, INPUT);
 Serial.begin(9600);
 pinMode(SCL, OUTPUT); 
 pinMode(SDO, INPUT);
  
  calibrate();
}

void loop() {
  int lx, ly, rx, ry;
  lx = analogRead(7);
  ly = analogRead(6);
  rx = analogRead(5);
  ry = analogRead(4);
  //we need to convert a 0-1000 to -127 - 127
  lx = floor((lx - leftXcenter) * multiplierLX);
  ly = floor((ly - leftYcenter) * multiplierLY);
  rx = floor((rx - rightXcenter) * multiplierRX);
  ry = floor((ry - rightYcenter) * multiplierRY);
  if(lx > 127) lx = 127;
  if(ly > 127) ly = 127;
  if(rx > 127) rx = 127;
  if(ry > 127) ry = 127;
  gp.setLeftXaxis(lx);
  gp.setRightXaxis(rx);
  gp.setLeftYaxis(ly);
  gp.setRightYaxis(ry);
  
  int UPLEFT, UPRIGHT, UP, DOWN, LEFT, RIGHT, RIGHTBUTTON, LEFTBUTTON, X, Y, A, B;

  UPLEFT = digitalRead(14);
  UPRIGHT = digitalRead(15);
  RIGHTBUTTON = digitalRead(0);
  LEFTBUTTON = digitalRead(1);
  UP = digitalRead(4);
  DOWN = digitalRead(5);
  LEFT = digitalRead(6);
  RIGHT = digitalRead(7);
  X = digitalRead(8);
  Y = digitalRead(9);
  A = digitalRead(10);
  B = digitalRead(16);
  
      
  
  if(UPLEFT == LOW)    //
    gp.setButtonState(0, true);
  else
    gp.setButtonState(0, false);

  if(UPRIGHT == LOW)
    gp.setButtonState(1, true);
  else
    gp.setButtonState(1, false);
 
  if(UP == LOW)
    gp.setButtonState(2, true);
  else
    gp.setButtonState(2, false);

  if(DOWN == LOW)
    gp.setButtonState(3, true);
  else
    gp.setButtonState(3, false);    

  if(LEFT == LOW)
    gp.setButtonState(4, true);
  else
    gp.setButtonState(4, false); 

  if(RIGHT == LOW)
    gp.setButtonState(5, true);
  else
    gp.setButtonState(5, false);     

  if(RIGHTBUTTON == LOW)
    gp.setButtonState(6, true);
  else
    gp.setButtonState(6, false);   

  if(LEFTBUTTON == LOW)
    gp.setButtonState(7, true);
  else
    gp.setButtonState(7, false);     

  if(X == LOW)
    gp.setButtonState(8, true);
  else
    gp.setButtonState(8, false);

  if(Y == LOW)
    gp.setButtonState(9, true);
  else
    gp.setButtonState(9, false);

  if(A == LOW)
    gp.setButtonState(10, true);
  else
    gp.setButtonState(10, false);  

  if(B == LOW)
    gp.setButtonState(11, true);
  else
    gp.setButtonState(11, false);       

  delay(20);
}

void calibrate()
{
  int lx, ly, rx, ry;
  int i = 0;
  while(i < 13)
  {
    lx = analogRead(7);
    ly = analogRead(6);
    rx = analogRead(5);
    ry = analogRead(4);
    bool validLX = lx > (leftXcenter - 100) && lx < (leftXcenter + 100);
    bool validLY = ly > (leftYcenter - 100) && ly < (leftYcenter + 100);
    bool validRX = rx > (rightXcenter - 100) && rx < (rightXcenter + 100);
    bool validRY = ry > (rightYcenter - 100) && ry < (rightYcenter + 100);
    if(validLX && validLY && validRX && validRY)
    {
      i++;
      //nothing to do here!
    }
    else i = 0;
    delay(20);
  }
  leftXcenter = lx;
  leftYcenter = ly;
  rightXcenter = rx;
  rightYcenter = ry;
  multiplierLX = (double)127 / (double)lx;
  multiplierLY = (double)127 / (double)ly;
  multiplierRX = (double)127 / (double)rx;
  multiplierRY = (double)127 / (double)ry;
}

My problem is in this line

  int UPLEFT, UPRIGHT, UP, DOWN, LEFT, RIGHT, RIGHTBUTTON, LEFTBUTTON, X, Y, A, B;
  UPLEFT = digitalRead(14);
  UPRIGHT = digitalRead(15);
  RIGHTBUTTON = digitalRead(0);
  LEFTBUTTON = digitalRead(1);
  UP = digitalRead(4);
  DOWN = digitalRead(5);
  LEFT = digitalRead(6);
  RIGHT = digitalRead(7);
  X = digitalRead(8);
  Y = digitalRead(9);
  A = digitalRead(10);
  B = digitalRead(16);
  

this lines defined for particular function equal input pin but i want Instead UPRIGHT = digitalRead(15), for example key 2 = UPRIGHT
I do not know what the correct code for this part, i really confused
Please help or guide me to solve my problem
Thanks

What is this supposed to do ?

1 Like

Have you tested your Read_TTP229_Keypad function with some code like

#define SCL 3
#define SDO 4

byte Read_TTP229_Keypad(void)
{
  byte Num;
  byte Key_State = 0;
  for (Num = 1; Num <= 16; Num++)
  {
    digitalWrite(SCL, LOW);
    if (!digitalRead(SDO))
      Key_State = Num;
    digitalWrite(SCL, HIGH);
  }
  return Key_State;
}

void setup()
{

  Serial.begin(9600);
  pinMode(SCL, OUTPUT);
  pinMode(SDO, INPUT);
}

void loop()
{
  byte key = Read_TTP229_Keypad();
  Serial.println(key);
  delay(1000);
}

Note that one usually needs a short delay between activating a clock line and reading the associated data. But your code might work due to the way digitalRead() works.
It should print 0 if no key is pressed and 1..16 if a key is pressed.

If that works correctly, you can modify the rest of your code. If I understand you correctly, you want to replace all digitalRead stuff by the keypad. The following should do that; I can not test or compile.

E.g.

#include <Gamepad.h>

#define SCL 3
#define SDO 4
// number of buttons on gamepad
const byte numButtons = 12;

Gamepad gp;

byte Read_TTP229_Keypad(void)
{
  byte Num;
  byte Key_State = 0;
  for (Num = 1; Num <= 16; Num++)
  {
    digitalWrite(SCL, LOW);
    if (!digitalRead(SDO))
      Key_State = Num;
    digitalWrite(SCL, HIGH);
  }
  return Key_State;
}

void setup()
{
  Serial.begin(9600);
  pinMode(SCL, OUTPUT);
  pinMode(SDO, INPUT);
}

void loop()
{
  byte key = Read_TTP229_Keypad();
  Serial.println(key);

  // if no key pressed or number does not reflect an existing gamepad button
  if (key == 0 || key >= numButtons)
  {
    // clear all gamepad buttons
    for (byte cnt = 0; cnt < numButtons); cnt++)
    {
      gp.setButtonState(cnt, false);
    }
  }
  // if 'valid' key
  else
  {
    // set the gamepad button state
    gp.setButtonState(key - 1, true);
  }

  delay(1000);
}

Note: code for demo purposes only.

1 Like

Hi,
Welcome to the forum.

What model arduino are you using?
Can you please post a circuit diagram?

Have you run some simple code to test that you can communicate with the Capacitive KeyPad?
You need to establish that you can get data from the keypad, FORGET your code for the moment and write or use example code with library.

I see you are not using the TTP229 library.

It has examples as well.

Tom... :smiley: :+1: :coffee: :australia:

1 Like

first im very thankful from every one who reply and guide me
Code of "sterretje" worked perfectly and im very grateful
i don't know why my Arduino pro micro suddenly stopped working but i will trying to fix it
i don't think problem was from programing because it worked
can some one guess why it failed
thank you

Please explain "stopped working".
Which PC operating system?

1 Like

thanks for you reply sir
I was testing gamepad and it was working so well but suddenly it was disconnected and Windows did not identity or read my arduino anymore, board have power and only one red light LED is on
I tried to reset it but it did not work but later I will try to program it with another arduino
i use windows 7 and linux on my other PC and tested it on both of them and they can not read arduino usb

Have you connected a reset button to the ProMicro? If not, time to do so :wink:

Start an upload and double tap the reset button at the moment that the IDE reports the memory usage.

Let us know if it succeeds or not.

1 Like

thank you very much sir for your attention
you helped me a lot
i successfully reset the board and its working now but i still don't understand why this happen in first place
Thank you very much

32U4 boards can be reset by opening and closing the serial port with a baudrate of 1200 which will activate the bootloader so you can upload.

Part of the code that you upload is responsible for that; if your code has a flaw, it can affect that "reset" code and you can no longer activate the bootloader by opening and closing the serial port with a baudrate of 1200.

1 Like

great insight Sir
I have question about code part, with all respect but i just after upload only code you wright for me and tested the gamepad function this happen for me, that's why it's hard to believe there is a problem with code ,maybe because i did not wight rest of my code this happened, i really don't know
What do you think ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.