Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Touch buttons at once. #16

Open
Kprellz opened this issue May 11, 2021 · 1 comment
Open

Multiple Touch buttons at once. #16

Kprellz opened this issue May 11, 2021 · 1 comment

Comments

@Kprellz
Copy link

Kprellz commented May 11, 2021

  • Arduino board: Metro M0 Express

  • Arduino IDE version (found in Arduino -> About Arduino menu): 1.8.13

  • List the steps to reproduce the problem below (if possible attach a sketch or
    copy the sketch code in too):

I am trying to set up multiple touch sensitive buttons, upon running the example code it seems as though pressing any 1 touch sense work as intended, however if I touch 2 or more of them, the number seems to be unchanged or even to drop. Is there a work around for this? or is this a hardware issue?

@zero-is-one
Copy link

zero-is-one commented Jul 3, 2022

I think I found a solution to this. Reading this I stumbled upon the circuit python repo that has a touch reset feature. So I was able to get multitouch working with the code below. This seems a little hacky since you need to the begin() function in the "loop" function but I didn't have any issues. Perhaps someone who understands registers more could clean up the touchin_reset function and add it to the repo.

#include <Arduino.h>
#include "Adafruit_FreeTouch.h"

unsigned int buttonIndex = 0;
const int buttonCount = 6;
int buttonVals[buttonCount] = {-1,-1,-1,-1,-1,-1};
Adafruit_FreeTouch buttons[] = {
  Adafruit_FreeTouch(A0, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE),
  Adafruit_FreeTouch(A7, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE),
  Adafruit_FreeTouch(A9, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE),
  Adafruit_FreeTouch(A8, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE),  
  Adafruit_FreeTouch(A6, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE),  
  Adafruit_FreeTouch(A1, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE)
};

void setup() {

}

// https://github.com/adafruit/circuitpython/blob/main/ports/atmel-samd/common-hal/touchio/TouchIn.c#L114
void touchin_reset() {
    Ptc *ptc = ((Ptc *)PTC);
    if (ptc->CTRLA.bit.ENABLE == 1) {
        ptc->CTRLA.bit.ENABLE = 0;
        while (ptc->CTRLA.bit.ENABLE == 1) {
        }

        ptc->CTRLA.bit.SWRESET = 1;
        while (ptc->CTRLA.bit.SWRESET == 1) {
        }
    }
}

void loop() {

  touchin_reset();

  buttonIndex = buttonIndex+1;
  if (buttonIndex>=buttonCount){ buttonIndex = 0; }
  buttons[buttonIndex].begin();
  buttonVals[buttonIndex] =  buttons[buttonIndex].measure();

  for (unsigned int i = 0; i < buttonCount; i++)
  {
    Serial.print(buttonVals[i]);
    Serial.print(",");
  }
  
  Serial.println(buttonIndex); 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants