Hi,
Thanks Mark I appreciate the help. So I got a set a bluetooth pairing speakers for the music, and I mounted a mic in one of the speakers and got the reactive lights working on that. So I’ll have both speakers with a mic in them. Now I’m trying to get the scoreboard code to work. Right now I’m using a Mega2560 to piece this together and to get a better feel for what board I should use, and a 32x8 ws2812b matrix for the scoreboard display. I still need to add the reactive lights in this code, but thought I’d get the scoreboard side done first. Once I get this figured out I’ll get the recommended boards and connect them through HC-05 bluetooth models. And plan on putting together a phone/tablet app together on thunkable to control it with buttons on the boards too. If you can give me some help with the scoreboard code, it would be much appreciated. Thanks
Edited by moderator to add code tags
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_NeoMatrix.h>
#include <Arduino.h>
#include <Adafruit_NeoPixel.h>
#include "Button.h"
// Similar to F(), but for PROGMEM string pointers rather than literals
#define F2(progmem_ptr) (const __FlashStringHelper *)progmem_ptr
/////// Hardware setup ////////
// Physical Button Pin Setup
#define BUTTON_PIN_1 36
#define BUTTON_PIN_2 37
#define BUTTON_PIN_3 38
#define BUTTON_PIN_4 39
// LED Matrix Pin Setup
#define PIN 22
// MATRIX DECLARATION:
// Parameter 1 = width of NeoPixel matrix
// Parameter 2 = height of matrix
// Parameter 3 = pin number (most are valid)
// Parameter 4 = matrix layout flags, add together as needed:
// NEO_MATRIX_TOP, NEO_MATRIX_BOTTOM, NEO_MATRIX_LEFT, NEO_MATRIX_RIGHT:
// Position of the FIRST LED in the matrix; pick two, e.g.
// NEO_MATRIX_TOP + NEO_MATRIX_LEFT for the top-left corner.
// NEO_MATRIX_ROWS, NEO_MATRIX_COLUMNS: LEDs are arranged in horizontal
// rows or in vertical columns, respectively; pick one or the other.
// NEO_MATRIX_PROGRESSIVE, NEO_MATRIX_ZIGZAG: all rows/columns proceed
// in the same order, or alternate lines reverse direction; pick one.
// See example below for these values in action.
// Parameter 5 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 8, PIN,
NEO_MATRIX_BOTTOM + NEO_MATRIX_RIGHT +
NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
NEO_GRB + NEO_KHZ800);
//////// Program variables ////////
// last param indicates 'double buffering'
Button homeAddButton = Button(BUTTON_PIN_1);
Button awayAddButton = Button(BUTTON_PIN_2);
Button homeSubButton = Button(BUTTON_PIN_3);
Button awaySubButton = Button(BUTTON_PIN_4);
// variables will change:
int buttonState = 0; // varibles for reading the pushbutton status
const int WINNING_SCORE = 21;
int homeScore = 0;
int awayScore = 0;
// Last scored var and constants
const int NO_SCORE = 0;
const int HOME_SCORED = 1;
const int AWAY_SCORED = 2;
const int lastScored = NO_SCORE;
int hue = 0;
/****************************************
* Program Setup
****************************************/
void setup() {
while (!Serial);
delay(500);
Serial.begin(19200);
//initalize push button pins as input:
pinMode(BUTTON_PIN_1, INPUT);
pinMode(BUTTON_PIN_2, INPUT);
pinMode(BUTTON_PIN_3, INPUT);
pinMode(BUTTON_PIN_4, INPUT);
// initialize display
matrix.begin();
matrix.setBrightness(10);
matrix.print("#TimeToPlay");
serialPrintScores();
}
/****************************************
* Main Program Loop
****************************************/
void loop() {
int currentHomeScore = homeScore;
int currentAwayScore = awayScore;
// read the state of the pushbutton value:
buttonState = digitalRead(BUTTON_PIN_1);
buttonState = digitalRead(BUTTON_PIN_2);
buttonState = digitalRead(BUTTON_PIN_3);
buttonState = digitalRead(BUTTON_PIN_4);
// check if pushbuttons are pressed. If it is, the buttonState is HIGH:
if (buttonState ==HIGH) {
homeScore = increaseScore(homeScore);
lastScored = HOME_SCORED;
}
else if (buttonState == HIGH) {
homeScore = decreaseScore(homeScore);
} else if (buttonState == HIGH) {
awayScore = increaseScore(awayScore);
lastScored = AWAY_SCORED; {
} else if (buttonState == HIGH) {
awayScore = decreaseScore(awayScore);
}
else if (buttonState == HIGH, button3State == HIGH) {
resetScores = NO_SCORE;
Serial.println("Reset scores");
}
if (currentHomeScore != homeScore || currentAwayScore != awayScore) {
serialPrintScores();
if (gameOver()) {
Serial.println("Winner, winner, chicken dinner!");
}
}
displayScoreBoardScreen();
matrix.swapBuffers(false);
}
/****************************************
* Scoring
****************************************/
int increaseScore(int score) {
if (score == 21) {
return 13;
}
return score + 1;
}
int decreaseScore(int score) {
if (score == 0) {
return 0;
}
return score - 1;
}
int resetScores() {
homeScore = 0;
awayScore = 0;
}
bool gameOver() {
return homeScore == WINNING_SCORE || awayScore == WINNING_SCORE;
}
bool scoreResetPressed() {
return homeSubButton.isHeld() && awaySubButton.isHeld();
}
void serialPrintScores() {
Serial.println("Current scores: " + displayableScore(homeScore) + " v " + displayableScore(awayScore));
}
/****************************************
* Display Related
****************************************/
void displayScoreBoardScreen() {
matrix.setTextSize(1);
// fill the screen with 'black'
clearDisplay();
if (gameOver()) {
drawGameOverBorder();
}
displayScores();
}
void drawGameOverBorder() {
matrix.drawRect(0, 0, 32, 16, matrix.ColorHSV(hue, 255, 255, true));
hue += 7;
if(hue >= 1536) hue -= 1536;
}
void displayScores() {
putDigitLarge(1, 2, '0' + homeScore / 10, 7, 0, 0);
putDigitLarge(7, 2, '0' + homeScore % 10, 7, 0, 0);
uint16_t indicatorColor = matrix.Color333(7,6,0);
if (lastScored == NO_SCORE) {
matrix.setCursor(14, 4);
matrix.setTextColor(indicatorColor);
matrix.print('v');
} else if (lastScored == HOME_SCORED) {
// Draw a < pointing towards home score
matrix.drawLine(14, 7, 17, 4, indicatorColor);
matrix.drawLine(14, 8, 17, 5, indicatorColor);
matrix.drawLine(14, 8, 17, 11, indicatorColor);
matrix.drawLine(14, 7, 17, 10, indicatorColor);
} else if (lastScored == AWAY_SCORED) {
// Draw a > pointing towards away score
matrix.drawLine(15, 4, 18, 7, indicatorColor);
matrix.drawLine(15, 5, 18, 8, indicatorColor);
matrix.drawLine(15, 11, 18, 8, indicatorColor);
matrix.drawLine(15, 10, 18, 7, indicatorColor);
}
putDigitLarge(19, 2, '0' + awayScore / 10, 0, 7, 0);
putDigitLarge(25, 2, '0' + awayScore % 10, 0, 7, 0);
}
String displayableScore(int score) {
String scoreString = String(score);
if (scoreString.length() == 1) {
scoreString = "0" + scoreString;
}
return scoreString;
}
void displayText(String text) {
matrix.setTextWrap(false); // Allow text to run off right edge
matrix.setTextSize(2); // size 1 == 8 pixels high
int textX = matrix.width();
int textMin = text.length() * -12;
while ((--textX) >= textMin) {
//Serial.println("TextX: " + textX);
matrix.fillScreen(0);
matrix.setTextColor(matrix.ColorHSV(hue, 255, 255, true));
matrix.setCursor(textX, 1);
matrix.print(text);
hue += 7;
if(hue >= 1536) hue -= 1536;
matrix.swapBuffers(true);
delay(10);
}
}
void clearDisplay() {
matrix.fillScreen(0);
}
void updateButtons()
{
homeAddButton.update();
awayAddButton.update();
homeSubButton.update();
awaySubButton.update();
}
void putDigitLarge(uint8_t x, uint8_t y, char c, uint8_t r, uint8_t g, uint8_t b)
{
// fonts defined for ascii 32 and beyond (index 0 in font array is ascii 32);
byte charIndex = c - '0' + 22;
putChar(x, y, bigFont[charIndex], 12, 6, r, g, b);
}
void putChar(uint8_t x, uint8_t y, const unsigned char * c, uint8_t h, uint8_t w, uint8_t r, uint8_t g, uint8_t b) {
for (byte row = 0; row<h; row++)
{
byte rowDots = pgm_read_byte_near(&c[row]);
for (byte col = 0; col<w; col++)
{
if (rowDots & (1 << (w - col - 1)))
matrix.drawPixel(x + col, y + row, matrix.Color333(r,g,b));
else
matrix.drawPixel(x+col, y+row, matrix.Color333(0,0,0));
}
}
}