I’m having trouble trying understanding why this variable “int rotatePos[3];” needs to be global to work as I expect it to.
If I make this Variable global the program runs fine and you see a stream of numbers counting up at my Serial.println(rotatePos[arrow]); //<–Result. = 1,2,3,4 ETC (Expected result)
However if I include this variable within Arrows{}, it no longer works as I would expect. It compiles, it loads but at the Serial.println(rotatePos[arrow]); //<–Result =1 and appears to stop. If I close “Serial Monitor” and reopen the result = 2, close and reopen “Serial Monitor” result = 3 and so on.
Why is this? What am I missing here!! >:(
look for the <---------------------------- in the code (4 off).
NOTE – The only place the Variable is used is in this sketch. The two instances of “int rotatePos[3];” is only for the demonstration in this thread.
int rotatePos[3]; <---------------------------------------------------- Here works as expected.
void Arrows(byte RunTime) {
int rotatePos[3]; //<---------------------------------------------- Here, does not work
byte arrowArray[] = {0x18, 0x3C, 0x7E, 0x7E, 0x7E, 0x66, 0x66, 0x42};
byte startup[3] = {1, 0, 0};
byte myX = 8;
byte myY = 12;
byte myZ = 8;
byte XY = (myX + myY - 2) * 2; //Arrow 0
byte ZY = (myZ + myY - 2) * 2; //Arrow 1
byte XZ = (myX + myZ - 2) * 2;//Arrow 2
byte lag[] = {0, ZY / 2, 0};
byte ArrowXY[XY];
byte ArrowZY[ZY];
byte ArrowXZ[XZ];
byte fadeInDone = 0;
byte startArrow = 0;
int myColour = 0, ChangeColour = 0;
unsigned long StartTime = millis();
while (millis() < StartTime + RunTime * 1000) {
CleanCube();
memset(ArrowXY, 0, XY * sizeof(byte));
memset(ArrowZY, 0, ZY * sizeof(byte));
memset(ArrowXZ, 0, XZ * sizeof(byte));
byte temp = 0;
for (byte arrow = 0; arrow < 3; arrow++) {
switch (arrow) {
case 0://ArrowXY
for (uint8_t i = 0; i < startup[arrow]; i++) {
ArrowXY[i + lag[arrow]] = arrowArray[i];
// ArrowXY[i] = arrowArray[i];
}
//Loop
//shift and loop the ArrowXY until the correct position is reached
for (int8_t n = 0; n < rotatePos[arrow]; n++) {
//shift first column into a temp array
temp = ArrowXY[0];
//shift all columns down
for (int8_t r = 0; r < XY - 1; r++) {
ArrowXY[r] = ArrowXY[r + 1];
}
//move data from first column into last
ArrowXY[XY - 1] = temp;
}
//increment/reset rotatePos, this is what gives the rotation effect
rotatePos[arrow]++; // <------------------------------------------------- This var.
Serial.println(rotatePos[arrow]); //< ----------------------------------- **** Result ******
if (rotatePos[arrow] > XY - 1) {
rotatePos[arrow] = 0;
//Controls the start of the next arrow.
if (!fadeInDone) {
if (startup[startArrow] >= 8) {
startArrow++;
}
if (startArrow > 2) {
fadeInDone = 1;
}
}
}// end of loop
//Wrap ArrowXY around the outside of the cube
for (int8_t r = 0; r < XY; r++) {
if (r >= 0 && r < myY) {//Going down the left side of cube
copyColumn(arrow, myY - 1 - r, myX - 1, ArrowXY[XY - 1 - r]);
} else if (r >= myY && r < myY + myX - 1) {//Going along the bottom
copyColumn(arrow, 0, myX - (r - myX - (myY - myX)) - 2, ArrowXY[XY - 1 - r]);
} else if (r >= myY + myX - 1 && r < myY + myX + myY - 2) {// Going up the right side
copyColumn(arrow, r - (myY + myX - 2), 0, ArrowXY[XY - 1 - r]);
} else if (r >= myY + myY + myX - 2 && r < (myY + myX) * 2 - 4) {//Going across the top
copyColumn(arrow, myY - 1, r - (myY + myX + myY - 3), ArrowXY[XY - 1 - r]);
}
}//for (int8_t r = 0; r < XY; r++)
break;//Case 0
//************************************************************************************
case 1://ArrowZY
for (uint8_t i = 0; i < startup[arrow]; i++) {
ArrowZY[i + lag[arrow]] = arrowArray[i];
// ArrowZY[i] = arrowArray[i];
}
//Loop
//shift and loop the ArrowZY until the correct position is reached
for (int8_t n = 0; n < rotatePos[arrow]; n++) {
//shift first column into a temp array
temp = ArrowZY[0];
//shift all columns down
for (int8_t r = 0; r < ZY - 1; r++) {
ArrowZY[r] = ArrowZY[r + 1];
}
//move data from first column into last
ArrowZY[ZY - 1] = temp;
}
//increment/reset rotatePos, this is what gives the rotation effect
rotatePos[arrow]++;
if (rotatePos[arrow] > ZY - 1) {
rotatePos[arrow] = 0;
}//if (rotatePos[arrow] > ZY - 1)
//Wrap ArrowZY around the outside of the cube
for (int8_t r = 0; r < ZY; r++) {
if (r >= 0 && r < myY) {//Going down the BACK side of cube
copyColumn(arrow, myY - 1 - r, myZ - 1, ArrowZY[ZY - 1 - r]);
} else if (r >= myY && r < myY + myZ - 1) {//Going along the bottom
copyColumn(arrow, 0, myZ - (r - myZ - (myY - myZ)) - 2, ArrowZY[ZY - 1 - r]);
} else if (r >= myY + myZ - 1 && r < myY + myZ + myY - 2) {// Going up the FRONT side
copyColumn(arrow, r - (myY + myZ - 2), 0, ArrowZY[ZY - 1 - r]);
} else if (r >= myY + myY + myZ - 2 && r < (myY + myZ) * 2 - 4) {//Going across the top
copyColumn(arrow, myY - 1, r - (myY + myZ + myY - 3), ArrowZY[ZY - 1 - r]);
}
}//for (int8_t r = 0; r < ZY; r++)
break;//Case 1
//*******************************************************************************************
case 2://ArrowXZ
for (uint8_t i = 0; i < startup[arrow]; i++) {
ArrowXZ[i + lag[arrow]] = arrowArray[i];
// ArrowXZ[i] = arrowArray[i];
}
//Loop
//shift and loop the ArrowXZ until the correct position is reached
for (int8_t n = 0; n < rotatePos[arrow]; n++) {
//shift first column into a temp array
temp = ArrowXZ[0];
//shift all columns down
for (int8_t r = 0; r < XZ - 1; r++) {
ArrowXZ[r] = ArrowXZ[r + 1];
}
//move data from first column into last
ArrowXZ[XZ - 1] = temp;
}
//increment/reset rotatePos, this is what gives the rotation effect
rotatePos[arrow]++;
if (rotatePos[arrow] > XZ - 1) {
rotatePos[arrow] = 0;
}//if (rotatePos[arrow] > XZ - 1)
//Wrap ArrowXZ around the outside of the cube
for (int8_t r = 0; r < XZ; r++) {
if (r >= 0 && r < myX) {//Going around the BACK side of cube
copyColumn(arrow, myX - 1 - r, myZ - 1, ArrowXZ[XZ - 1 - r]);
} else if (r >= myX && r < myX + myZ - 1) {//Going around right side
copyColumn(arrow, 0, myZ - (r - myZ - (myX - myZ)) - 2, ArrowXZ[XZ - 1 - r]);
} else if (r >= myX + myZ - 1 && r < myX + myZ + myX - 2) {// Going around the FRONT side
copyColumn(arrow, r - (myX + myZ - 2), 0, ArrowXZ[XZ - 1 - r]);
} else if (r >= myX + myX + myZ - 2 && r < (myX + myZ) * 2 - 4) {//Going around the left side
copyColumn(arrow, myX - 1, r - (myX + myZ + myX - 3), ArrowXZ[XZ - 1 - r]);
}
}//for (int8_t r = 0; r < XZ; r++)
break;//Case 2
//*******************************************************************************************************
}//switch (arrow)
}//for (byte arrow = 0;arrow<3;arrow++)
if (!fadeInDone) {
(startup[startArrow] < 8) ? startup[startArrow]++ : 8;
}
// increment_colour
if (ChangeColour++ > sizeof(arrowArray) / 2) {
ChangeColour = 0;
GetColour(myColour++);
if (myColour >= Size_CW) myColour = myColour - Size_CW;
}//if (ChangeColour++ > sizeof(arrowArray) / 2)
delay(75);
}//while (millis() < StartTime + RunTime * 1000)
CleanCube();
}