Coding Basics


#1

Okay so lets pretend that I know absolutely nothing about coding. Ok, I know nothing about coding. I am hoping to use this platform as a way to learn some fundamentals. The problem is, even after spending hours reading through the code examples that you all have posted (awesome job btw!), I still haven’t been able to figure out how to start from scratch and design my own program on the LED cube.

Basically, I don’t even know what I don’t know at this point, and I really want to not suck at this. Can anyone please help me? I’m looking for a basic “Hello World” type introduction to coding this cube that will give me the ability to expand and hopefully go into some of the next kits with a little more confidence…

Thanks!


#2

Do you have some specific questions? Looks like your first go at it worked out well!


#3

@mkenig, my first questions is how do I go about setting the colors to be variable? Here is the code that I’m working on right now…

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>

#define PIN            6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      96

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


void setup() { 
  pixels.begin(); // This initializes the NeoPixel library.
  randomSeed(analogRead(0));
}
int a[]=  {2,1,4};
int b[]= {4,3,6,9,12};
int c[]= {2,11,14};
int d[]= {8,0,2,5,7,8,10,13,15};

uint32_t red=pixels.Color(25,0,0);
uint32_t green=pixels.Color(0,25,0);
uint32_t blue=pixels.Color(0,0,25);

void loop(){
  stripe(a,0);
  //delay(100);
  stripe(a,16);
  //delay(100);
  stripe(a,32);
  //delay(100);
  stripe(a,48);
  //delay(100);
  stripe(a,64);
  //delay(100);
  stripe(a,80);
  delay(500);
  stripe(b,0);
  //delay(100);
  stripe(b,16);
  //delay(100);
  stripe(b,32);
  //delay(100);
  stripe(b,48);
  //delay(100);
  stripe(b,64);
  //delay(100);
  stripe(b,80);
  delay(500);
  stripe(c,0);
 // delay(100);
  stripe(c,16);
  //delay(100);
  stripe(c,32);
  //delay(100);
  stripe(c,48);
  //delay(100);
  stripe(c,64);
  //delay(100);
  stripe(c,80);
  delay(500);
  fill(d,0,green);
  fill(d,16,green);
  fill(d,32,green);
  fill(d,48,green);
  fill(d,64,green);
  fill(d,80,green);
  delay(1000);
  clear_all();
  delay(1000);
}

void clear_all(){
  for(int i=0; i<pixels.numPixels(); i++){
    pixels.setPixelColor(i, pixels.Color(0,0,0));
  }
  pixels.show();
}


void stripe(int letter[], int offset){
  for(int i=1;i<=letter[0];i++){
    pixels.setPixelColor(letter[i]+offset, pixels.Color(0,0,10)); // Moderately bright green color.
  } 
  pixels.show();
}

void fill(int letter[],int offset,int color){
  for(int i=1;i<=letter[0];i++){
    pixels.setPixelColor(letter[i]+offset,pixels.Color(1,1,1));
  }
  pixels.show();
}

The final phrase there sets the color of all the fill commands to (1,1,1) instead of letting me define it with the “int color” thing. How do I fix this?


#4

One thing you could try is using a random color.

So instead of this:
pixels.Color(0,0,10)

You could try this:
pixels.Color(random(0, maxIntensity),random(0, maxIntensity),random(0, maxIntensity))

This would set the three RGB aspects of the color to three random numbers of whatever maxIntensity you want (0 to 255).


#5

Tom,

I recommend getting a copy of Simon Monk’s “Programming Arduino: Getting Started with Sketches”, second edition. It’s about $11 on Amazon. Another intro book is “Getting Started with Arduino: The Open Source Electronics Prototyping Platform (Make)”, 3rd Edition by Massimo Banzi and Michael Shiloh - about $15 on Amazon. Humble Bundle had a special on Make books a few months ago - pdf versions of 12 Make books covering Raspberry Pi’s, Arduino’s, sensors, IOT, etc. for just a few bucks.

The Arduino sketches use the c programming language. Never hurts to have an intro to c book. It would be more generic about programming in general than an Arduino book.

I generated a simple “Hello” sketch to flash HELLO around the sides of the cube for you. I tried to include some comments to make it easier to understand what the code is doing.

Have fun.

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

// Display Hello on LED Cube

#define PIN 6

// The following section sets up some useful variables
uint8_t wait = 500;
uint8_t maxIntensity = 100;  // max color value is 255. Set to lower value to reduce intensity of colors
const uint8_t numFaces = 6;
const uint8_t numRows = 4;
const uint8_t numCols = 4;
const uint8_t numPixelsPerFace = numRows * numCols; // not needed, just added to reduce computations
const uint16_t numPixels = numFaces * numPixelsPerFace;
uint8_t face = 0;
uint8_t row = 0;
uint8_t col = 0;

/*  The following line creates an instance of an Adafruit_NeoPixel object named thimbleCube.
 *  It provides the code to light individual LED's on the cube. We tell it the number of
 *  LED's (pixels), the pin on the Arduino to use (6), and some set up info).
 */ 
Adafruit_NeoPixel thimbleCube = Adafruit_NeoPixel(numPixels, PIN, NEO_GRB + NEO_KHZ800);

/*  Now set up the LED patterns for the letters in HELLO. This is an array of LED positions on
 *  a cube face.
 *  Since we're using c, the first LED is at position 0. 
 *  So a face looks like this: 
 *     0,  1,  2,  3
 *     4,  5,  6,  7
 *     8,  9, 10, 11
 *    12, 13, 14, 15
 *  We can set the color of any LED on the cube by passing its sequential number in a call to thimbleCube.  
 *  For the whole cube, it's LED's 0-95. To set the 9th LED on the 3rd face we have to add an
 *  offset - the number of LED's on the first two faces (32). So the 9th LED on the 3rd face is LED
 *  number 41.
 */
uint8_t numPixelsInH(9);
uint8_t letterH[] = {0,2,4,6,8,9,10,12,14};
uint8_t numPixelsInE(9);
uint8_t letterE[] = {0,1,2,4,8,9,12,13,14};
uint8_t numPixelsInLL(10);
uint8_t letterLL[] = {0,2,4,6,8,10,12,13,14,15};
uint8_t numPixelsInO(8);
uint8_t letterO[] = {1,2,4,7,8,11,13,14};

// and finally lets set a color for the letters
uint32_t blueColor = thimbleCube.Color(0, 0, maxIntensity);
uint32_t blackColor = thimbleCube.Color(0, 0, 0);

void setup() {
  thimbleCube.begin();      // this line initializes the object thimbleCube setting all LED's off (black).
  thimbleCube.show();       // this lights the LED's - at this time it lights them black - same as off
}

void loop()
{
  uint8_t face = 1;    // just write "HELLO" around sides of cube starting on face #2- remember #1 is 0
  uint8_t pixelOffset = face * numPixelsPerFace;
  // first the "H"
  for (uint8_t pixel = 0; pixel < numPixelsInH; ++pixel)
  {
    thimbleCube.setPixelColor(letterH[pixel] + pixelOffset, blueColor);    
  }
  thimbleCube.show();
  delay(wait);

  // now for "E" on next face
  ++face;                                // unitary command to increment variable by 1
  pixelOffset = face * numPixelsPerFace; // update offset for next face
  for (uint8_t pixel = 0; pixel < numPixelsInE; ++pixel)
  {
    thimbleCube.setPixelColor(letterE[pixel] + pixelOffset, blueColor);    
  }
  thimbleCube.show();
  delay(wait);

  // now for "LL" on next face
  ++face;                                
  pixelOffset = face * numPixelsPerFace; 
  for (uint8_t pixel = 0; pixel < numPixelsInLL; ++pixel)
  {
    thimbleCube.setPixelColor(letterLL[pixel] + pixelOffset, blueColor);    
  }
  thimbleCube.show();
  delay(wait);

  // and finally "O"
  ++face;
  pixelOffset = face * numPixelsPerFace;
  for (uint8_t pixel = 0; pixel < numPixelsInO; ++pixel)
  {
    thimbleCube.setPixelColor(letterO[pixel] + pixelOffset, blueColor);    
  }
  thimbleCube.show();
  delay(wait);
  
  paintCube(blackColor);  // turns all LED's off
  delay(wait);
}

// this is a function to paint the cube all one color - black to turn off all LED's
void paintCube(uint32_t color)
{
  for (uint8_t pixel = 0; pixel < numPixels; ++pixel)
  {
    thimbleCube.setPixelColor(pixel, color);
  }
  thimbleCube.show();
}

#6

Tom,

Just replace this line in function fill to use the color you pass in instead of a really light white.

//    pixels.setPixelColor(letter[i]+offset,pixels.Color(1,1,1));
pixels.setPixelColor(letter[i]+offset,color);

#7

Hi Tom,
As well as changing the pixels.Color(1,1,1) with your variable color (as @Grandpa has suggested), you should also match the type of the variable you are receiving in the method with what is being passed to it.
In this case the method is defining void fill(int letter[],int offset,int color). So the variable color is an integer value, but green, red and blue are defined at the top as uint32_t variables. This means your method should say void fill(int letter[],int offset,uint32-t color)

@Grandpa suggestion to get some beginners books is also a great one. You might also try some of the Arduino Tutorials at https://www.arduino.cc/en/Tutorial/HomePage they do require a few other electrical bits and pieces, but as you grow your collection you will also grow your understanding.

Happy coding
Coyote


#8

This is a great place to start! Thanks a ton guys!