Code - Help Required - Xmas Display Pong Game

I’ve been working on a project for some weeks now and have just one last issue to resolve. Im quite good with the hardware but not too good with coding.
The code below is working fine on my display and the pong game works well. I need to add some commands to this code to get the follow features, please could someone help me with the code alterations?

1, I want to have a pattern displayed when not playing the game.
2, Once someone presses a button (makes a pin go HIGH briefly) I want the game to start.
3, After each game has ended, in the “resetAnim” I’d like to be able to display a pattern (different from N0.1 pattern)
4, If neither paddle is moved for 5 minutes I want it to automatically return to showing the pattern display (No.1).
5, I’d like to be able to choose the size of the paddles. Currently each paddle is just one LED, I’d like to choose 2 or 3 LEDS (making the games easier)

This project is a present for Christmas and I'm urgently needing to get the code completed. Any help would be gratefully received.
All the best,
Phillip

int colsR[9] = {1,2,4,8,16,32,64,128, 0};
int rows[9] = {1,2,4,8,16,32,64,128, 255};

const int latchPin = 10;
const int clockPin = 9;
const int dataPin = 11;

int player_one = A1; //red-left
int player_two = A0; //blue-right

int p1_pos;
int p2_pos;
 
int ball_x=3, ball_y=3;
int ball_dir_x=1, ball_dir_y=-1;
int count = 0;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);    
  pinMode(clockPin, OUTPUT);  
  randomSeed(analogRead(5));
}

void registerWrite(int row,  int col) {
  digitalWrite(latchPin, 0);
  shiftOut(dataPin, clockPin, MSBFIRST, row);
  shiftOut(dataPin, clockPin, MSBFIRST, col);
  digitalWrite(latchPin, 1);
}

void resetAnim()
{
delay(250);
}

void resetBall()
{
  resetAnim();
  
  ball_x=3; ball_y = 4;
  ball_dir_x = 1; ball_dir_y = -1;
}

void getPlayerPositions()
{
  p1_pos = analogRead(player_one);
  p1_pos = p1_pos/128;
  p2_pos = analogRead(player_two);
  p2_pos = p2_pos/128;  
}

void renderBall()
{
  registerWrite(rows[ball_y], colsR[ball_x]);
  delay(3);
  registerWrite(rows[8], colsR[8]);
  delay(3);  
}  

void moveBall()
{
  ball_x += ball_dir_x;
  ball_y += ball_dir_y;
}

void checkLocationAndBounce()
{
  // bounce on y
  if(ball_y >= 7)
    ball_dir_y= -1;
  else if(ball_y <= 0)
    ball_dir_y = 1;

  // on x only bounce if player
  // paddle is on same spot
  if(ball_x == 7 && ball_y == p1_pos)
  {
    randomBounceBack();
    ball_dir_x= -1;
  }
  else if(ball_x == 7)
  {
      resetBall();
   }
  else if(ball_x == 0 && ball_y == p2_pos) {
    randomBounceBack();
    ball_dir_x = 1;   
  }  else if(ball_x == 0)
  {
     resetBall();
  }
}

void randomBounceBack()
{
  // set a random Y direction when we
  // bounce on a paddle
  
  int y_dir = random(3);
  switch(y_dir)
  {
    case 0:
      ball_dir_y = -1;
      break;
      case 1:
      ball_dir_y = 0;
      break;
      case 2:
      ball_dir_y = 1;
      break;
  }  
}

void renderPlayerPaddles()
{
  getPlayerPositions();
  registerWrite(rows[p2_pos], colsR[0]);
  delay(3);
  registerWrite(rows[8], colsR[8]);
  delay(3);
  registerWrite(rows[p1_pos], colsR[7]);  
  delay(3);
  registerWrite(rows[8], colsR[8]);
  delay(3);
}

void loop() {  
  
  renderBall();
  renderPlayerPaddles();
  if(count >10) {  
    moveBall();
    checkLocationAndBounce();
    count = 0;
  }
  count++; 
}

Sorry,

This is an 8x8 LED Matrix running through two 595 compatible shift registers.

Many Thanks

void loop()
{  
  renderBall();
  renderPlayerPaddles();
  if(count >10)
  {
    moveBall();
    checkLocationAndBounce();
    count = 0;
  }
  count++; 
}

Why is it necessary to redraw the ball and paddle on each pass through loop? How long does it take to do that? That defines how long between ball updates.

Connecting switches and buzzers is easy, if you have pins available. Making the buzzer sound at the end of the games is trivial, if the games has a defined end. I don't see that happening in loop.

Determining the last time a player moved a paddle is easy. The blink without delay example shows how to record the time, and how to check how much time has elapsed since that (or the start of a game) has happened.

Displaying a different pattern before the game starts is easy, if you have a pattern in mind. Displaying a different pattern while the game is not actively being played is easy, if you have a pattern in mind.

PaulS:
Why is it necessary to redraw the ball and paddle on each pass through loop?

I didnt write this code, I have just altered parts to fit my design. I dont really understand these commands.

PaulS:
Connecting switches and buzzers is easy,

Got this working already. many thanks

PaulS:
"is easy"

It's only easy when you know how. The code may as well be written in Spanish, I still cant get my head round it.

I'm almost there for the button push. Ive taken the pong game void loop and renamed it void pong. I have then made a void pattern.
In my new void loop, I have this code but cant get it to switch properly. Still trying.....

void loop(){

  if (digitalRead(switchPin) == LOW)
  {
    pong();
  }
else{
  pattern();
}
  }

My code as of 9pm tonight :~
Please help me tidy this up....

int colsR[9] = {1,2,4,8,16,32,64,128, 0};
int rows[9] = {1,2,4,8,16,32,64,128, 0};

const int latchPin = 10;
const int clockPin = 9;
const int dataPin = 11;
int bleep = 5;
int switchPin = 6;
int buzz = 3;

boolean lastButton = LOW;
boolean currentButton = LOW;
int player_one = A1; //red-left
int player_two = A0; //blue-right
int p1_pos, p2_pos;
 
int ball_x=3, ball_y=3;
int ball_dir_x=1, ball_dir_y=-1;
int count = 0;

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);    
  pinMode(clockPin, OUTPUT); 
  pinMode(bleep, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(buzz, OUTPUT);
  randomSeed(analogRead(5));
}


boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
   return current;
}




void registerWrite(int row,  int col) {
  digitalWrite(latchPin, 0);
  shiftOut(dataPin, clockPin, MSBFIRST, row);
  shiftOut(dataPin, clockPin, MSBFIRST, col);
  digitalWrite(latchPin, 1);
}



void resetAnim()
{
  digitalWrite(buzz, HIGH);
  delay(150);
  digitalWrite(buzz, LOW);
  delay(20);
  digitalWrite(buzz, HIGH);
  delay(150);
  digitalWrite(buzz, LOW);
  delay(20);
  digitalWrite(buzz, HIGH);
  delay(150);
  digitalWrite(buzz, LOW);
}

void resetBall()
{
  resetAnim();
  
  ball_x=3; ball_y = 4;
  ball_dir_x = 1; ball_dir_y = -1;
}

void getPlayerPositions()
{
  p1_pos = analogRead(player_one);
  p1_pos = p1_pos/128;
  p2_pos = analogRead(player_two);
  p2_pos = p2_pos/128;  
}

void renderBall()
{
  registerWrite(rows[ball_y], colsR[ball_x]);
  delay(3);
  registerWrite(rows[8], colsR[8]);
  delay(3);  
}  

void moveBall()
{
  digitalWrite(bleep, HIGH);
  delay(2);
  digitalWrite(bleep, LOW);
  ball_x += ball_dir_x;
  ball_y += ball_dir_y;
  
}

void checkLocationAndBounce()
{
  // bounce on y
  if(ball_y >= 7)
    ball_dir_y= -1;
  else if(ball_y <= 0)
    ball_dir_y = 1;

  // on x only bounce if player
  // paddle is on same spot
  if(ball_x == 7 && ball_y == p1_pos)
  {
    randomBounceBack();
    ball_dir_x= -1;
  }
  else if(ball_x == 7)
  {
      resetBall();
   }
  else if(ball_x == 0 && ball_y == p2_pos) {
    randomBounceBack();
    ball_dir_x = 1;   
  }  else if(ball_x == 0)
  {
     resetBall();
  }
}

void randomBounceBack()
{
  // set a random Y direction when we
  // bounce on a paddle
  
  int y_dir = random(3);
  switch(y_dir)
  {
    case 0:
      ball_dir_y = -1;
      break;
      case 1:
      ball_dir_y = 0;
      break;
      case 2:
      ball_dir_y = 1;
      break;
  }  
}

void renderPlayerPaddles()
{
  getPlayerPositions();
  registerWrite(rows[p2_pos], colsR[0]);
  delay(3);
  registerWrite(rows[8], colsR[8]);
  delay(3);
  registerWrite(rows[p1_pos], colsR[7]);  
  delay(3);
  registerWrite(rows[8], colsR[8]);
  delay(3);
}

void pong() {  
  
  renderBall();
  renderPlayerPaddles();
  if(count >10) {  
    moveBall();
    checkLocationAndBounce();
    count = 0;
  }
  count++; 
}



void pattern() {
for (int character = 0; character < 8; character ++){
switch (character) {

case 0:
rows[7] = 0b10100011;
rows[6] = 0b10001001;
rows[5] = 0b10010001;
rows[4] = 0b11000101;
rows[3] = 0b10001011;
rows[2] = 0b10100001;
rows[1] = 0b11001001;
rows[0] = 0b10000001;
break;

case 1:
rows[0] = 0b10100011;
rows[7] = 0b10001001;
rows[6] = 0b10010001;
rows[5] = 0b11000101;
rows[4] = 0b10001011;
rows[3] = 0b10100001;
rows[2] = 0b11001001;
rows[1] = 0b10000001;
break;

case 2:
rows[1] = 0b10100011;
rows[0] = 0b10001001;
rows[7] = 0b10010001;
rows[6] = 0b11000101;
rows[5] = 0b10001011;
rows[4] = 0b10100001;
rows[3] = 0b11001001;
rows[2] = 0b10000001;
break;

case 3:
rows[2] = 0b10100011;
rows[1] = 0b10001001;
rows[0] = 0b10010001;
rows[7] = 0b11000101;
rows[6] = 0b10001011;
rows[5] = 0b10100001;
rows[4] = 0b11001001;
rows[3] = 0b10000001;
break;

case 4:
rows[3] = 0b10100011;
rows[2] = 0b10001001;
rows[1] = 0b10010001;
rows[0] = 0b11000101;
rows[7] = 0b10001011;
rows[6] = 0b10100001;
rows[5] = 0b11001001;
rows[4] = 0b10000001;
break;

case 5:
rows[4] = 0b10100011;
rows[3] = 0b10001001;
rows[2] = 0b10010001;
rows[1] = 0b11000101;
rows[0] = 0b10001011;
rows[7] = 0b10100001;
rows[6] = 0b11001001;
rows[5] = 0b10000001;
break;

case 6:
rows[5] = 0b10100011;
rows[4] = 0b10001001;
rows[3] = 0b10010001;
rows[2] = 0b11000101;
rows[1] = 0b10001011;
rows[0] = 0b10100001;
rows[7] = 0b11001001;
rows[6] = 0b10000001;
break;

case 7:
rows[6] = 0b10100011;
rows[5] = 0b10001001;
rows[4] = 0b10010001;
rows[3] = 0b11000101;
rows[2] = 0b10001011;
rows[1] = 0b10100001;
rows[0] = 0b11001001;
rows[7] = 0b10000001;
break;


}
for (int refreshed = 0; refreshed < 200; refreshed++){
for (int count = 0; count < 8; count ++){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, colsR[count]);
shiftOut(dataPin, clockPin, LSBFIRST, rows[count]);
//shiftOut(dataPin, clockPin, MSBFIRST, column[count]);

digitalWrite(latchPin, HIGH);
}
}

}

}



void loop(){

  if (digitalRead(switchPin) == LOW)
  {
    pong();
  }
else{
  pattern();
}
  }

It isn't a tidyup, but it'll make things a little quicker, hopefully

const byte colsR[9] = {1,2,4,8,16,32,64,128, 0};
const byte rows[9] = {1,2,4,8,16,32,64,128, 0};

The byte is an improvement, but 'rows' can't be declared const as it is changed by 'void pattern()'

I'd recommend that you put each { on a new line, and use Tools + Auto format to fix the indenting. The code jumps all over the place, making it hard to read. Consistent indenting makes the intent of the program clearer, too.

You will need to hold the switch down to play the game. That might get a little inconvenient, unless it is a toggle switch. If it is not, reading the current state, and comparing it to the previous state, and doing something, like starting the game, only when the states are not the same (and the current state is the desired state).

int currState;
int prevState = HIGH;

void loop()
{
   currState = digitalRead(switchPin);
   if(currState != prevState)
   {
      if(currState == LOW)
      {
         pong();
      }
   }
   prevState = currState;
}

To call pattern(), instead, requires determining when pattern() should be called. That's where millis() and keeping track of activities in the game come in to play. If pattern() is to be called until the game starts, you'll need a boolean, playing, initialized to false, and set to true when the game starts. Then,

if(!playing)
pattern();

can be put after the assignment to prevState, to show the pre-game pattern.

Thanks for the messages so far. Some bits have made sense but I'm still banging my head off a wall.

What I have:
I have an 8x8 Matrix driven by two shift registers which pull down GND. The first shift registers drains are connected to PNP transistors and control the + side which are the columns.
I then have a push button (momentary button) grounded via a 10k resistor to and goes HIGH when push down, connected to pin 6 of the arduino.
I have two types of buzzers to get different tones. One is used for ball movement and the other for "end of game" tones.

All I want to do is
To have the pattern being displayed by default.
Then if someone presses the button (HIGH briefly) that the Pong game will start playing.
If after a period of time (5 min for example) the POTs remain still (allow for some voltage fluctuations) the display returns to the pattern routine.

For someone who knows Arduino code I'm sure this isn't that hard, however, I don't know how to code, its taken 6 weeks of stress to get this far and this final stage is driving me mad.

Would anyone please have it in the kindness of their heart to rewrite/repair this code to get this finished?

I know its better for me to learn, and I am honestly, but Xmas is next week and I simply MUST have this finished in the next day or two.

I don't care WHAT the pattern is for now, I'm happy to create this like in the code, all its doing right now is lighting up columns 1&8 and then a snow flake falling affect of the inner 6 columns. I know this could be coded better too.

My 8x8 is decorated with RED Xmas hats down the left, blue Xmas hats down the right and white snowflake shaped led layouts for all other areas and contains over 620 LED's! The hardware is fine, looks great, it just needs the coding.

Pictures, videos, schematics and code for others to follow will be made available over Xmas as I've found very few useful tutorials on this.

PLEASE help a man in distress.

This is what I have right now, and it plays the pattern, nothing happens when button is pushed.

int colsR[9] = {
  1,2,4,8,16,32,64,128, 0};
int rows[9] = {
  1,2,4,8,16,32,64,128, 0};

const int latchPin = 10;
const int clockPin = 9;
const int dataPin = 11;

int buzz = 3;
int bleep = 5;
int switchPin = 6;

int currState;
int prevState = HIGH;

int player_one = A1; //red-left
int player_two = A0; //blue-right
int p1_pos, p2_pos;

int ball_x=3, ball_y=3;
int ball_dir_x=1, ball_dir_y=-1;
int count = 0;

boolean playing = false;

void setup() 
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);    
  pinMode(clockPin, OUTPUT); 
  pinMode(bleep, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(buzz, OUTPUT);
  randomSeed(analogRead(5));
}

void registerWrite(int row,  int col)
{
  digitalWrite(latchPin, 0);
  shiftOut(dataPin, clockPin, MSBFIRST, row);
  shiftOut(dataPin, clockPin, MSBFIRST, col);
  digitalWrite(latchPin, 1);
}

void resetAnim()
{
  digitalWrite(buzz, HIGH);
  delay(150);
  digitalWrite(buzz, LOW);
  delay(20);
  digitalWrite(buzz, HIGH);
  delay(150);
  digitalWrite(buzz, LOW);
  delay(20);
  digitalWrite(buzz, HIGH);
  delay(150);
  digitalWrite(buzz, LOW);
}

void resetBall()
{
  resetAnim();

  ball_x=3; 
  ball_y = 4;
  ball_dir_x = 1; 
  ball_dir_y = -1;
}

void getPlayerPositions()
{
  p1_pos = analogRead(player_one);
  p1_pos = p1_pos/128;
  p2_pos = analogRead(player_two);
  p2_pos = p2_pos/128;  
}

void renderBall()
{
  registerWrite(rows[ball_y], colsR[ball_x]);
  delay(3);
  registerWrite(rows[8], colsR[8]);
  delay(3);  
}  

void moveBall()
{
  digitalWrite(bleep, HIGH);
  delay(2);
  digitalWrite(bleep, LOW);
  ball_x += ball_dir_x;
  ball_y += ball_dir_y;

}

void checkLocationAndBounce()
{
  // bounce on y
  if(ball_y >= 7)
    ball_dir_y= -1;
  else if(ball_y <= 0)
    ball_dir_y = 1;

  // on x only bounce if player
  // paddle is on same spot
  if(ball_x == 7 && ball_y == p1_pos)
  {
    randomBounceBack();
    ball_dir_x= -1;
  }
  else if(ball_x == 7)
  {
    resetBall();
  }
  else if(ball_x == 0 && ball_y == p2_pos) {
    randomBounceBack();
    ball_dir_x = 1;   
  }  
  else if(ball_x == 0)
  {
    resetBall();
  }
}

void randomBounceBack()
{
  // set a random Y direction when we
  // bounce on a paddle
  int y_dir = random(3);
  switch(y_dir)
  {
  case 0:
    ball_dir_y = -1;
    break;
  case 1:
    ball_dir_y = 0;
    break;
  case 2:
    ball_dir_y = 1;
    break;
  }  
}

void renderPlayerPaddles()
{
  getPlayerPositions();
  registerWrite(rows[p2_pos], colsR[0]);
  delay(3);
  registerWrite(rows[8], colsR[8]);
  delay(3);
  registerWrite(rows[p1_pos], colsR[7]);  
  delay(3);
  registerWrite(rows[8], colsR[8]);
  delay(3);
}

void pong()
{  
  renderBall();
  renderPlayerPaddles();
  if(count >10)
  {  
    moveBall();
    checkLocationAndBounce();
    count = 0;
  }
  count++; 
}

void pattern()
{
  for (int character = 0; character < 8; character ++)
  {
    switch (character)
    {
    case 0:
      rows[7] = 0b10100011;
      rows[6] = 0b10001001;
      rows[5] = 0b10010001;
      rows[4] = 0b11000101;
      rows[3] = 0b10001011;
      rows[2] = 0b10100001;
      rows[1] = 0b11001001;
      rows[0] = 0b10000001;
      break;

    case 1:
      rows[0] = 0b10100011;
      rows[7] = 0b10001001;
      rows[6] = 0b10010001;
      rows[5] = 0b11000101;
      rows[4] = 0b10001011;
      rows[3] = 0b10100001;
      rows[2] = 0b11001001;
      rows[1] = 0b10000001;
      break;

    case 2:
      rows[1] = 0b10100011;
      rows[0] = 0b10001001;
      rows[7] = 0b10010001;
      rows[6] = 0b11000101;
      rows[5] = 0b10001011;
      rows[4] = 0b10100001;
      rows[3] = 0b11001001;
      rows[2] = 0b10000001;
      break;

    case 3:
      rows[2] = 0b10100011;
      rows[1] = 0b10001001;
      rows[0] = 0b10010001;
      rows[7] = 0b11000101;
      rows[6] = 0b10001011;
      rows[5] = 0b10100001;
      rows[4] = 0b11001001;
      rows[3] = 0b10000001;
      break;

    case 4:
      rows[3] = 0b10100011;
      rows[2] = 0b10001001;
      rows[1] = 0b10010001;
      rows[0] = 0b11000101;
      rows[7] = 0b10001011;
      rows[6] = 0b10100001;
      rows[5] = 0b11001001;
      rows[4] = 0b10000001;
      break;

    case 5:
      rows[4] = 0b10100011;
      rows[3] = 0b10001001;
      rows[2] = 0b10010001;
      rows[1] = 0b11000101;
      rows[0] = 0b10001011;
      rows[7] = 0b10100001;
      rows[6] = 0b11001001;
      rows[5] = 0b10000001;
      break;

    case 6:
      rows[5] = 0b10100011;
      rows[4] = 0b10001001;
      rows[3] = 0b10010001;
      rows[2] = 0b11000101;
      rows[1] = 0b10001011;
      rows[0] = 0b10100001;
      rows[7] = 0b11001001;
      rows[6] = 0b10000001;
      break;

    case 7:
      rows[6] = 0b10100011;
      rows[5] = 0b10001001;
      rows[4] = 0b10010001;
      rows[3] = 0b11000101;
      rows[2] = 0b10001011;
      rows[1] = 0b10100001;
      rows[0] = 0b11001001;
      rows[7] = 0b10000001;
      break;
    }

    for (int refreshed = 0; refreshed < 200; refreshed++)
    {
      for (int count = 0; count < 8; count ++)
      {
        digitalWrite(latchPin, LOW);
        shiftOut(dataPin, clockPin, MSBFIRST, colsR[count]);
        shiftOut(dataPin, clockPin, LSBFIRST, rows[count]);
        digitalWrite(latchPin, HIGH);
      }
    }
  }
}


void loop()
{

  currState = digitalRead(switchPin);
  if(currState != prevState)
  {
    if(currState == LOW)
    {
      pong();
    }
  }
  prevState = currState;

  if(!playing)
    pattern();
}

This is what I have right now, and it plays the pattern, nothing happens when button is pushed.

The Serial class is your friend. Add a call to Serial.begin() in setup(). Add calls to Serial.print() and Serial.println() in loop(). Does the switch get read correctly? What is the value of currState?

Inside the block where pong() is called, you need to set playing to true.

Thanks for helping :slight_smile:
Ive added those 3 bits of code like this:

void setup()


{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);    
  pinMode(clockPin, OUTPUT); 
  pinMode(bleep, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(buzz, OUTPUT);
  randomSeed(analogRead(5));
  Serial.begin();
}

And this?

void loop()
{

  Serial.print();
  Serial.println();
  
  
  currState = digitalRead(switchPin);
  if(currState != prevState)
  {
    if(currState == LOW)
    {
      pong();
    }
  }
  prevState = currState;

  if(!playing)
    pattern();
}

I get error; no matching function for call to "HardwareSerial::begin()"

Sorry, I have the serial working.

void setup()
{
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);    
  pinMode(clockPin, OUTPUT); 
  pinMode(bleep, OUTPUT);
  pinMode(switchPin, INPUT);
  pinMode(buzz, OUTPUT);
  randomSeed(analogRead(5));
  Serial.begin(9600);
}
void loop()
{

  Serial.print(1000);
  Serial.println(1000);
  currState = digitalRead(switchPin);

This is the serial readout.

Ø´0?Ga4È´ ?10001000
10001000
10001000
10001000
10001000
10001000
10001000

The button was pressed a couple of times during this read out.

Serial.print(1000);
Serial.println(1000);

I would think that printing currState, after reading the pin, would be more useful.

Change the 9600 to 115200. The stone age end a long time ago.

Ok, never used serial before.

Yes currState goes high (1) when pressed, and back to low (0) when released.

Display doesnt change, just get "pattern" nothing changes when button pressed.

Yes currState goes high (1) when pressed, and back to low (0) when released.

So, starting the game when the switch goes LOW is not a good idea.

You can print other things, like playing. Does playing change appropriately? Does ping() get called? What happens when ping() returns? Or does it?

Please Paul, I'm not a coder, I trying my best but almost at the point of throwing a match to it.

Please could you just read through my code and rewrite the bits that are wrong.?

I will study your changes and learn every bit of it but I'm getting too stressed and worried it wont be ready in time and trying to learn every aspect of this code is just making the matter worse.

All the best,

Try the highlighted changes.

void loop()
{
currState = digitalRead(switchPin);
if(currState != prevState)
{
if(currState == HIGH)
{
playing = true;
}
}
prevState = currState;

if(!playing)
pattern();
else
pong();
}

Thanks very much for your time Paul.

Ive run a serial print on "playing" and nothing changes when I press the button. I just get the same number over and over.

The matrix displays the pattern, button does nothing. Sometimes it will switch over to pong and mess up.

Phil

How is the switch wired? I'm beginning to suspect that "incorrectly" is the answer.

You should have one side connected to the digital pin, and one side connected to ground, and the internal pullup resistor turned on.

digitalWrite(switchPin, HIGH); // after the pinMode statement

Then, HIGH means not pressed and LOW means pressed.

The button has a 10k pull down resistor on it keeping the pin low. When the button is pressed it goes high to the arduino.

I have a
boolean playing = false;
and mentioned in the loop, but i dont understand how its being controlled by the button?
I believe this may be an issue?
The button goes high when pressed and switchPin knows it goes high. How does "playing" get informed?