Sunday, May 1, 2011

Arduino Alien Animation on an 8x8 LED display

A little animation of a space invaders alien on an 8x8 LED display

Using a pair of 74HC595 shift registers.

Below is the code.  Be aware I'm more about getting the Arduino to do something and so the code will be a bit rough around the edges.  It works but I definitely do not consider it best practise or even efficent code.
Use at your own risk.

// Control an 8x8 LED display with 2 x 74HC595 shift registers
// Using only 3 pins from the Arduino

//Pin connected to Pin 12 of 74HC595 (Latch)
int latchPin = 8;

//Pin connected to Pin 11 of 74HC595 (Clock)
int clockPin = 12;

//Pin connected to Pin 14 of 74HC595 (Data)
int dataPin = 11;

uint8_t led[8];
uint8_t ledo[8];
uint8_t ledn[8];
long counter1 = 0;
int setn=0;

void setup() {
//set pins to output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

// 1st animation for the character
ledo[0] = B01000010;
ledo[1] = B00100100;
ledo[2] = B00111100;
ledo[3] = B01111110;
ledo[4] = B11111111;
ledo[5] = B10111101;
ledo[6] = B11000011;
ledo[7] = B01100110;

// 2nd animation for the character
ledn[0] = B11000011;
ledn[1] = B00100100;
ledn[2] = B00111100;
ledn[3] = B01011010;
ledn[4] = B11111111;
ledn[5] = B00111100;
ledn[6] = B00100100;
ledn[7] = B00100100;

// set the display to the 1st character
  for (int i=0; i<8; i++) {
  led[i]= ledo[i];

void loop() {
// counter1 used for delay in animation

// set the LEDs

if (counter1 >=150) {
counter1 = 0;

// setn is used to decide id ledn or ledo is used for the animation
if (setn ==0){
  for (int i=0; i<8; i++) {
  led[i]= ledn[i];
  setn = 1;
  else {
  for (int i=0; i<8; i++) {
  led[i]= ledo[i];
  setn = 0;


void screenUpdate() {
uint8_t row = B00000001;

for (byte k = 0; k < 9; k++) {
// Open up the latch ready to receive data
digitalWrite(latchPin, LOW);
shiftIt(~row );
shiftIt(led[k] ); // LED array

// Close the latch, sending the data in the registers out to the matrix
digitalWrite(latchPin, HIGH);
row = row << 1;

void shiftIt(byte dataOut) {
// Shift out 8 bits LSB first,
// on rising edge of clock

boolean pinState;

//clear shift register read for sending data
digitalWrite(dataPin, LOW);

// for each bit in dataOut send out a bit
for (int i=0; i<8; i++) {

  //set clockPin to LOW prior to sending bit
digitalWrite(clockPin, LOW);

// if the value of DataOut and (logical AND) a bitmask
// are true, set pinState to 1 (HIGH)
if ( dataOut & (1<<i) ) {
pinState = HIGH;
else {
  pinState = LOW;

//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);

//send bit out on rising edge of clock
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);

//stop shifting
digitalWrite(clockPin, LOW);

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.