**CUBE CODE** Red Blue Emergengy Flashing

The "Cube4" is a 4x4x4 RGB LED cube with an Arduino Leonardo compatible onboard controller. [Product page]
Post Reply
yatto
Posts: 5
Joined: Wed Jan 06, 2016 3:58 pm

**CUBE CODE** Red Blue Emergengy Flashing

Post by yatto » Fri Jan 22, 2016 2:16 pm

Hi guys

Bought this kit for my 11 year old and I to build and get a taste for electronics and coding. Absolutely loved it, and it works a treat. So we wanted to try our first Sketch, bare in mind we (I) have never done any sort of coding before, so I am sure there are better ways of achieving what we did but it seems to work ok. Our first attempt was to try and replicate the Red Blue flashing of emergency lights. End result is ok. If anyone has any tips for simplifying the Sketch please feel free to share

Code: Select all

#include "SPI.h"
#include "Cube.h"

int timer=500.0;
int timer2=50.0;
int timer3=350;
int timer4=125;

Cube cube;

void setup(void) 
{
cube.begin(0, 115200); // Start on serial port 0 (USB) at 115200 baud
}
  
void loop(void) {

//Fast Flash

      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);

      cube.all (BLACK);
      delay (timer2);

      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);

      cube.all (BLACK);
      delay (timer2);
      
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);

      cube.all (BLACK);
      delay (timer2);

      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);

      cube.all (BLACK);
      delay (timer2);

      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);

      cube.all (BLACK);
      delay (timer2);
      
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);

      cube.all (BLACK);
      delay (timer2);

      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);

      cube.all (BLACK);
      delay (timer2);
      
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer2);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay (timer2);

      cube.all (BLACK);
      delay (timer2);

//Slow Flash

      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer3);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer3);
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer3);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer3);
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer3);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer3);
      cube.setplane(X, 0, RED);
      cube.setplane(X, 1, RED);
      cube.setplane(X, 2, BLACK);
      cube.setplane(X, 3, BLACK);
      delay(timer3);
      cube.setplane(X, 0, BLACK);
      cube.setplane(X, 1, BLACK);
      cube.setplane(X, 2, BLUE);
      cube.setplane(X, 3, BLUE);
      delay(timer3);
          
          
      
      
      }

neographophobic
Posts: 9
Joined: Mon Sep 28, 2015 6:36 am

Re: **CUBE CODE** Red Blue Emergengy Flashing

Post by neographophobic » Sun Jan 24, 2016 11:45 am

That's really cool. Congrats on getting that sketch working.

You mentioned that you were interested in tips for simplifying the sketch and that you were new to coding. I hope the following helps. I'll post the full revised sketch, and then explain the bits within it for you.

Code: Select all

#include "SPI.h"
#include "Cube.h"

int fastDelay = 50;
int slowDelay = 350;

Cube cube;

void setup(void)
{
  cube.begin(0, 115200); // Start on serial port 0 (USB) at 115200 baud
}

void loop(void) {
  //Fast Flash
  for (int i = 1; i <= 4; i++) {

    // Red Flash
    for (int j = 1; j <= 3; j++) {
      cube.box(0, 0, 0, 1, 3, 3, RED);
      delay(fastDelay);
      cube.all(BLACK);
      delay(fastDelay);
    }

    // Extra delay between Red and Blue Flashes
    delay(fastDelay);

    // Blue Flash
    for (int j = 1; j <= 3; j++) {
      cube.box(2, 0, 0, 3, 3, 3, BLUE);
      delay(fastDelay);
      cube.all(BLACK);
      delay(fastDelay);
    }

    // Extra delay between Red and Blue Flashes
    delay(fastDelay);
  }

  //Slow Flash
  for (int i = 1; i <= 4; i++) {
    cube.all(BLACK);
    cube.box(0, 0, 0, 1, 3, 3, RED);
    delay(slowDelay);
    cube.all(BLACK);
    cube.box(2, 0, 0, 3, 3, 3, BLUE);
    delay(slowDelay);
  }

  cube.all(BLACK);
}
Explanations:-

Code: Select all

int fastDelay = 50;
int slowDelay = 350;
I renamed timer2 and timer3 to names that were more descriptive. As code gets more complex it is handy to have variables that are easy to determine what they do. I personally use a style of lowercase first word, then uppercase first character of any other words (sometimes called Camel Case). Other people put underscores between words. It's a personal preference.

Code: Select all

  //Fast Flash
  for (int i = 1; i <= 4; i++) {
  	// do stuff
  }
The "for" line is a loop. Loops cause the same piece of code to be run multiple times. When I looked at your sketch I noticed the following pattern. The "fast flashes" are made up of the red flashing fast 4 times, then the blue flashing fast 4 times then this is repeated 4 times. The "slow flashes" are made up of the red then blue shown 4 times. This is an ideal candidate for using multiple loops. In the above example I've added "// do stuff" to represent the items you want to be repeated, and "}" which finishes the loop.

int i = 1; means declare a variable i, and set its initial value to 1. i <= 4; means that the loop should keep running while i is less than or equal to 4. i++; means that at the end of the loop, add 1 to the value of i. All together it says we will loop the following code 4 times, incrementing our counter (i) by 1 each time.

I mentioned above about using descriptive names for variables, yet in the loop I use "i" (and "j" later). "i" and "j" are typically used within loops by many programmers as short hand for counters.

Code: Select all

    // Red Flash
    for (int j = 1; j <= 3; j++) {
      cube.box(0, 0, 0, 1, 3, 3, RED);
      delay(fastDelay);
      cube.all(BLACK);
      delay(fastDelay);
    }
This handles drawing the red "fast" flash. It starts with another loop, which will run 3 times. It uses the "box" method to draw the RED LEDs (it does the same as the two cube.setplane(X, 0, RED); and cube.setplane(X, 1, RED); lines. It then pauses for the fastDelay time, and then it sets all LEDs to BLACK, and pauses the fastDelay time again.

Code: Select all

    // Extra delay between Red and Blue Flashes
    delay(fastDelay);
As it says, we add an additional delay from when we finish the RED fast flashes, to when we move to the BLUE fast flashes

Code: Select all

    // Blue Flash
    for (int j = 1; j <= 3; j++) {
      cube.box(2, 0, 0, 3, 3, 3, BLUE);
      delay(fastDelay);
      cube.all(BLACK);
      delay(fastDelay);
    }
The code for the blue fast flash is pretty much identical to the red fast flashes. The only difference is that we draw a blue box that is the same as cube.setplane(X, 2, BLUE); and cube.setplane(X, 3, BLUE);

Code: Select all

  //Slow Flash
  for (int i = 1; i <= 4; i++) {
    cube.all(BLACK);
    cube.box(0, 0, 0, 1, 3, 3, RED);
    delay(slowDelay);
    cube.all(BLACK);
    cube.box(2, 0, 0, 3, 3, 3, BLUE);
    delay(slowDelay);
  }
After the fast flash has finished (the red flash is run 4 times, then the blue flash is run 4 times, and then the red and blue fast flash sequences are repeated another 3 times), we move onto the slow flash. Again it is a loop that runs 4 times. It starts by turning all LEDs off and then instantly drawing the red side, pausing the slowDelay time, then turning all of the LEDs off and instantly drawing the blue side and pausing the slow delay time again.

This is how I would simplify your sketch. I'm sure other people have different ideas as well and I would love to hear them as well.

I hope this helps.

yatto
Posts: 5
Joined: Wed Jan 06, 2016 3:58 pm

Re: **CUBE CODE** Red Blue Emergengy Flashing

Post by yatto » Tue Jan 26, 2016 11:45 am

Thanks neographophobic, that's awesome, exactly what I was hoping for

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests