PAID JOB for PROGRAMMER

Sorry, I had posted up so much code I thought I was starting to annoy everyone :slight_smile:

The code is a mish-mash of other peoples code, I haven't written it, I've just added to and altered.

Currently, everything works fine, the pong game starts, press the button and the pattern displays, but that's where to goodness ends.

I need to be able to press the button a 2nd time to go back to pong, press again, back to pattern.

On the few times I've managed to get it to switch back and forth, the pong game messes up and the pattern loops just once and returns to pong by itself.

I've almost given up to be honest. I will just tell my mum to switch off the power to reset back to pong and then to press the button to see the pattern but it would be preferred if the button would switch back and forth between the two displays.

int cols[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 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);
  //digitalWrite(switchPin, HIGH); // after the pinMode statement
  pinMode(buzz, OUTPUT);
  randomSeed(analogRead(5));
  //Serial.begin(115200);
}

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();

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

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