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