Close

Code

A project log for 4x4x4 Simple LED Cube

A simple, one color, LED cube that does work.

max-felix-mllerMax-Felix Müller 05/10/2019 at 22:330 Comments

Here is the completed working code for a random pattern. Nothing special, not efficient or fast at all. But it needed to be done quick since it's a gift for my fathers birthday which is now...

#define MOS1 8
#define MOS2 9
#define MOS3 10
#define MOS4 11

#define DAT1 0
#define DAT2 1
#define DAT3 2
#define DAT4 3
#define DAT5 4
#define DAT6 5
#define DAT7 6
#define DAT8 7

#define OE1 A0
#define OE2 A1
#define LE1 A2
#define LE2 A3

void setup() {
  // mosfets
  pinMode(MOS1, OUTPUT);
  pinMode(MOS2, OUTPUT);
  pinMode(MOS3, OUTPUT);
  pinMode(MOS4, OUTPUT);

  // data
  pinMode(DAT1, OUTPUT);
  pinMode(DAT2, OUTPUT);
  pinMode(DAT3, OUTPUT);
  pinMode(DAT4, OUTPUT);
  pinMode(DAT5, OUTPUT);
  pinMode(DAT6, OUTPUT);
  pinMode(DAT7, OUTPUT);
  pinMode(DAT8, OUTPUT);

  // latch
  pinMode(LE1, OUTPUT);
  pinMode(LE2, OUTPUT);

  // disable outpputs
  pinMode(OE1, OUTPUT);
  pinMode(OE2, OUTPUT);
  digitalWrite(OE1, HIGH);
  digitalWrite(OE2, HIGH);
}

int data1[] = {0, 0, 0, 0};
int data2[] = {0, 0, 0, 0};

void loop() {
  // make new random data
  for (int i = 0; i < 4; i++) {
    data1[i] = int(random(256));
    data2[i] = int(random(256));
  }

  for (int x = 0; x < 10; x++) {
    for (int i = 0; i < 4; i++) {
      // enable outpputs
      digitalWrite(OE1, LOW);
      digitalWrite(OE2, LOW);

      // load data1
      if (data1[i] % 2 == 0) {
        digitalWrite(DAT1, HIGH);
      }
      if (data1[i] % 3 == 0) {
        digitalWrite(DAT2, HIGH);
      }
      if (data1[i] % 4 == 0) {
        digitalWrite(DAT3, HIGH);
      }
      if (data1[i] % 5 == 0) {
        digitalWrite(DAT4, HIGH);
      }
      if (data1[i] % 6 == 0) {
        digitalWrite(DAT5, HIGH);
      }
      if (data1[i] % 7 == 0) {
        digitalWrite(DAT6, HIGH);
      }
      if (data1[i] % 8 == 0) {
        digitalWrite(DAT7, HIGH);
      }
      if (data1[i] % 9 == 0) {
        digitalWrite(DAT8, HIGH);
      }

      // latch
      digitalWrite(LE1, HIGH);
      delay(1);
      digitalWrite(LE1, LOW);

      // reset
      digitalWrite(DAT1, LOW);
      digitalWrite(DAT2, LOW);
      digitalWrite(DAT3, LOW);
      digitalWrite(DAT4, LOW);
      digitalWrite(DAT5, LOW);
      digitalWrite(DAT6, LOW);
      digitalWrite(DAT7, LOW);
      digitalWrite(DAT8, LOW);

      // load data2
      if (data2[i] % 2 == 0) {
        digitalWrite(DAT1, HIGH);
      }
      if (data2[i] % 3 == 0) {
        digitalWrite(DAT2, HIGH);
      }
      if (data2[i] % 4 == 0) {
        digitalWrite(DAT3, HIGH);
      }
      if (data2[i] % 5 == 0) {
        digitalWrite(DAT4, HIGH);
      }
      if (data2[i] % 6 == 0) {
        digitalWrite(DAT5, HIGH);
      }
      if (data2[i] % 7 == 0) {
        digitalWrite(DAT6, HIGH);
      }
      if (data2[i] % 8 == 0) {
        digitalWrite(DAT7, HIGH);
      }
      if (data2[i] % 9 == 0) {
        digitalWrite(DAT8, HIGH);
      }

      // latch
      digitalWrite(LE2, HIGH);
      delay(1);
      digitalWrite(LE2, LOW);

      // reset
      digitalWrite(DAT1, LOW);
      digitalWrite(DAT2, LOW);
      digitalWrite(DAT3, LOW);
      digitalWrite(DAT4, LOW);
      digitalWrite(DAT5, LOW);
      digitalWrite(DAT6, LOW);
      digitalWrite(DAT7, LOW);
      digitalWrite(DAT8, LOW);

      // enable mosfet
      if (i == 0) {
        digitalWrite(MOS1, HIGH);
        delay(1);
        digitalWrite(MOS1, LOW);
      } else if (i == 1) {
        digitalWrite(MOS2, HIGH);
        delay(1);
        digitalWrite(MOS2, LOW);
      } else if (i == 2) {
        digitalWrite(MOS3, HIGH);
        delay(1);
        digitalWrite(MOS3, LOW);
      } else if (i == 3) {
        digitalWrite(MOS4, HIGH);
        delay(1);
        digitalWrite(MOS4, LOW);
      }

      // disable outpputs
      digitalWrite(OE1, HIGH);
      digitalWrite(OE2, HIGH);
    }
    delay(1);
  }
}

 If you want only one led to light up, you have to use modulo with prime numbers. Like this, when modulo 4 is zero, modulo 2 will always be zero too.

Discussions