xfpd3x3x3LED.ino
// Please, visit this simulation by Urish...
// https://wokwi.com/projects/278306947843752461

// 3D LED CUBE

// three cathode levels (horizontal) are tied to three analog pins, LOW = ON, HIGH = OFF
// nine anode columns are tied to nine digital pins, LOW = OFF, HIGH = ON

int ledPin[] = {   2,  3,  4, 5, 6, 7, 8, 9, 10, // anode pins / vertical columns
                   14, 15, 16 // cathode pins / horizontal layers
               };

int d1 = 100, d2 = 200, d5 = 500;

void setup() {
  for (int i = 0; i < 12; i++) {
    pinMode(ledPin[i], OUTPUT); // all pins as output
  }

  for (int i = 9; i < 12; i++) {
    digitalWrite(ledPin[i], HIGH); // three cathode pins HIGH (OFF)
  }
}

void loop() {
  // blinkEach();
  // blinkAll();
  // levels();
  // panels();
  randoms();
  // swipe(); // a real mess in work
}

//**************************************************
// FUNCTIONS
//**************************************************

void swipe() { // a real mess in work
  for (int k = 0; k < 9; k++) { // each anode column
    digitalWrite(ledPin[k], !digitalRead(ledPin[k])); // anodes
    for (int j = 0; j < 2; j++) { // on/off
      for (int i = 0; i < 3; i++) { // three cathode
        digitalWrite(ledPin[i + 9], j); // cathodes
        delay(d1);
      }
    }
  }
}

void randoms() {
  int i = random(9), j = i / 3 + 9;
  digitalWrite(ledPin[i], HIGH); // anode
  digitalWrite(ledPin[j], LOW); // cathode
  delay(d1);
  digitalWrite(ledPin[i], LOW); // anode
  digitalWrite(ledPin[j], HIGH); // cathode
}

void levels() { // horizontal levels
  for (int i = 0; i < 9; i++) {
    digitalWrite(ledPin[i], HIGH); // anodes on
  }

  for (int j = 0; j < 3; j++) { // cathodes
    for (int i = 0; i < 2; i++) {
      delay(d1);
      digitalWrite(ledPin[j + 9], i); // on/off
    }
  }

  for (int i = 0; i < 9; i++) {
    digitalWrite(ledPin[i], LOW); // anodes off
  }
}

void panels() { // three anode columns make a panel
  for (int k = 0; k < 3; k++) { // anode columns
    for (int j = 0; j < 2; j++) { // on/off
      for (int i = 0; i < 3; i++) { // three pins (anode and cathode)
        digitalWrite(ledPin[i + k * 3], !digitalRead(ledPin[i + k * 3])); // anodes
        digitalWrite(ledPin[i + 9], !digitalRead(ledPin[i + 9])); // cathodes
      }
      delay(d1);
    }
  }
}

void blinkEach() {
  for (int j = 9; j < 12; j++) {
    digitalWrite(ledPin[j], LOW); // cathode pins A0, A1, A2
    for (int i = 0; i < 9; i++) { // anode pins 2, 3, 4, 5, 6, 7, 8, 9, 1-
      digitalWrite(ledPin[i], HIGH); //turn each anode ON
      delay(d2);
      digitalWrite(ledPin[i], LOW); // turn each anode OFF
    }
    digitalWrite(ledPin[j], HIGH); // cathode OFF
  }
}

void blinkAll() {
  for (int l = 0; l < 3; l++) { // three times
    for (int k = 0; k < 2; k++) { // off/on
      for (int j = 9; j < 12; j++) { // light the LEDs
        digitalWrite(ledPin[j], !digitalRead(ledPin[j])); // cathode pins A0, A1, A2
        for (int i = 0; i < 9; i++) { // anode pins 2, 3, 4, 5, 6, 7, 8, 9, 1-
          digitalWrite(ledPin[i], !digitalRead(ledPin[i])); //turn each anode ON
        }
      }
      delay(500);
    }
  }
}