variable being affected by function with no reference to it

I've got a bug in which the value of an int array slot is being inadvertently changed to a value of 255. I've narrowed down the cause to a function call, which if commented out fixes the issue. The weird thing is, nothing in that function makes any reference to the affected array or calls any other functions that might do so (unless the fastLED library is doing something I can't see).

The array being affected is playerScores[1], and the function affecting it is waitingPlayerLed() called at line 135. Sorry, the code is too long to embed here; link instead:

I've included 2 serial monitor screenshot attachments of what it should look like versus what actually happens. When operating correctly, a player who holds their RFID tag to the reader is able to earn a point every second for as long as the round time hasn't expired (reflected in serial monitor). Then, it's the other players turn.

In the bug version though, you'll see that at the end of the round, when gameIsLive == 0, playerScore[1] now holds a value of 255. The only code in the entire script that should change any slot in playerScores[] is a simple playerScores[x]++, so I have no idea why it's jumping to 255.

Attach the code here.

As I said, it's too long to post under the character limits for a newbie forum user. And there's also a lame 5 minute delay between replies.

Reread the request:

evanmars:
Attach the code here.

Without even looking at your code, I'm 95% sure that you're over-writing an array boundary somewhere.

I've included 2 serial monitor screenshot attachments

You wasted a huge amount of resources posting pictures of text. Do not do that again. Post text AS TEXT!

If you had bothered to read the "Read this before posting..." threads, you would know that you will get NO help here unless you POST YOUR CODE.

Read the stickies BEFORE replying, and post your code properly.

And there's also a lame 5 minute delay between replies.

That "lame" 5 minute delay is to give you time to read the rules AND think before you get all defensive.

PaulS:
You wasted a huge amount of resources posting pictures of text. Do not do that again. Post text AS TEXT!

If you had bothered to read the "Read this before posting..." threads, you would know that you will get NO help here unless you POST YOUR CODE.

Read the stickies BEFORE replying, and post your code properly.

Homie, I did review the stickies. The screenshots are from the serial monitor, not my script. The post character limits don't allow me to fit the entire code. Do you want half of it now, and then the functions 5 minutes later? I pre-emptively apologized for not being able to do this from the beginning as a result of the restrictions imposed by the forum itself. I included the link to streamline the process as the next best option.

rooshio:
Homie, I did review the stickies.

Your posts show no evidence of that.

The post character limits don't allow me to fit the entire code.

You have 26 posts on here on the forum and you don't know the difference between posting code in-line and attaching it as a file?

Sorry, my understanding was that code was embedded using the tags. adding attachment:

highScoreGame1.3.ino (10.2 KB)

The screenshots are from the serial monitor, not my script.

Your script sent data to an application that only knows how to display text. You can cut-and-paste the text here.

The post character limits don't allow me to fit the entire code.

If you had read the rules, you'd have learned two things. One would be how to attach the code when it is more than 9500 characters. The other is the need to develop a minimal sketch to illustrate the problem. Often, in doing that, you'll have a face-palm moment, and not need to post at all.

I pre-emptively apologized for not being able to do this from the beginning as a result of the restrictions imposed by the forum itself.

The rules wouldn't be needed if every shit-stain spammer on earth suddenly got annihilated.

long roundLength = 5000; //in milliseconds

Do you plan to alter the rules in the future to allow for negative round lengths? If not, the type should be unsigned long.

        waitingPlayer();
        waitingPlayerLed(); //this works but for some reason makes p1 score 255 after first round

Can't see the code for this, but I have to wonder why waitingPlayer() doesn't do whatever waitingPlayerLed() does.

I also have to wonder why the first thing that loop() does is stick its head in the sand for a full second. Please explain.

      return false;
      break; // if not equal, then break out of the "for" loop

Nothing gets executed after a return statement.

Can you now post the Serial Monitor output as text?

Do you plan to alter the rules in the future to allow for negative round lengths? If not, the type should be unsigned long.

Noted, thank you.

Can't see the code for this, but I have to wonder why waitingPlayer() doesn't do whatever waitingPlayerLed() does.

That function is in there:

void waitingPlayer() { 
    if (playerTurn == pNumJustTagged) { //5 rounds = 6 turns / 2 ps = 3 rounds of 2 ps
      rndExpireTime = currentMillis + roundLength;
      gameIsLive = 1;
      Serial.print("correct player Found! round ends at: ");
      Serial.println(rndExpireTime);
    }
  }

This function waits for the correct RFID tag and then activates the round timer for that player. waitingPlayerLed() controls the LED display to display the color of the player whose turn it is.

I also have to wonder why the first thing that loop() does is stick its head in the sand for a full second. Please explain.

The delay is just for testing/readability purposes through the serial monitor.

@rooshio, can you comment out other sections of your program (as many as possible to make it as short as possible) and still see the same problem?

You have a 10k program. Go back to the earlier version that did not have the problem and work forward again very slowly so you can see what causes the problem.

...R

Nothing gets executed after a return statement.

I see, making the beak command unnecessary. TY.

Can you now post the Serial Monitor output as text?

What I expect (i.e., when waitingPlayerLed() is commented out)

Found chip PN532
Firmware ver. 1.6
Waiting for an ISO14443A Card ...
p1:0 p2:0 round:1/4 time:1504/30000 pTurn:1 gameOver:0 gameIsLive: 0
p1:0 p2:0 round:1/4 time:2589/30000 pTurn:1 gameOver:0 gameIsLive: 0
correct player Found! round ends at: 8676
p1:1 p2:0 round:1/4 time:3676/8676 pTurn:1 gameOver:0 gameIsLive: 1
p1:2 p2:0 round:1/4 time:4760/8676 pTurn:1 gameOver:1 gameIsLive: 1
p1:3 p2:0 round:1/4 time:5846/8676 pTurn:1 gameOver:1 gameIsLive: 1
p1:4 p2:0 round:1/4 time:6932/8676 pTurn:1 gameOver:1 gameIsLive: 1
p1:5 p2:0 round:1/4 time:8018/8676 pTurn:1 gameOver:1 gameIsLive: 1
p1:5 p2:0 round:2/4 time:9104/8676 pTurn:2 gameOver:1 gameIsLive: 0
p1:5 p2:0 round:2/4 time:10190/8676 pTurn:2 gameOver:1 gameIsLive: 0
p1:5 p2:0 round:2/4 time:11276/8676 pTurn:2 gameOver:1 gameIsLive: 0

What happens instead (i.e., when waitingPlayerLed() is included)

Found chip PN532
Firmware ver. 1.6
Waiting for an ISO14443A Card ...
p1:0 p2:0 round:1/4 time:1504/30000 pTurn:1 gameOver:0 gameIsLive: 0
correct player Found! round ends at: 6504
p1:1 p2:0 round:1/4 time:3377/6504 pTurn:1 gameOver:0 gameIsLive: 1
p1:2 p2:0 round:1/4 time:4464/6504 pTurn:1 gameOver:0 gameIsLive: 1
p1:3 p2:0 round:1/4 time:5549/6504 pTurn:1 gameOver:0 gameIsLive: 1
p1:4 p2:0 round:1/4 time:6635/6504 pTurn:1 gameOver:0 gameIsLive: 1
p1:4 p2:0 round:2/4 time:7721/6504 pTurn:2 gameOver:0 gameIsLive: 0
p1:255 p2:0 round:2/4 time:9606/6504 pTurn:2 gameOver:0 gameIsLive: 0
p1:255 p2:0 round:2/4 time:11495/6504 pTurn:2 gameOver:0 gameIsLive: 0
p1:255 p2:0 round:2/4 time:13383/6504 pTurn:2 gameOver:0 gameIsLive: 0
correct player Found! round ends at: 18383
p1:255 p2:1 round:2/4 time:15261/18383 pTurn:2 gameOver:0 gameIsLive: 1
p1:255 p2:2 round:2/4 time:16349/18383 pTurn:2 gameOver:0 gameIsLive: 1
p1:255 p2:3 round:2/4 time:17434/18383 pTurn:2 gameOver:0 gameIsLive: 1
p1:255 p2:4 round:2/4 time:18519/18383 pTurn:2 gameOver:0 gameIsLive: 1
p1:255 p2:4 round:3/4 time:19605/18383 pTurn:1 gameOver:0 gameIsLive: 0
p1:0 p2:4 round:3/4 time:21492/18383 pTurn:1 gameOver:0 gameIsLive: 0
p1:0 p2:4 round:3/4 time:23367/18383 pTurn:1 gameOver:0 gameIsLive: 0

Note, score registers as "4" twice when the RFID card is held in place. Then the score jumps to 255, and then is later reset to 0.

This function waits for the correct RFID tag and then activates the round timer for that player. waitingPlayerLed() controls the LED display to display the color of the player whose turn it is.

Isn't that more or less an atomic activity? A player takes a turn, the next player is up, and needs to be shown and his/her turn timer started. Seems like one operation (function call) to me, not two. YMMV.

You can, of course, keep waitingPlayerLed(), but call it from wiatingPlayer().

Without the actual output, it's hard to see what is wrong.

Robin2:
@rooshio, can you comment out other sections of your program (as many as possible to make it as short as possible) and still see the same problem?

You have a 10k program. Go back to the earlier version that did not have the problem and work forward again very slowly so you can see what causes the problem.

...R

Maybe I'm misunderstanding you, but the addition of waitingPlayerLed() is the step in this process that makes it not work. But there doesn't seem to be anything involved that would make this so.

  for (byte j = 8; j <= 40; j++) {
    leds[j].r = playerColors[playerTurn][0];
    leds[j].g = playerColors[playerTurn][1];
    leds[j].b = playerColors[playerTurn][2];

j will be assigned a value of 40, and leds[40] will be written to. There is no leds[40]. The maximum index of the leds array is 39.

PaulS:

  for (byte j = 8; j <= 40; j++) {

leds[j].r = playerColors[playerTurn][0];
    leds[j].g = playerColors[playerTurn][1];
    leds[j].b = playerColors[playerTurn][2];



j will be assigned a value of 40, and leds[40] will be written to. There is no leds[40]. The maximum index of the leds array is 39.

I... I think that may have done it! So it seems by telling the fastLED library that I have 40 LEDs, it's creating an index somewhere between 0-39 (for a total count of 40). But by accidentally assigning a rgb value at position 40, it looks like it's actually pointing toward the memory address for the player 1 score! Sure enough, when I throw some dummy values in there, playerScores[1] reflects those same numbers. And by using "<" instead of "<=", it seems to mitigate the issue! I have some familiarity with referencing beyond an array's index, but had no idea how subtle it could be. Thanks so much!

Thanks so much!

And thank you for persevering through the forum restrictions and our insistence that you follow the rules (guidelines, actually), so that we could actually help you. Thank you, too, for following up once you incorporated the fix.

And, yes, writing outside the bounds of an array can cause all kinds of subtle bugs.