Can I make this shorter?

Hello,
I want to make an LED-Cube and i started programming a little bit. My question is if i want to make pattern like this it would be a very big code. Can i make this shorter?Verwende dieses Symbol um Code zu posten?

int data = 2;
int clock = 3;
int latch = 4;


unsigned long m1[] = {1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1};
unsigned long m2[] = {1,1,0,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1};
unsigned long m3[] = {1,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1};
unsigned long m4[] = {1,1,1,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0};
unsigned long m5[] = {0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1};


void setup() {
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);
  
  
  
}

  


void loop() {
   digitalWrite(latch, LOW);
  for(int i=0; i<20; i++){
    digitalWrite(clock, LOW);
    digitalWrite(data, m1[i]);
    digitalWrite(clock, HIGH);
  }
  digitalWrite(latch, HIGH);

  delay(500);

  digitalWrite(latch, LOW);
  for(int i=0; i<20; i++){
    digitalWrite(clock, LOW);
    digitalWrite(data, m2[i]);
    digitalWrite(clock, HIGH);
  }
  digitalWrite(latch, HIGH);

  delay(500);

  digitalWrite(latch, LOW);
  for(int i=0; i<20; i++){
    digitalWrite(clock, LOW);
    digitalWrite(data, m3[i]);
    digitalWrite(clock, HIGH);
  }
  digitalWrite(latch, HIGH);

  delay(500);

  digitalWrite(latch, LOW);
  for(int i=0; i<20; i++){
    digitalWrite(clock, LOW);
    digitalWrite(data, m4[i]);
    digitalWrite(clock, HIGH);
  }
  digitalWrite(latch, HIGH);

  delay(500);

  digitalWrite(latch, LOW);
  for(int i=0; i<20; i++){
    digitalWrite(clock, LOW);
    digitalWrite(data, m5[i]);
    digitalWrite(clock, HIGH);
  }
  digitalWrite(latch, HIGH);

  delay(500);

}


Yes, you can make that shorter.

Don't waste 32 bits when 1 will do.

Why unsigned long for an array that only stores 0 and 1?
Read about 2D arrays.

for (int i blabla) {
    for (int j blabla) {
           digitalWrite(data, m[i][j];
    }
}

Hint

const int data = 2;
const int clock = 3;
const int latch = 4;

byte m[5][20] = {{1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1},
                 {1,1,0,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1},
                 {1,0,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1},
                 {1,1,1,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0},
                 {0,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1}};

void setup() {
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);
}

void loop() {
  for (int i=0; i<5;i++) {
   digitalWrite(latch, LOW);
   for(int j=0; j<20; j++){ 
    digitalWrite(clock, LOW);
    digitalWrite(data, m[i][j]);
    digitalWrite(clock, HIGH);
   }
   digitalWrite(latch, HIGH);
   delay(500);
  } 
}

Why use eight hundred bits, when, at most, one hundred and sixty will do the job?

1 Like
unsigned long m1[] = {0xE00FF};
unsigned long m2[] = {0xD00FF};
unsigned long m3[] = {0xB00FF};
unsigned long m4[] = {0xEFF00};
unsigned long m5[] = {0x700FF};
1 Like

...or even an array of uint32_t

i tried this but i does not work. how do I have to write the void loop with hexa?

What does that mean?

Try this code:
I have no tested with LEDs, only on simulator.

PS: Cube - Wokwi Arduino and ESP32 Simulator

# define  data  2
# define clock  3
# define latch  4

unsigned long m[] = { 0b11100000000011111111,
                      0b11010000000011111111,
                      0b10110000000011111111,
                      0b11101111111100000000,
                      0b01110000000011111111
                    };
//---------------------------------------------------------------
void setup() {
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);
  pinMode(latch, OUTPUT);
}
//---------------------------------------------------------------
void loop() {
  for (int i = 0; i < 5; i++)
  {
    digitalWrite(latch, LOW);
    flash(m[i]);
    digitalWrite(latch, HIGH);
    delay(500);
  }
}
//---------------------------------------------------------------
void flash(long myArray)
{
  for (int i = 19; i > -1; i--) {
    digitalWrite(clock, LOW);
    digitalWrite(data, bitRead(myArray, i));
    digitalWrite(clock, HIGH);
  }
}

You can move the bits around with << (rotate left) and >> (rotate right) or you can use "mask" bits to test (or read) an individual bit or bits (0xE00FF & 0x00002 will result in 0x2) but (0xE00FF & 0x11102 will also result in 0x2). Arduino Bitwise information

my set up are 4"sets" of 8 LED. With my code above I let one set of LED shine and the turn it off and then an other set of 8 LED and so on. but when i replace it with hexa it makes something completly different.

No.
Didn't understand any of that.

The arrays show 5 sets of 20.
4 * 8 = 32
5 * 20 = 100

i try to explain it.


i want to control 512 led with 9 shift register. 1 output from one shift register controls one row of one slice. that means i can control every 8 rows of one slice with one shift register. so i can control every 8 slice with 8 shift register. but the problem is how can i control each LED of one row? simply by using a ninth shift register and control the 8 "layers" with this shift register. the left 8x4 LED should represent one slice but only the half of it. (their should another 4 layers above it. the right 8 LED should represent a second slide. so if i give data on the first output of the first register and data for the lowest layer with the third shift register (the one one the left side). the lowest LED on the left side will light up. the code above will let light one set of 8 LED and so on. i hope its understandable

Ah. The 8x8x8.

Yes. the code i sent first does work but imagine if i want to make a pattern or something. the code with all the 1's and 0's would be very long. this is because i asked to make my code shorter.

Kevin Darrah uses calculations, because patterns can be calculated without entering static values.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.