0

CPX Left buttonm (A) always on
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

CPX Left buttonm (A) always on

by dj_segfault on Sun Jul 07, 2019 4:38 am

Using the demo program, as well as my own program, the left button is always pressed. That is, CircuitPlayground.leftButton() and digitalRead(CPLAY_LEFTBUTTON) both return true all the time. The right button works as expected. The left button and right button feel the same. The left button is not physically stuck down.

The only extra hardware I have is a .96" OLED display hooked up through I2C. It doesn't look like those ports should matter at all.
Here is my code: https://pastebin.com/2z8TXG6V It always shows the left button as pressed

However, when I run the CPX Demo code, the left button works as expected! It's calling the same CircuitPlayground.leftButton() my program is, and getting different results.

Can anyone tell me what I'm doing wrong in my program? Thanks.

dj_segfault
 
Posts: 22
Joined: Wed Dec 20, 2017 5:15 am

Re: CPX Left buttonm (A) always on

by adafruit_support_mike on Mon Jul 08, 2019 3:59 am

Post the code you're using (between CODE tags please) and we'll take a look.

adafruit_support_mike
 
Posts: 59172
Joined: Thu Feb 11, 2010 2:51 pm

Re: CPX Left buttonm (A) always on

by dj_segfault on Mon Jul 08, 2019 4:24 am

I included a pastebin link to it above, but here it is. Tomorrow I will try commenting out things to see if that changes the left button response, but here is what I have right now:
Code: Select all | TOGGLE FULL SIZE

#include <Adafruit_Circuit_Playground.h>


#include <Wire.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


//// I2C
#define OLED_I2C_ADDRESS 0x3C

//// OLED
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//// BUTTONS
int CAP_THRESHOLD = 450;
const uint8_t BTN_UP = 0;     //readCap for A6
const uint8_t BTN_DOWN = 1;   //readCap for A7
const uint8_t BTN_SELECT = 9; //readCap for A2
const uint8_t BTN_BACK = 10;  //readCap for A3
const uint8_t BTN_USER1 = 6;  //readCap for A1
const uint8_t BTN_USER2 = 12;  //readCap for A0

//// Other
boolean DEBUG_MODE = true;

void debug(char* message) {
  Serial.println(message);
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println(message);
  display.display();

}

boolean isCapPressed(uint8_t capNum) {
  int capValue = CircuitPlayground.readCap(capNum, 30);
  if ( capValue > CAP_THRESHOLD) {
    CircuitPlayground.playTone(262, 10);
      Serial.print("Cap ");
      Serial.print(capNum);
      Serial.print(" pressed, value is ");
      Serial.println(capValue);
    return true;
  }
    Serial.print("Cap ");
    Serial.print(capNum);
    Serial.print(" NOT pressed, value is ");
    Serial.println(capValue);
  return false;
}


void setup() {
  Serial.begin(9600);

  CircuitPlayground.begin();


  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_I2C_ADDRESS)) {
    Serial.println("SSD1306 allocation failed");
    for (;;); // Don't proceed, loop forever
  }
  // Clear the buffer
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.display();
  Serial.println("Starting");
  Serial.print("Debug is ");
  Serial.println(DEBUG_MODE);
}

void loop() {
    if (CircuitPlayground.rightButton()) {
      debug("Right");
    }
   
    if (CircuitPlayground.leftButton()) {
      debug("Left");
    }


  if (isCapPressed(BTN_UP)) {
    debug("Up");
  }
  if (isCapPressed(BTN_DOWN)) {
    debug("Down");
  }
  if (isCapPressed(BTN_SELECT)) {
    debug("Select");
  }
  if (isCapPressed(BTN_BACK)) {
    debug("Back");
  }
  if (isCapPressed(BTN_USER1)) {
    debug("User 1");
  }
  if (isCapPressed(BTN_USER2)) {
    debug("User 2");
  }

  digitalWrite(13, 1);
  delay(1000);
  digitalWrite(13, 0);
}

dj_segfault
 
Posts: 22
Joined: Wed Dec 20, 2017 5:15 am

Re: CPX Left buttonm (A) always on

by dj_segfault on Tue Jul 09, 2019 2:40 am

OK I found the problem, and.... it's not fun.
The problem is the reset pin for the Adafruit_SSD1306 library (I'm using a .96" I2C one).
Code: Select all | TOGGLE FULL SIZE
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

The demo code has
Code: Select all | TOGGLE FULL SIZE
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)

D4 happens to be the left button on the CPX, so that forces the pin high I guess.

However, when I use -1 as the code says, the board crashes (program no longer running) when I press some of the touch pads or the left and right button. I tried other values, and the display doesn't light up at all.

How can I get around this? What's a good value to use?

dj_segfault
 
Posts: 22
Joined: Wed Dec 20, 2017 5:15 am

Re: CPX Left buttonm (A) always on

by adafruit_support_mike on Tue Jul 09, 2019 6:37 pm

Move the OLED RESET signal to some other pad on the CPX, and use that pad’s number.

If you don’t want to connect a reset signal, use the number of a pad you aren’t using for anything else. Check the pinout diagram to see which pins are defined but not necessary for your project:

https://learn.adafruit.com/adafruit-cir ... ss/pinouts

adafruit_support_mike
 
Posts: 59172
Joined: Thu Feb 11, 2010 2:51 pm

Re: CPX Left buttonm (A) always on

by dj_segfault on Wed Jul 10, 2019 12:43 pm

Surprisingly, using 6, which maps to A1/D6, works, and A1 still works as a capsense input.
I did notice sometimes when I upload the code, the OLED won't work until I press the reset switch on the CPX and I can live with that.

I will do more experimentation with passing in -1, and digging into the code more, I see there's a constructor where you can specify the reset pin is not to be used. I will try that too.

dj_segfault
 
Posts: 22
Joined: Wed Dec 20, 2017 5:15 am

Re: CPX Left buttonm (A) always on

by adafruit_support_mike on Thu Jul 11, 2019 12:13 pm

Glad to hear you got it working.

The capacitive sensing system shifts between setting a pin to OUTPUT so it can charge the touchpad, then setting it to INPUT to see how long it takes the pad to discharge. The OLED reset signal is a ‘flip low then high’ that occurs when the driver object is created. They should stay mostly out of each other’s way.

The touch sensing might generate unwanted reset pulses if you connected the pad to the OLED, but can’t cause problems if it isn’t connected.

adafruit_support_mike
 
Posts: 59172
Joined: Thu Feb 11, 2010 2:51 pm

Re: CPX Left buttonm (A) always on

by dj_segfault on Thu Jul 11, 2019 2:25 pm

Thanks for your excellent detailed response. I appreciate understanding how both mechanisms work, it helps in modeling what it will do in different situations.

dj_segfault
 
Posts: 22
Joined: Wed Dec 20, 2017 5:15 am

Please be positive and constructive with your questions and comments.