Running out of space

Hi.

This is my first post here so I hope I do this correct.

I have had my arduino for a couple of weeks and have never programmed before.

I have built a 6x6x6 cube with 74HC238. (Can get them from my school) I know its not the smartest way, but want to learn in the process.

I have had 6 days of electrical education in school, mechanical engineer , so excuse my ignorance in Electronics and programming

Problem is I am running out of Space with my program.. Can someone help me with some kind of smart Work around? dont know how to optimize the program size.

I have attached my program.

Thank you very much
Ronni
Denmark

_6x6x6-1.00.ino (94.6 KB)

It may not save space but your program is crying out for arrays and for loops to be used instead of acres of repeated code.

Programming requires finding ways to generalize processes so that they can be described by repetitive actions, but with variables changing from one iteration to the next. Go back and read up on the control structures in the Arduino reference, and study some examples.

this example may be complicated for someone new to coding (might be wrong as I haven't any way to test).

look at the general idea and see if you can follow the code. (I hate stacking "ifs" on examples so it might be difficult just click on the right of the closed brace } and it will show where the open { brace is)

//this is based on crude digital to binary conversion
// your program followed binary counting so i hope
//you understand how it works
// no way to really test but when tested in parts 
//it seemed to work


byte zNumbers = 0;
byte pNumbers = 0;
byte  pcount = 0;
byte  zcount = 0;
byte oneTime = 0;
byte ledArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13};
byte pArray[] = {4, 3, 2, 6, 5, 1};
byte zArray[] = {7, 8, 9, 10, 11, 13};
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
byte stage = 1;
byte extracount = 0;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (byte x = 0; x < 12; x = x + 1) {
    //do this 12 times
    pinMode (ledArray[x], OUTPUT);
    digitalWrite(ledArray[x], LOW);
  }
}

void loop() {
  unsigned long currentMillis = millis();
  if (stage == 1) {//single leds
    if (pcount < 6) {
      if (currentMillis - previousMillis1 >= 50) {//50ms delay
        zcount++;
        if (zcount >= 36) {
          zcount = 0;
          pcount++;
        }
        zNumbers = zcount;
        pNumbers = pcount;
        singleLed();
        previousMillis1 = currentMillis;
      }
    } else {
      stage = 2;
      pcount = 0;
      zcount = 0;
    }
  }
  if (stage == 2) {//levels
    if (pcount < 6) {
      if (currentMillis - previousMillis2 >= 50) {
        zcount++;
        if (zcount >= 36) {
          zcount = 0;
          extracount++;
          if (extracount >= 5) {
            extracount = 0;
            pcount++;
          }
          zNumbers = zcount;
          pNumbers = pcount;
          singleLed();
          previousMillis2 = currentMillis;
        }
      }
    } else {
      stage = 3;
      pcount = 0;
      zcount = 0;
    }
  }


  if (stage == 3) { //cols
    zNumbers = 31; //all leds on in zArray
    //to bored to carry on


    delay(5000);
    stage = 1;
  }
}


void singleLed() {//this clears all before writing
  for (byte x = 0; x < 12; x = x + 1) {
    digitalWrite(ledArray[x], LOW);
    turnOnLeds();
  }
}

void turnOnLeds() {//this turns on required led based on znumber and pnumber

//written is 2 parts as you may want to leave some leds on 
//then you can use a diffrent void rather than singleLed() to clear the 
//other leds




  if (zNumbers >= 32) {
    digitalWrite(zArray[5], HIGH);
    zNumbers = zNumbers - 32;
  }
  if (zNumbers >= 16) {
    digitalWrite(zArray[4], HIGH);
    zNumbers = zNumbers - 16;
  }
  if (zNumbers >= 8) {
    digitalWrite(zArray[3], HIGH);
    zNumbers = zNumbers - 8;
  }
  if (zNumbers >= 4) {
    digitalWrite(pArray[2], HIGH);
    zNumbers = zNumbers - 4;
  }
  if (zNumbers >= 2) {
    digitalWrite(zArray[1], HIGH);
    zNumbers = zNumbers - 2;
  }
  if (zNumbers >= 1) {
    digitalWrite(zArray[0], HIGH);
    zNumbers = zNumbers - 1;
  }
  
  
  //////////////////////////////////////////////

  if (pNumbers >= 32) {
    digitalWrite(pArray[5], HIGH);
    pNumbers = pNumbers - 32;
  }
  if (pNumbers >= 16) {
    digitalWrite(pArray[4], HIGH);
    pNumbers = pNumbers - 16;
  }
  if (pNumbers >= 8) {
    digitalWrite(pArray[3], HIGH);
    pNumbers = pNumbers - 8;
  }
  if (pNumbers >= 4) {
    digitalWrite(pArray[2], HIGH);
    pNumbers = pNumbers - 4;
  }
  if (pNumbers >= 2) {
    digitalWrite(pArray[1], HIGH);
    pNumbers = pNumbers - 2;
  }
  if (pNumbers >= 1) {
    digitalWrite(pArray[0], HIGH);
    pNumbers = pNumbers - 1;
  }
}

if the program doesn't work just tell us what it is doing so I can find the mistakes

It really helps to learn beginner basics before launching an ambitious project:

And these Example codes (in your IDE) are very beginner.

https://www.arduino.cc/en/Tutorial/Arrays

https://www.arduino.cc/en/Tutorial/SwitchCase

https://www.arduino.cc/en/Tutorial/WhileStatementConditional

so excuse my ignorance in Electronics

Take a physics course to start and electronics will make sense quicker, same as chemistry.

Ehm.. As a mechanical engineer student I have physics

Cant see why a physics course can change that.

But thanks for the links.

GoForSmoke:
Take a physics course to start and electronics will make sense quicker, same as chemistry.

I can see the outputs are working on the arduino but my decoders dont activate with your program

I have attached my proteus setup, if you use it...

Thank you

gpop1:
this example may be complicated for someone new to coding (might be wrong as I haven't any way to test).

look at the general idea and see if you can follow the code. (I hate stacking "ifs" on examples so it might be difficult just click on the right of the closed brace } and it will show where the open { brace is)

//this is based on crude digital to binary conversion

// your program followed binary counting so i hope
//you understand how it works
// no way to really test but when tested in parts
//it seemed to work

byte zNumbers = 0;
byte pNumbers = 0;
byte  pcount = 0;
byte  zcount = 0;
byte oneTime = 0;
byte ledArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13};
byte pArray[] = {4, 3, 2, 6, 5, 1};
byte zArray[] = {7, 8, 9, 10, 11, 13};
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
byte stage = 1;
byte extracount = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  for (byte x = 0; x < 12; x = x + 1) {
    //do this 12 times
    pinMode (ledArray[x], OUTPUT);
    digitalWrite(ledArray[x], LOW);
  }
}

void loop() {
  unsigned long currentMillis = millis();
  if (stage == 1) {//single leds
    if (pcount < 6) {
      if (currentMillis - previousMillis1 >= 50) {//50ms delay
        zcount++;
        if (zcount >= 36) {
          zcount = 0;
          pcount++;
        }
        zNumbers = zcount;
        pNumbers = pcount;
        singleLed();
        previousMillis1 = currentMillis;
      }
    } else {
      stage = 2;
      pcount = 0;
      zcount = 0;
    }
  }
  if (stage == 2) {//levels
    if (pcount < 6) {
      if (currentMillis - previousMillis2 >= 50) {
        zcount++;
        if (zcount >= 36) {
          zcount = 0;
          extracount++;
          if (extracount >= 5) {
            extracount = 0;
            pcount++;
          }
          zNumbers = zcount;
          pNumbers = pcount;
          singleLed();
          previousMillis2 = currentMillis;
        }
      }
    } else {
      stage = 3;
      pcount = 0;
      zcount = 0;
    }
  }

if (stage == 3) { //cols
    zNumbers = 31; //all leds on in zArray
    //to bored to carry on

delay(5000);
    stage = 1;
  }
}

void singleLed() {//this clears all before writing
  for (byte x = 0; x < 12; x = x + 1) {
    digitalWrite(ledArray[x], LOW);
    turnOnLeds();
  }
}

void turnOnLeds() {//this turns on required led based on znumber and pnumber

//written is 2 parts as you may want to leave some leds on
//then you can use a diffrent void rather than singleLed() to clear the
//other leds

if (zNumbers >= 32) {
    digitalWrite(zArray[5], HIGH);
    zNumbers = zNumbers - 32;
  }
  if (zNumbers >= 16) {
    digitalWrite(zArray[4], HIGH);
    zNumbers = zNumbers - 16;
  }
  if (zNumbers >= 8) {
    digitalWrite(zArray[3], HIGH);
    zNumbers = zNumbers - 8;
  }
  if (zNumbers >= 4) {
    digitalWrite(pArray[2], HIGH);
    zNumbers = zNumbers - 4;
  }
  if (zNumbers >= 2) {
    digitalWrite(zArray[1], HIGH);
    zNumbers = zNumbers - 2;
  }
  if (zNumbers >= 1) {
    digitalWrite(zArray[0], HIGH);
    zNumbers = zNumbers - 1;
  }
 
 
  //////////////////////////////////////////////

if (pNumbers >= 32) {
    digitalWrite(pArray[5], HIGH);
    pNumbers = pNumbers - 32;
  }
  if (pNumbers >= 16) {
    digitalWrite(pArray[4], HIGH);
    pNumbers = pNumbers - 16;
  }
  if (pNumbers >= 8) {
    digitalWrite(pArray[3], HIGH);
    pNumbers = pNumbers - 8;
  }
  if (pNumbers >= 4) {
    digitalWrite(pArray[2], HIGH);
    pNumbers = pNumbers - 4;
  }
  if (pNumbers >= 2) {
    digitalWrite(pArray[1], HIGH);
    pNumbers = pNumbers - 2;
  }
  if (pNumbers >= 1) {
    digitalWrite(pArray[0], HIGH);
    pNumbers = pNumbers - 1;
  }
}





if the program doesn't work just tell us what it is doing so I can find the mistakes

6x6x6 - Version 1.00.PDF (138 KB)

nissan200sx:
Ehm.. As a mechanical engineer student I have physics

Cant see why a physics course can change that.

But thanks for the links.

don't take that as a insult as your original post said that you only had 6 days of electrical education in school .

If you want to be good at electronics you have to understand the basics of what electricity is and the laws that it follows. Just the fact that you understand there are such things as laws of physics is a head start. so you should already learnt and have a good understanding of ohms laws etc.

its really hard to follow so many lines of code so you may have to dumb it down a little so I understand what its doing.

im guessing that I skipped the fact that pin 12 should he turned on

pEN im now seeing as pin enabled ?

add to top of program before set-up

#define pEN 12

then add to set up

pinMode(pEN, OUTPUT);
digitalWrite(pEN, HIGH);

other things I may have skipped because I found no code or I didn't understand there real use

#define DECODER_BITS 8
#define LEDS_PER_ROW 6

:slight_smile:

I dont have so many issues with the physical part of my electrical setup, its the programming thats difficult.

I know and can use ohm law, can connect the IC chips I have used so far in my builds, learnt binary for this project, but cant get my program optimized. I really want to remove the delay() feature abd use mills instead as delay slows the program Down and fills up the memory real quick.

The examples I have found uses digitalwrite, but need to use a void instead of a digital output.

Thanks

Now I have active outputs.

But it Works backwards.. ? It starts with last LED in the row.

Thanks for the help.

gpop1:
its really hard to follow so many lines of code so you may have to dumb it down a little so I understand what its doing.

im guessing that I skipped the fact that pin 12 should he turned on

pEN im now seeing as pin enabled ?

add to top of program before set-up

#define pEN 12

then add to set up

pinMode(pEN, OUTPUT);
digitalWrite(pEN, HIGH);

other things I may have skipped because I found no code or I didn't understand there real use

#define DECODER_BITS 8
#define LEDS_PER_ROW 6

The examples I have found uses digitalwrite, but need to use a void instead of a digital output.

digitalWrite() is a function. What you refer to as a "void" is also a function. The word void in this context just means that the function does not return a value, although it may well change global variables so that the changed values are available to other parts of the program.

nissan200sx:
:slight_smile:

I dont have so many issues with the physical part of my electrical setup, its the programming thats difficult.

I know and can use ohm law, can connect the IC chips I have used so far in my builds, learnt binary for this project, but cant get my program optimized. I really want to remove the delay() feature abd use mills instead as delay slows the program Down and fills up the memory real quick.

The examples I have found uses digitalwrite, but need to use a void instead of a digital output.

Thanks

no problem we all start off with a very small tool box of code functions then as a project gets more complicated we start to look for better tools to use

take set up for example

yours did this

void  setup() {
  //set decoder pins  to  low
  pinMode(p0, OUTPUT);
  digitalWrite(p0,  LOW);
  pinMode(p1, OUTPUT);
  digitalWrite(p0,  LOW);
  pinMode(p2, OUTPUT);
  digitalWrite(p0,  LOW);
  pinMode(p3, OUTPUT);
  digitalWrite(p0,  LOW);
  pinMode(p4, OUTPUT);
  digitalWrite(p0,  LOW);
  pinMode(p5, OUTPUT);
  digitalWrite(p0,  LOW);

  //set cathode pins  to  low
  pinMode(Z0, OUTPUT);
  digitalWrite(Z0,  LOW);
  pinMode(Z1, OUTPUT);
  digitalWrite(Z1,  LOW);
  pinMode(Z2, OUTPUT);
  digitalWrite(Z2,  LOW);
  pinMode(Z3, OUTPUT);
  digitalWrite(Z3,  LOW);
  pinMode(Z4, OUTPUT);
  digitalWrite(Z4,  LOW);
  pinMode(Z5, OUTPUT);
  digitalWrite(Z5,  LOW);

  //enable  decoders
  pinMode(pEN,  OUTPUT);
  digitalWrite(pEN, HIGH);
}

theres nothing wrong with your set up but its a long way to write all the pins to OUTPUT and LOW

now I looked at that I though if the pins could assign a number to represent the real pin number then I could just repeat digitalWrite and pinmode instead

so I looked in the tool box and pulled out a array

byte ledArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13};

now the first point in the array is 0. so when I use the array and use 0 it will be changed by the program to 1, when I use 11 (remember we count 0 so 0 to 11 is 12 total) 11 will be changed to the last number in the array which is 13

now I can use a for to do most of the set up work for me

 for (byte x = 0; x < 12; x = x + 1) {
    //do this 12 times
    pinMode (ledArray[x], OUTPUT);
    digitalWrite(ledArray[x], LOW);
  }
}

this for will make x change from 0 to 1 to 2 to 3 etc until 11
each time the x will be used in the pinmode line so its the same as saying

pinMode (array 0,OUTPUT);
pinMode (array 1,OUTPUT);
pinMode (array 2,OUTPUT);

as the number is referencing a array then
ledArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13}

then in real terms I have said

pinMode (array 0,OUTPUT); = pinMode (pin 1 ,OUTPUT);

pinMode (array 11,OUTPUT); = pinMode (pin 13 ,OUTPUT);

p.s remove the serial.begin(9600) line as that uses pin 0 and pin 1 it was something I used to test a idea that you shouldn't require, It may cause a problem later on

you should also see that I skipped the

pinMode(pEN, OUTPUT);
digitalWrite(pEN, HIGH);

as its just two lines of code then theres no real reason to make it complicated so they just need to be added back in the same way you had them

nissan200sx:
Now I have active outputs.

But it Works backwards.. ? It starts with last LED in the row.

Thanks for the help.

dumb it down for me. which pin or pins would be high on the first led that does comes on compared to which pin or pins should be on for the led you want to start on

ok now ive looked at the wiring example I think I have a better understanding. Its not fun looking at 4000 lines of code and trying to follow the patterns.

ops that looked wrong

ok try this I think its right

I struggled to keep z and p straight in my head so I had to rename them to things that made sense.

#define pinEnable 12
byte layerNumber = 0;
byte matrixNumber = 0;
byte matrixCount = 0;
byte oneTime = 0;
byte outputArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13};
byte matrixArray[] = {4, 3, 2, 6, 5, 1};
byte layerArray[] = {7, 8, 9, 10, 11, 13};
unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;
byte stage = 1;
byte extracount = 0;


void setup() {
  // put your setup code here, to run once:

  for (byte x = 0; x < 12; x = x + 1) {
    //do this 12 times
    pinMode (outputArray[x], OUTPUT);
    digitalWrite(outputArray[x], LOW);
  }
  pinMode (pinEnable, OUTPUT);
  digitalWrite(pinEnable, HIGH);
}

void loop() {
  unsigned long currentMillis = millis();
  if (stage == 1) {//single leds
    if (layerNumber < 6) {
      if (currentMillis - previousMillis1 >= 50) {//50ms delay
        matrixCount++;
        if (matrixCount >= 36) {
          matrixCount = 0;
          layerNumber++;
        }
        matrixNumber = matrixCount;
        singleLed();
        previousMillis1 = currentMillis;
      }
    } else {
      stage = 2;
      matrixCount = 0;
      layerNumber = 0;
    }
  }
  if (stage == 2) {//levels
    if (layerNumber < 6) {
      if (currentMillis - previousMillis2 >= 50) {
        matrixCount++;
        if (matrixCount >= 36) {
          matrixCount = 0;
          extracount++;
          if (extracount >= 5) {
            extracount = 0;
            layerNumber++;
          }
          matrixNumber = matrixCount;
          singleLed();
          previousMillis2 = currentMillis;
        }
      }
    } else {
      stage = 3;
      matrixCount = 0;
      layerNumber = 0;
    }
  }

  if (stage == 3) { //cols
    layerNumber = 0;
    matrixNumber = 36;
    //all leds on in layerArray
    //to bored to carry on
    delay(5000);
    stage = 1;
  }
}


void singleLed() {//this clears all before writing
  for (byte x = 0; x < 12; x = x + 1) {
    digitalWrite(outputArray[x], LOW);
    turnOnLeds();
  }
}

void turnOnLeds() {//this turns on required led based on znumber and pnumber

  //written is 2 parts as you may want to leave some leds on
  //then you can use a diffrent void rather than singleLed() to clear the
  //other leds
  if (matrixNumber >= 32) {
    digitalWrite(matrixArray[5], HIGH);
    matrixNumber = matrixNumber - 32;
  }
  if (matrixNumber >= 16) {
    digitalWrite(matrixArray[4], HIGH);
    matrixNumber = matrixNumber - 16;
  }
  if (matrixNumber >= 8) {
    digitalWrite(matrixArray[3], HIGH);
    matrixNumber = matrixNumber - 8;
  }
  if (matrixNumber >= 4) {
    digitalWrite(matrixArray[2], HIGH);
    matrixNumber = matrixNumber - 4;
  }
  if (matrixNumber >= 2) {
    digitalWrite(matrixArray[1], HIGH);
    matrixNumber = matrixNumber - 2;
  }
  if (matrixNumber >= 1) {
    digitalWrite(matrixArray[0], HIGH);
    matrixNumber = matrixNumber - 1;
  }

  digitalWrite(layerArray[layerNumber], HIGH);
}

I will re-write the code using this layout:

unsigned int leddataB[] = { // Pin Nr: 15,14,13,12,11,10,9,8
B00010000 // 1

unsigned int leddataD[] = { // Pin Nr: 7,6,5,4,3,2,1,0 (7 always = 1)
B10000000, // 1

void led(index) {
PORTB= leddataB[index-1];
PORTD= leddataD[index-1];
}

Have switched pin 7 wire with pin 12 and redefined pEN 12 to pEN 7

Juuust need to rewrite my 4000+ lines Down to binary.

Thanks for the help.

Sorry for the interruption.

but I just loved my old '87 200sx

it was an awesome little go-kart that took way more abuse than it ever should have.

ahhh memories.

now back to your regularly scheduled program. :wink:

haha. Yeah its a nice car!

I have AWD from skyline on mine and RB34DET engine.

WOW to cool!!

mine was just a beater hatchback with a 5sp.

but i could get that thing sideways and hold it there for miles.

but enough off topic stuff you've got 4000 lines of code to rewrite. :slight_smile: