Thanks for the replies.
Here is a copy of all the code;
int cols[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;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean pongGame = false;
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);
digitalWrite(switchPin, HIGH); // after the pinMode statement
pinMode(buzz, OUTPUT);
randomSeed(analogRead(5));
//Serial.begin(115200);
}
boolean debounce(boolean last)
{
boolean current = digitalRead(switchPin);
if (last != current)
{
delay(5);
current = digitalRead(switchPin);
}
return current;
}
void registerWrite(int rows, int cols)
{
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, MSBFIRST, rows);
shiftOut(dataPin, clockPin, MSBFIRST, cols);
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], cols[ball_x]);
delay(3);
registerWrite(rows[8], cols[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], cols[0]);
delay(3);
registerWrite(rows[8], cols[8]);
delay(3);
registerWrite(rows[p1_pos], cols[7]);
delay(3);
registerWrite(rows[8], cols[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, cols[count]);
shiftOut(dataPin, clockPin, LSBFIRST, rows[count]);
digitalWrite(latchPin, HIGH);
}
}
}
}
void loop()
{
//Serial.print(currState != prevState);
//Serial.println();
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH)
{
pattern();
}
else
{
pong();
}
lastButton = currentButton;
}
Im not a coder, I find this really hard to get my head round. Thanks everyone for the help and advice.