How we can use 4X4 touch key pad with one button library .

Can some one help me, how we can use a 4X4 touch key pad ttp229 with onebutton library. My idea is to get more functions with this combination.
There is an example of mp3 player using the ttp229 and dfplayer.
http://www.trainelectronics.com/Arduino/MP3Sound/DFPlayer-keypad/index.htm

touch_keypad_mp3_player.ino (3.73 KB)

You may have to write your own library to get (unnamed) desired features of the onebutton library on the touch keypad. I'm guessing you are looking for features like 'long press' and 'double-click'.

Dear johnwasser,
Thanks a lot for your reply, Your guess is 100% tru. :slight_smile:
I dont have enough programming experience so i try here to get help.

This is the state machine from the OneButton library. You will need to integrate something like this into each button of the touch button keypad. Instead of the "digitalRead()" to get the button state you need the information from the touch keypad library.

void OneButton::tick(void)
{
  // Detect the input information 
  int buttonLevel = digitalRead(_pin); // current button signal.
  unsigned long now = millis(); // current (relative) time in msecs.

  // Implementation of the state machine
  if (_state == 0) { // waiting for menu pin being pressed.
    if (buttonLevel == _buttonPressed) {
      _state = 1; // step to state 1
      _startTime = now; // remember starting time
    } // if

  } else if (_state == 1) { // waiting for menu pin being released.

    if ((buttonLevel == _buttonReleased) && ((unsigned long)(now - _startTime) < _debounceTicks)) {
      // button was released to quickly so I assume some debouncing.
	  // go back to state 0 without calling a function.
      _state = 0;

    } else if (buttonLevel == _buttonReleased) {
      _state = 2; // step to state 2
      _stopTime = now; // remember stopping time

    } else if ((buttonLevel == _buttonPressed) && ((unsigned long)(now - _startTime) > _pressTicks)) {
      _isLongPressed = true;  // Keep track of long press state
      if (_pressFunc) _pressFunc();
	  if (_longPressStartFunc) _longPressStartFunc();
	  if (_duringLongPressFunc) _duringLongPressFunc();
      _state = 6; // step to state 6
      
    } else {
      // wait. Stay in this state.
    } // if

  } else if (_state == 2) { // waiting for menu pin being pressed the second time or timeout.
    if (_doubleClickFunc == NULL || (unsigned long)(now - _startTime) > _clickTicks) {
      // this was only a single short click
      if (_clickFunc) _clickFunc();
      _state = 0; // restart.

    } else if ((buttonLevel == _buttonPressed) && ((unsigned long)(now - _stopTime) > _debounceTicks)) {
      _state = 3; // step to state 3
      _startTime = now; // remember starting time
    } // if

  } else if (_state == 3) { // waiting for menu pin being released finally.
    // Stay here for at least _debounceTicks because else we might end up in state 1 if the
    // button bounces for too long.
    if (buttonLevel == _buttonReleased && ((unsigned long)(now - _startTime) > _debounceTicks)) {
      // this was a 2 click sequence.
      if (_doubleClickFunc) _doubleClickFunc();
      _state = 0; // restart.
    } // if

  } else if (_state == 6) { // waiting for menu pin being release after long press.
    if (buttonLevel == _buttonReleased) {
	  _isLongPressed = false;  // Keep track of long press state
	  if(_longPressStopFunc) _longPressStopFunc();
      _state = 0; // restart.
    } else {
	  // button is being long pressed
	  _isLongPressed = true; // Keep track of long press state
	  if (_duringLongPressFunc) _duringLongPressFunc();
    } // if  

  } // if  
} // OneButton.tick()

How i can add? the touch switch is I2C :frowning: i have no idea.
If you have some free time do a favor plz have a look on the code.

/* d. bodnar revision - 11-11-15 This one works - be sure to jumper as per web page here:
*keypad code from TTP229 16 Key Capacitive Keypad (HCMODU0079) - forum.hobbycomponents.com
*Working with DF Player
The sketch assumes that the keypad is configured to 16 key active low mode
by shorting pads P1-3 and P1-P4 together (see schematic or sport forum for more
information). Connect the keypad to your Arduino as follows:

Keypad......Arduino
VCC.........+5V
GND.........GND
SCL.........Digital pin 8
SDO.........Digital pin 9

You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.

/* Define the digital pins used for the clock and data */
#define SCL_PIN 8
#define SDO_PIN 9

/* Used to store the key state */
byte Key;

#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
const int buttonPin = 3; // the number of the pushbutton pin
const int buttonPin2 = 4; // second button pin
int ledPin1 = 11; // laser control
const int ledPin2 = 12; // the number of the LED pin

int buusyPin = 10; // sound player busy
int bsy = 0;
int z = 0;

void setup()
{
/* Initialise the serial interface /
Serial.begin(9600);
/
Configure the clock and data pins */
pinMode(SCL_PIN, OUTPUT);
pinMode(SDO_PIN, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
Serial.begin (9600);
mp3_set_serial (Serial); //set Serial for DFPlayer-mini mp3 module
// mp3_reset();
delay (400);
mp3_set_volume (25); // 15 is low for unpowered speaker - had to remove reset to get vol to work
delay (400);
Serial.println("");
Serial.println("play MP3 files in sequence with trigger & random option v3.1c-smaller");
delay(100); // may be needed for initialization of sound
randomSeed(analogRead(0));
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
delay(1000);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}

/* Main program /
void loop()
{
Serial.println("at top of loop");
/
Read the current state of the keypad */
Key = Read_Keypad();

/* If a key has been pressed output it to the serial port */
if (Key)
{
Serial.println(Key);

/* Wait a little before reading again
so not to flood the serial port*/
// delay(1000);

mp3_play(Key);
dlayPrint();
}

}

/* Read the state of the keypad */
byte Read_Keypad(void)
{
byte Count;
byte Key_State = 0;

/* Pulse the clock pin 16 times (one for each key of the keypad)
and read the state of the data pin on each pulse */
for (Count = 1; Count <= 16; Count++)
{
digitalWrite(SCL_PIN, LOW);

/* If the data pin is low (active low mode) then store the
current key number */
if (!digitalRead(SDO_PIN))
Key_State = Count;

digitalWrite(SCL_PIN, HIGH);
}

return Key_State;
}

// routine to stay here till busy pin goes low once then goes high after speech item completes
void dlayPrint()
{
digitalWrite(ledPin1, HIGH);
while (digitalRead(buttonPin) == 1) { //wait for button to be released
//delay (100);
}

int bsyflag = 0;
Serial.println(" ");
Serial.print("busypin ");
for ( z = 0; z <= 10000; z++) {
bsy = digitalRead(buusyPin);
Serial.print(bsy);
delay(50);
if (bsyflag == 1 && bsy == 1) {
break;
}
if (digitalRead(buttonPin) == 1) {
break;
}
if (bsy == 0) {
bsyflag = 1;
}
Key = Read_Keypad();
if (Key)
{
break;
}

}
Serial.println(" ");
digitalWrite(ledPin1, LOW);
Serial.println("done");
}