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;
}