Different LED Patterns


I am working on a project where different buttons pressed on the Esplora will cause a different LED pattern. I have 9 LEDs connected to an individual Arduino pin, and to ground. 4 pins on the Esplora are connected to analog pins on the Uno.

I know a little about arrays, and the very basic sample on the Arduino website. https://www.arduino.cc/en/Tutorial/Arrays

However, I was wondering if there was a way to have 4 different arrays that would run dependent on an input signal, rather than using a ridiculous amount of ‘digitalWrite()’ commands with delays, because I read that that is not optimal.

Here is the shell of the code I am working with:

int led1 = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;
int led5 = 6;
int led6 = 7;
int led7 = 8;
int led8 = 9;
int led9 = 10;

int input1 = A1;
int input2 = A2;
int input3 = A3;
int input4 = A4;

void setup() {

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
  pinMode(led8, OUTPUT);
  pinMode(led9, OUTPUT);

  pinMode(input1, INPUT);
  pinMode(input2, INPUT);
  pinMode(input3, INPUT);
  pinMode(input4, INPUT);


void loop() {

  int button1 = digitalRead(input1);
  int button2 = digitalRead(input2);
  int button3 = digitalRead(input3);
  int button4 = digitalRead(input4);

  if (button1 == HIGH) {


  if (button2 == HIGH) {


  if (button3 == HIGH) {


  if (button4 = HIGH) {


  else {


void pattern1() {


void pattern2() {


void pattern3() {


void pattern4() {


void nopattern() {


The idea is that when a button on the esplora is pressed(i need to make seperate code for esplora), a certain pattern will appear in the LEDs until a different button is pressed. All insight or even links to pages that would be useful would be greatly appreciated!

Much thanks,


Yes arrays are the way to go, hey can cut your code down to size. This page I wrote might help you. http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

Grumpy_Mike: Yes arrays are the way to go, hey can cut your code down to size. This page I wrote might help you. http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

This was very useful, thank you! Let me see how well I can digest this and I'll get back to you soon.

Though this link is very helpful I'm having trouble translating it into my situation... Could you guide me a little further?

Well for a start it removes the need for all those led2, led3 and so on. Define an array with the PIN numbers in it, then use a for loop to set the pin mode on all of them. Then define an array with your pattern in it, using one bit of the number for each LED. If you only had 8 LEDs then you could get away with a byte array but as you have 9 then you will have to go to an int array. Then Look at your input buttons to see which array element of the pattern array value to get, and a for loop can be used to set each LED output high or low depending on if there is a one in the pattern number or not.

Almost exactly what that page tells you how to do.

Okay. I wrote this code just to get the idea of one array down. Here is what I have so far:

int pins [8] = { 2, 3, 4, 5, 6, 7, 8, 9 };
int mask = 1;
char two = B11111111 ;

void setup() {

  for(int i = 2; i<7; i++) pinMode(i, OUTPUT);


void loop() {
  for(int i=0; i<8; i++) { if ((mask & two) == 0) {
      digitalWrite(pins[i], LOW); 
    else {
      digitalWrite(pins[i], HIGH);
    mask = mask << 1;


The LEDs turn on one after the other, there is a pause and then they turn off. However, it only happens once. How can I make this a continuous loop of action?

It is doing it continuously the point is that the second time it is doing it all the LEDs are in the same state as you left them so you nee nothing happening. Turn off all the LEDs at the end of the loop to see it repeat.

You also need to reset the value in the mask variable.

can anyone help me to create this led effect


You should start your own thread. You should say what sort of hardware you have and how the LEDs are wired.

k thanks!!