NaMaLiCS Tutorial Part 4, C_SETUP

  • WRTyler
  • -the National Mall guy-
  • wrTyler
  • 3 yrs ago

All Arduino progams have two functions that MUST be included, setup() and loop()setup() is executed once at the startup of the program.  loop() is executed next and runs until you load another program onto the Arduino or you unplug the Arduino from it's power source.

The first thing I do in starting up is open a communications line to a console window and send a text string to the console that the program has started.  For me, having the console is essential for debugging.  There may be other, better ways to debug, but this approach has served me well over the years.  NOTE: There is a beta version of the Arduino IDE that has a live debugging feature.

 

// This is run once when the program is started

void setup() {
  // Setup the serial monitor for displaying text messages
  Serial.begin(9600);

  // Print a text message to the serial monitor
  //  indicating the program has started
  Serial.println("Starting NaMaLiCS");

Next, I set up the Adafruit pixels object which will be used when communicating with the BrickPixel hardware.  This is the first time the Adafruit NeoPixel library gets used.

  // Initialize the NeoPixel object (this is REQUIRED)
  pixels.begin();

  // Clear out any prior BrickPixel settings
  pixels.clear();

For the flicker effects to appear realistic, they need to look random, so set up the random number generator.  The Arduino has a pseudo-random number generator.  If you use a specific number for the seed, every time you start the program, the SAME set of random numbers are generated.  Arduino recommends using the analogRead() function on an unconnected pin on the Arduino board for the seed if you need something truly random.

  // Set up the random number generator used for pixel flickering.
  //  Initialized with an unused analogue pin which appears to give
  //  better randomness
  randomSeed( analogRead(0) );

Then all the pixels get set to a minimum brightness, in this case, totally dark.  This is where some of the global values setup in the B_GLOBALS tab are first used.  NUMPIXELS is used to access all of the pixelSets connected to the BrickPixel shield.  pixels.setPixelColor() is called to change the brightness values of the three pixels within each pixelSet.  pixel.show() pushes out the updated values to all the pixels.  After changes are made to a pixel's brightness in the program, nothing changes on the actual hardware until pixels.show() is called.

  // Set all pixels to minimum brightness
  for(int i=0; i<NUMPIXELS; i++) {
    pixels.setPixelColor(i, pixels.Color(minBrightness, minBrightness, minBrightness));
    pixels.show();
  }

Lastly, we grab the current time in milliseconds and store it in variables that are used for the non-blocking timing approach.

  // Get start timing values used by flickering pixels
  startMillis = millis();
  flickerMillis01 = millis();
  flickerMillis02 = millis();

Next, we'll take a look at the loop() portion of the program.  This is where all the action takes place!

-wrtyler-

Reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
Like Follow
  • 3 yrs agoLast active
  • 17Views
  • 1 Following