NeoPixel Shield Fun and a Robot Sim!

Back at Maker Faire, I picked up a few cool things to play with. One of these things was a Adafruit NeoPixel Shield for the Arduino.

For those who don’t know, the NeoPixel is an addressable LED that Adafruit Industries sells. The shield (that I got) is a board with 40 of these LED’s on it. The thing that makes this device (and LED’s for that matter) so wonderful, is that it only requires a three wire connection, those being positive (+), negative (-) and data. This means that you can use as many as you like (providing you have enough RAM) with out eating into your I/O of your Arduino, or and other microprocessor.

After I did a little soldering, as some assembly is required for use as a shield with the Arduino, I downloaded the library files from Adafruit and loaded an example onto me Mega 2560. I was amazed by what I saw, and also a little blinded as these LED’s are bright! Thought it got me thinking about what I could do with this new toy of mine.

After thinking it over for a little while, I came up with a very neat idea, thought I need just a little back story. For one of my classes in college I was tasked to build a robot with a team that would travel thought a simple maze. Me, being the overachiever that I am, decided that I wanted to use a method of steering and navigating the robot using active mapping. This is when I discovered a tutorial on doing active mapping using something called the Wavefront Algorithm, at a place called Society of Robots (This is it!). Included in this tutorial was a simulator. This simulator would show you step by step the path your robot would take with given obstacles on the map. This was very cool but was done in a black and white command prompt window and was too fast to see what was happening. Although the simulator was really only mean to trouble shoot the logic and algorithm the future robot would follow and check to make sure it all worked, I thought it would be cool to see a virtual robot travel through a maze.

With my project in mind I got to work, with the first problem I needed to face was how I was going to draw everything to the shield. This is an issue as the simulator wrote to the computer display by using a 2D array, while the NeoPixel shield is just a string of LED’s. This turned out to be no problem at all as you can store 2D arrays in a 1D array. With just a little bit of math, and by setting X and Y values for the array I was able to make a function which would give me a location in a 1D array which would be the same for a 2D array.

 uint16_t SetElement(uint16_t row, uint16_t col)
{
     //array[width * row + col] = value;
     return Y * row+col;
} 

With that out of the way, the rest was easy. I copied over all the code from the C++ source over to the Arduino sketch and removed all the things I knew wouldn’t work like cout and fout statements. I then went thought the code making the array size the program could work with, set from one main variable. The last thing I had to do was create a way to output to the lights. Being that the original program was all text based, all that needed to be done for each location in the array was to output a letter or number, but now that the display has been replaced with lights this wasn’t going to work. Again this was easy to overcome. I created a function that would take in the value of the object which needed to be printed and the location it needed to be printed to, and it would intern set that Led to a set color.

void LED_Print(int x,int y,int object)
{
   if(object==nothing)
   {
     strip.setPixelColor(SetElement(x,y),strip.Color(0,0,0));//off
   }
   else if(object==wall)
   {
     strip.setPixelColor(SetElement(x,y),strip.Color(100,0,0));//red
   }
   else if(object==goal)
   {
     strip.setPixelColor(SetElement(x,y),strip.Color(255,215,0));//yellow
   }
   else if(object==robot)
   {
     strip.setPixelColor(SetElement(x,y),strip.Color(0,0,100));//blue
   }
   else
   {
     strip.setPixelColor(SetElement(x,y),

     strip.Color(100/object*2,71/object*2,0));//yellow dim for distance
   }

}

After I compiled, and fixed all my missing semicolons I had this…

I could have stopped here but there’s no fun in that, so I decided to take it one step further. I made a second version of the code with an added function, which after four moves of the robot, the field would randomly regenerate. This then shows the real power behind Wavefront. I don’t have a video of this due to a slight flaw. Being that the new field is random, sometimes there is no path to the goal, and when this happens, nothing will change for a long time as the code needed to cycle a whole bunch of times in the background before a new field is created.

Both versions of the code can be found at my GitHub:

Wavefront Fixed Maze: HERE (https://github.com/M-tech-Creations/NeoPixel_Shield_Wavefront_fixed)

Wavefront Dynamic Maze: HERE (https://github.com/M-tech-Creations/NeoPixel_Shield_Wavefront_dynamic)

 

Updated to add direct GitHub links (10/17/15)

Arduino, Arduino C, Hardware, Programing , , , , ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.