Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

LED light question for newbie

Status
Not open for further replies.
I happen to have an 8x8 neopixel board laying around so I hooked it up to an Arduino Nano (been meaning to do this for some time). After installing the library as detailed here, adding a push button the code was fairly straight forward.

Whilst not beginner code this may get you started.
Code:
#include <Adafruit_NeoPixel.h>

#define PIN 13
#define KEYPIN 2

Adafruit_NeoPixel horse = Adafruit_NeoPixel(64, PIN, NEO_GRB + NEO_KHZ800);

uint32_t horseCount,lastRead,flashCount;
uint8_t key,previous;

void setup() {
  Serial.begin(115200);
  horseCount=0xfc000000;  //0b11111100000000000000000000000000;
  horse.begin();
  horse.show();
  pinMode(KEYPIN,INPUT_PULLUP);
  lastRead=0;
  displayHorse();
}

void loop(){
  if((millis()-lastRead)>20){       //read key every 20mS
    lastRead=millis();
    key=digitalRead(KEYPIN);
    if(key==0 && previous==1){
      progressHorse();              //move horse along
      displayHorse();               //update the display
      flashCount=millis();          //just to ensure the flash starts lit
    }
    previous=key;                   //keep a copy for next read
  }
  if(horseCount==0xfffffffc){       //all 30 LEDs lit?
    if((millis()-flashCount)>50){   //flash the display at 10Hz
      clearDisplay();
    }
    if((millis()-flashCount)>100){  //the 50 and 100 control the flash rate
      displayHorse();
      flashCount=millis();
    }
  }
}

void progressHorse(){               //move the horse on
  if(horseCount == 0xfffffffc){     //or if completed
    horseCount=0xfc000000;          //reset to first 6 LEDs
    return;
  }
  horseCount>>=1;
  horseCount|=0x80000000;
}

void displayHorse(){
  uint32_t temp=horseCount;
  uint8_t i;
  for(i=0;i<32;i++){
    if(temp&0x80000000)
      horse.setPixelColor(i,horse.Color(25,25,25));
    else
      horse.setPixelColor(i,horse.Color(0,0,0));
    temp<<=1;
  }
  for(i=0;i<32;i++)
    horse.setPixelColor(i+32,horse.Color(0,0,0));
  horse.show();
}

void clearDisplay(){
  uint8_t i;
  for(i=0;i<64;i++)
    horse.setPixelColor(i,horse.Color(0,0,0));
  horse.show();
}
The neopixels are connected to pin D13 and the push button between D2 and Gnd.

When 30 LEDs are lit it flashes them at 10Hz and on the next press of the button resets to the first 6 LEDs.

"
"

Mike.
Edit, the pixels are only at 10% brightness as at full brightness they hurt your eyes.
To change the brightness change the 25s in the following line.
horse.setPixelColor(i,horse.Color(25,25,25));
 
Last edited:
As I said in an earlier post, you only need 10 pins to control 18 players - a 3x6 (5x4 would be better) switch matrix and 1 pin to control all the LEDs.

BTW, the above sketch only uses 13% of the available program memory so controlling 18 players is easily within a Nano's capabilities.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top