Neotrellis delete button "cache"

Sorry if any of this uses incorrect terms. If i am correct the seesaw chip on the neotrellis holds a "cache" of button presses that the arduino reads when it is ready so the arduino can't miss one. what i want to do is delete this "cache" because i noticed during the animation function in this code any buttons pressed are read afterward and i want them not to be.

//////////////////////////////////////////////////////////////
// whack-a-mole game //
//////////////////////////////////////////////////////////////

#include "Adafruit_NeoTrellis.h"
#include <BlockNot.h>

BlockNot moleTimer(random(1000, 2000));  //In Milliseconds
BlockNot blueTimer(random(400, 1200));

Adafruit_NeoTrellis trellis;

////////////////////////////////////////////////////////////// variables

int randNum1;
int randNum2;
int count1;
int blueCount;
int redPoints = 0;
int speedPoints = 0;
////////////////////////////////////////////////////////////// buttons

//define a callback for key presses
TrellisCallback blink(keyEvent evt) {

  if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_RISING) {
    // Check is the pad pressed?
    if (trellis.pixels.getPixelColor(evt.bit.NUM) == 16711680) {  // if red
      trellis.pixels.setPixelColor(evt.bit.NUM, 0);
      trellis.pixels.show();
      redPoints++;
      if (redPoints > 20){
        speedPoints++;
        redPoints = 0;
      }
        for (uint16_t i = 0; i < trellis.pixels.numPixels() && trellis.pixels.getPixelColor(i) != 16711680; i++) {
          if (i == 15) {
            randNum1 = random(0, 16);
            for (count1 = 0; trellis.pixels.getPixelColor(randNum1) != 0; count1++) {
              // statement(s);
              randNum1 = random(0, 16);
              if (count1 > 20) {
                break;
              }
            }
            if (trellis.pixels.getPixelColor(randNum1) == 0) {
              trellis.pixels.setPixelColor(randNum1, 255, 0, 0);
              trellis.pixels.show();
            }
          }
        }
    } else if (trellis.pixels.getPixelColor(evt.bit.NUM) == 255) {  // if blue
      animation(0, 0, 255);
      trellis.pixels.setPixelColor(evt.bit.NUM, 0);
      trellis.pixels.show();
    } else {                                                   // if black
      trellis.pixels.setPixelColor(random(0, 16), 255, 0, 0);  // make more red
      trellis.pixels.setPixelColor(random(0, 16), 255, 0, 0);
      trellis.pixels.setPixelColor(random(0, 16), 255, 0, 0);
      trellis.pixels.setPixelColor(random(0, 16), 255, 0, 0);
      trellis.pixels.setPixelColor(random(0, 16), 255, 0, 0);
      trellis.pixels.setPixelColor(random(0, 16), 255, 0, 0);
      trellis.pixels.setPixelColor(random(0, 16), 255, 0, 0);
      trellis.pixels.show();
    }
  } else if (evt.bit.EDGE == SEESAW_KEYPAD_EDGE_FALLING) {
    // or is the pad released?
  }

  return 0;
}

////////////////////////////////////////////////////////////// setup

void setup() {
  Serial.begin(9600);
  // while(!Serial) delay(1);

  if (!trellis.begin()) {
    Serial.println("Could not start trellis, check wiring?");
    while (1) delay(1);
  } else {
    Serial.println("NeoPixel Trellis started");
  }

  //activate all keys and set callbacks
  for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) {
    trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING);
    trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING);
    trellis.registerCallback(i, blink);
  }

  //do a little animation to show we're on
  for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
    trellis.pixels.setPixelColor(i, Wheel(map(i, 0, trellis.pixels.numPixels(), 0, 255)));
    trellis.pixels.show();
    delay(50);
  }
  for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
    trellis.pixels.setPixelColor(i, 0x000000);
    trellis.pixels.show();
    delay(50);
  }
}

////////////////////////////////////////////////////////////// loop

void loop() {
  trellis.read();
  delay(15);
  if (moleTimer.TRIGGERED) {  // make new moles
    Serial.println("moleTimer TRIGGERED");
    moleTimer.setDuration(random(150, 2100-(speedPoints*100)));
    randNum1 = random(0, 16);
    for (count1 = 0; trellis.pixels.getPixelColor(randNum1) != 0; count1++) {
      // statement(s);
      randNum1 = random(0, 16);
      if (count1 > 20) {
        break;
      }
    }
    if (trellis.pixels.getPixelColor(randNum1) == 0) {
      trellis.pixels.setPixelColor(randNum1, 255, 0, 0);
      trellis.pixels.show();
    }
  }
  for (uint16_t i = 0; i < trellis.pixels.numPixels() && trellis.pixels.getPixelColor(i) != 0; i++) {
    if (i == 15) {
      animation(255, 255, 255);
    }
  }
  if (blueTimer.TRIGGERED) {
    Serial.println("blueTimer TRIGGERED");
    blueTimer.setDuration(random(400, 1200));
    randNum1 = random(0, 16);
    for (count1 = 0; trellis.pixels.getPixelColor(randNum1) != 0; count1++) {
      // statement(s);
      randNum1 = random(0, 16);
      if (count1 > 20) {
        break;
      }
    }
    if (trellis.pixels.getPixelColor(randNum1) == 0) {
      trellis.pixels.setPixelColor(randNum1, 0, 0, 255);
      trellis.pixels.show();
    }
    blueCount = 0;
    for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
      if (trellis.pixels.getPixelColor(i) == 255) {
        blueCount++;
      }
    }
    if (blueCount > 2) {
      randNum1 = random(0, 16);
      for (count1 = 0; trellis.pixels.getPixelColor(randNum1) != 255; count1++) {
        // statement(s);
        randNum1 = random(0, 16);
      }
      trellis.pixels.setPixelColor(randNum1, 255, 0, 0);
      trellis.pixels.show();
    }
  }
}


////////////////////////////////////////////////////////////// animation

// a little animation (the chosen color covers the whole screen)
// the peramiters decide what color the animation is so it can be used for other purposes.
void animation(int red, int green, int blue) {
  moleTimer.stop();
  for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
    trellis.pixels.setPixelColor(i, red, green, blue);
    trellis.pixels.show();
    delay(50);
  }
  for (uint16_t i = 0; i < trellis.pixels.numPixels(); i++) {
    trellis.pixels.setPixelColor(i, 0);
    trellis.pixels.show();
    delay(50);
  }
  trellis.read();
  trellis.pixels.clear();
  trellis.pixels.show();
  moleTimer.start();
}

////////////////////////////////////////////////////////////// rainbow color

// Input a value 0 to 255 to get a color value.
// The colors are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if (WheelPos < 85) {
    return trellis.pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
    WheelPos -= 85;
    return trellis.pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
    WheelPos -= 170;
    return trellis.pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  return 0;
}

One way to go, when You want the last press, a fresh press, is to read out the cache and wait.

the problem is that if i read the trellis it would activate the button callback.

And what does that cause? Can't it be handled?

@Railroader what i don't want it to do when i check after the animation (if i pressed a button during the animation while it's not checking) is for it to afterward check and spawn red into the scene. so i was wondering if there was a function i could put into my code to delete any existing cache

Have you read the Adafruit_NeoTrellis documentation?

If you know when to clear the "Cache", a simple read() should clear it, as it's actually a FIFO.

Also, if you follow this advice, try to deactivate the key the first time you detect the button falling, then re-activate it when the animation is over.

I'm not sure that works as you expect, but you may be able to deactivate the keypad by passing false as the third argument to activateKey().

You need to provide more documentation about the system. No way common helpers can help You with this not common device.

@embeddedkiddie

would i need to disable every key at the start and then enable every key at the end of the animation? because i wouldn't know specifically which buttons to de activate until i check. and i can't check during the animation.

so are you saying that i could modify this code to deactivate the keys and then run the normal code to activate

  //activate all keys and set callbacks
  for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) {
    trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING);
    trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING);
    trellis.registerCallback(i, blink);
  }

I have gotten it fixed by deactivating the buttons with this code

for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) {
    trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING, false);
    trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING, false);
  }

and activating with this

for (int i = 0; i < NEO_TRELLIS_NUM_KEYS; i++) {
    trellis.activateKey(i, SEESAW_KEYPAD_EDGE_RISING);
    trellis.activateKey(i, SEESAW_KEYPAD_EDGE_FALLING);
    trellis.registerCallback(i, blink);
  }

If this solves your problem, I'm happy. However, does deactivating all keypads meet your expectations for game design?

If you wanted to deactivate only certain keypads, such as keypads during animations, you'd need to keep track of such as the keypad numbers, right?

Anyway, enjoy programming and your game!