Automatic Variable Change?

So, I am trying to make Space Invader(whatever) with Arduinos.

I am using a 8x8 Matrix display, passive buzzer and a joystick.

But I have a problem.

That is, that I have a variable pos.

This variable is the players x position.

When I am shooting a bullet, the pos variable magically is set to 0.

Why do I not understand it?

Because no where in my code, is there anything that sets pos to 0 or similar.

For some reason, if I remove these lines of code:

  if (bulletYs[i] == 0 && bulletXs[i] != 0) {
    Serial.println("BAD BULLET BOY!");
    bulletYs[i] = 0;
    bulletXs[i] = 0;

  }

It magically works?

Here is my full code:

#include <LedControl.h>

#define SpeakerPin 48

//defining global variables
int pos = 3;

int bulletXs[100] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int bulletYs[100] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int index = 0;
int amountOfBullets = 0;


LedControl lc1 = LedControl(12, 10, 11, 1);

//telling C++ that we are going to make these functions
void drawPlayer(int midX, int midY);
void ComputeInput();
void handleBullets();
void drawBullets();



void setup() {
  //wake up the MAX72XX from power-saving mode
  lc1.shutdown(0, false);
  //set a medium brightness for the Leds
  lc1.setIntensity(0, 8);

  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(SpeakerPin, OUTPUT);

  Serial.begin(9600);


}

void loop() {
  lc1.clearDisplay(0);



  //lc1.setColumn(0,0,255);

  Serial.println(pos);


  ComputeInput();
  handleBullets();
  drawBullets();

  drawPlayer(pos, 7);
  Serial.println("Drawing the player");

  delay(200);

}

void drawPlayer(int midX, int midY) {
  lc1.setLed(0, midX + 1, midY, true);
  lc1.setLed(0, midX, midY, true);
  lc1.setLed(0, midX - 1, midY, true);
  lc1.setLed(0, midX, midY - 1, true);
}

void ComputeInput() {
  if (analogRead(A2) >= 300) {
    pos++;
    Serial.println("Changing pos because the player is moving right");
    tone(8, 400, 200);
  }

  else if (analogRead(A2) == 0) {
    pos--;
    Serial.println("Changing pos because the player is moving left");
    tone(8, 200, 200);
  }
  if (digitalRead(2) == 0) {
    Serial.println("Creating A bullet!");

    bulletXs[index] = pos;
    bulletYs[index] = 6;

    index++;
    amountOfBullets++;

    int startTime = millis();
    int startPitch = 1000;
    int tempPos = pos;
    Serial.print("Creating a tempPos:");
    Serial.println(tempPos);

    while (millis() - startTime <= 100) {
      drawPlayer(pos, 7);
      drawBullets();

      tone(8, startPitch, 10);

      startPitch = startPitch - 100;

      delay(10);
    }

    pos = tempPos;
    Serial.println("Setting Pos to tempPos");



  }
}

void handleBullets() {

  if (amountOfBullets == 10) {
    for (int i = 0; i <= 100; i++) {
      bulletXs[i] = -1;
      bulletYs[i] = 0;

    }

  }
  for (int i = 0; i <= 100; i++) {
    if (bulletYs[i] != 0) {
      bulletYs[i]--;
      tone(8, 1000, 50);

      Serial.println(bulletYs[i]);

      if (bulletYs[i] == 0 && bulletXs[i] != 0) {
        Serial.println("BAD BULLET BOY!");
        bulletYs[i] = 0;
        bulletXs[i] = 0;

      }
    }




  }



}



void drawBullets() {
  for (int i = 0; i <= 100; i++) {
    if (bulletXs[i] != -1) {
      lc1.setLed(0, bulletXs[i], bulletYs[i], true);
    }

  }


}

(Sorry for the fact that it looks terrible, I didn't bother commenting that much)

Hello,

Your problem is on these lines for (int i = 0; i <= 100; i++), array indices are in range 0 to size-1 (99 in your case), so i must not be above 99. So change these lines to for (int i = 0; i < 100; i++)

And, make sure index++; will never be above 99 or you will have similar issues (buffer overflow)

Thank you soo much for the quick reply!

I am glad to say that it mostly works

Now, the same will happen, but randomly.

EDIT: I just realised that the same thing does not happen, but the entire game seems to be resetting.

I don't think that it is because of:

if (index < 99){
    index++;
    }
    else{
      index = 0;
      }

Because I added a debug message to when the else statement happens, and it does not print.

Here is my updated code:

#include <LedControl.h>

#define SpeakerPin 48

//defining global variables
int pos = 3;

int bulletXs[100] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
int bulletYs[100] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int index = 0;
int amountOfBullets = 0;




LedControl lc1 = LedControl(12, 10, 11, 1);

//telling C++ that we are going to make these functions
void drawPlayer(int midX, int midY);
void ComputeInput();
void handleBullets();
void drawBullets();



void setup() {
  //wake up the MAX72XX from power-saving mode
  lc1.shutdown(0, false);
  //set a medium brightness for the Leds
  lc1.setIntensity(0, 8);

  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(SpeakerPin, OUTPUT);

  Serial.begin(9600);


}

void loop() {
  lc1.clearDisplay(0);



  //lc1.setColumn(0,0,255);

  Serial.println(pos);


  ComputeInput();
  handleBullets();
  drawBullets();

  drawPlayer(pos, 7);
  Serial.println("Drawing the player");

  delay(200);

}

void drawPlayer(int midX, int midY) {
  lc1.setLed(0, midX + 1, midY, true);
  lc1.setLed(0, midX, midY, true);
  lc1.setLed(0, midX - 1, midY, true);
  lc1.setLed(0, midX, midY - 1, true);
}

void ComputeInput() {
  if (analogRead(A2) >= 300) {
    pos++;
    Serial.println("Changing pos because the player is moving right");
    tone(8, 400, 200);
  }

  else if (analogRead(A2) == 0) {
    pos--;
    Serial.println("Changing pos because the player is moving left");
    tone(8, 200, 200);
  }
  if (digitalRead(2) == 0) {
    Serial.println("Creating A bullet!");

    bulletXs[index] = pos;
    bulletYs[index] = 6;

    if (index < 99){
    index++;
    }
    else{
      index = 0;
      }
    amountOfBullets++;

    int startTime = millis();
    int startPitch = 1000;
    int tempPos = pos;
    Serial.print("Creating a tempPos:");
    Serial.println(tempPos);

    while (millis() - startTime <= 100) {
      drawPlayer(pos, 7);
      drawBullets();

      tone(8, startPitch, 10);

      startPitch = startPitch - 100;

      delay(10);
    }

    pos = tempPos;
    Serial.println("Setting Pos to tempPos");



  }
}

void handleBullets() {

  if (amountOfBullets == 10) {
    for (int i = 0; i <= 100; i++) {
      bulletXs[i] = -1;
      bulletYs[i] = 0;

    }

  }
  for (int i = 0; i < 100; i++) {
    if (bulletYs[i] != 0) {
      bulletYs[i]--;
      tone(8, 1000, 50);

      Serial.println(bulletYs[i]);

      if (bulletYs[i] == 0 && bulletXs[i] != 0) {
        Serial.println("BAD BULLET BOY!");
        bulletYs[i] = 0;
        bulletXs[i] = 0;

      }
    }




  }



}



void drawBullets() {
  for (int i = 0; i <= 100; i++) {
    if (bulletXs[i] != -1) {
      lc1.setLed(0, bulletXs[i], bulletYs[i], true);
    }

  }


}

and PS. I have 2 more issues. 1 where there is a sort of ghost bullet, that follows the player, and every time you shoot, it comes down by 1 pixel.

  1. is that the first bullet you shoot, does not despawn, instead it stays at 0, 0.

But thank you for all of the help!

You still have 2 more lines for (int i = 0; i <= 100; i++) which need to be fixed as well

Thanks a lot, it works now, AND the ghost bullet is gone, though the 2nd other issue remains where the first bullet you shoot, when it gets to the top, it stays at 0, 0, while the others despawn

Thanks - sdsarduino

Despawn? You mean dissapear? Are you sure they dissapear, or are they simply all drawn on top of that first bullet? Looks too me, from reading your code, that's what will happen.

no, they will despawn because of:

if (bulletYs[i] == 0 && bulletXs[i] != 0) {
        Serial.println("BAD BULLET BOY!");
        bulletYs[i] = 0;
        bulletXs[i] = 0;

      }

Which basically removes them from the list and all their data, so despawn(well, at least, that is what is expected to happen)

Shouldn't that be:

if (bulletYs[i] == 0 && bulletXs[i] >= 0) {
        Serial.println("BAD BULLET BOY!");
        bulletYs[i] = 0;
        bulletXs[i] = -1;  // Mark this array element as empty
      }

yeah, thanks!

I just realized LOL.

Thanks again!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.