// forum: https://forum.arduino.cc/t/moving-numbers-through-an-array/1106764/
// This Wokwi project: https://wokwi.com/projects/360215012852359169
const int buttonPins[8]={ A0,A1,A2,A3,A4,A5,2,3};
int myArray[8] = {0,3,3,3,0,3,3,3};
int oldStates[8];
void setup()
{
Serial.begin(115200);
for( auto a:buttonPins)
pinMode(a, INPUT_PULLUP);
for( auto a:oldStates)
a = HIGH;
printNumbers();
}
void loop()
{
for( int i=0; i<8; i++)
{
int newState = digitalRead( buttonPins[i]);
if( newState != oldStates[i]) // something changed ?
{
if( newState == LOW) // button pressed ?
{
scatter( i);
printNumbers();
}
oldStates[i] = newState; // remember the new state
}
}
delay(20); // for debouncing of the buttons
}
// To be filled in, something with modulo or if-statement
void scatter( int index)
{
int t = myArray[index]; // temporary value, the original number.
myArray[index] = 0; // empty this one first before distributing them
for(int i=0; i<t; i++)
myArray[(index+i+1)%8]++; // modulo 8, to wrap around 0...7
}
void printNumbers()
{
for( auto a:myArray)
{
Serial.print(a);
Serial.print( " ");
}
Serial.println();
}