Seb Lee-Delisle

Menu

Arduino particles!

My good friend and FlashBrighton veteran Jo Summers has been enthusing at me for some time about the Arduino boards. And I just knew that as soon as I started playing with one, I would get obsessed, so I’ve been putting it off for a while.

I knew you could use Arduinos to interface with the computer, and Flash. But what I didn’t quite register was that you could program these little chips independently of the computer! Once you program them, you can disconnect them and (as long as you give them a power supply) they will continue to do exactly what you want them to do! So actually I haven’t done any work getting them working with Flash at all, I’ve been much more interested in getting them to work independently.

And 2 weeks later, in true predictable seb-style, I’ve made a particle system on the Arduino. I know. I’m sorry.

It all started when we decided to give away 8 workshop kits at the recent FlashBrighton Flex3 launch event, (which appears to have started a mini Brighton epidemic), and I knew I could put off my hardware tinkering no longer.

I love LEDs, or light emitting diodes. You know the little red lights you get in stuff? Ever since I started playing with electronics as a kid – my dad was a physics lecturer and he used to bring home components for me to play with. So naturally the first thing I started playing with was LEDs.

But the arduino only has 13 outputs, so you can only control 13 LEDs? Well that’s cool, but it’s not cool enough. So I started looking into alternatives. And I discovered Charlieplexing! You can read all about charlieplexing here on the instructables site – it’s a complicated way of wiring LEDs that enables you to control many with few connections, and with only 6 outputs, you can control 30 LEDs!

Charlieplexing some LEDs courtesy of Jeremy Keith (Photo by Jeremy Keith)

And that was cool for a while, but not enough. My need for LEDs was not going to be satisfied. So then I borrowed one of these cool 8 x 8 LED matrices from Nexus.

Once I figured out how to wire it up, and using the Arduino Matrix library, I got to work.

The only real trick here is that I’m working internally with a much higher resolution that the actual 8×8 grid, and then rounding to the nearest pixel when displaying each particle. This gives a really smooth movement. I’m only actually lighting one LED per particle but due to the Persistence of Vision (POV) effect it looks like there are more. And the final trick is to add a diffuser. It’s kinda like a hardware blur filter 🙂 It really tricks the eye into seeing the separate particles.

The code’s a bit hacky, but it’s just a prototype. I’ll explain it in more detail later if you’re interested! I just wanted to get this up before I went to sleep. Code below…

#include <binary.h>
#include <sprite.h>
#include <matrix.h>
 
Matrix myMatrix = Matrix(13,11,12);
 
const int numparticles = 4;
 
 
int velx[numparticles];
int vely[numparticles];
long posx[numparticles];
long posy[numparticles];
 
int res = 7;
int maxnum = 8<<res;
int maxspeed = 30;
int count = 0;
 
int row;
int col;
 
int i;
 
void setup()
{
  int i;
 
 
  myMatrix.setBrightness(15);
 
  for(int i = 0; i<numparticles; i++)
  {
    velx[i] = random(11)+5;
    vely[i] = random(11)+5;
    posx[i] = (i%2)<<res;
    posy[i] = (i/2)<<res;
 
  }
}
 
void loop()
{
  myMatrix.clear(); // clear display
 
  for( i = 0; i<numparticles; i++)
  {
 
    row = posx[i]>>res;
    col = posy[i]>>res;
    myMatrix.write(row, col, HIGH);
 
 
    posx[i]+=velx[i];
    posy[i]+=vely[i];
 
    if(posx[i]<0)
    {
      posx[i]=1;
      velx[i]=-velx[i];
    }
    else if(posx[i]>=maxnum)
    {
      posx[i] = maxnum+(maxnum-posx[i]);
      velx[i]= -velx[i];
      vely[i] = vely[i]+random(-1,1);
 
    if(vely[i]>maxspeed) vely[i] = maxspeed;
      else if(vely[i]<-maxspeed) vely[i] = -maxspeed;
 
 
    }
 
    if(posy[i]<0)
    {
      posy[i]=1;
      vely[i]=-vely[i];
    }
    else if(posy[i]>=maxnum)
    {
      posy[i] = maxnum+(maxnum-posy[i]);
      vely[i]=-vely[i];
      velx[i] = velx[i]+random(-1,1);
      if(velx[i]>maxspeed) velx[i] = maxspeed;
      else if(velx[i]<-maxspeed) velx[i] = -maxspeed;
 
 
    }
 
  }
  delay(1);
}
This entry was posted in Arduino, Particles. Bookmark the permalink.

13 Responses to Arduino particles!