Tag Archives: starter-kit

Remember the colours – a game

The following article is based on this entry on Newton’s blog (in Polish). All the pictures, program code and the video clip came from there.

This project’s goal was to use as many parts from the Arduino Starter Kit, as possible. So, what is this project exactly? As the title itself suggests, it’s a game, whose object is to remember a colour sequence and recreate it. At the very beginning the sequence consists of 3 colours only, hovever, the difficulty level increases – an extra colour is added each time. Every colour has a different tone assigned. This tone is emitted from a buzzer when the corresponding colour is displayed, so the game involves two senses, both sight and hearing. User interface consists of: RGB LED diode displaying the colours, 8 red LED diodes arranged in a row to display which colour in turn we are setting and two microswitch type buttons. By pushing both in the same time we are starting the game, the left button chooses the colour, the right – accepts. There are two more LED diodes, indicating whether we have recreated the sequence properly (left LED), or not (right LED).

Whole device mounted on a breadboard looks like the following:

Device mounted on a breadboard

You can see (and hear) how it works on this video clip. Hereunder is the circuit schematic made in Fritzing:

Circuit schematic

Circuit schematic

All the resistors used are 220Ω and the IC in the centre of the breadboard is 74HC595 shift register, which allows to drive eight LEDs using only three Arduino digital pins. This was described in “Shifted LEDs” article.

When everything is connected, we should take care of the code. At the beginning we must declare where we have connected what (to which input/output). The following code is responsible of it:

int latchPin = 8, clockPin = 12, dataPin = 3; //shift register
byte data = 0; //register data

int led8 = 7, led9 = 6; //additional LEDs
int ledR = 11, ledG = 10, ledB = 9; //LED RGB
int buttonLeft = 5, buttonRight = 4; //buttons
int speakerPin = 13; //buzzer

Here comes the initialization part – setting pin as input/output, turning internal pull-up resistors on and so on:

void setup()
{
  Serial.begin(57600);
  for(int i=3;i<=13;i++) // setting all the pins
    pinMode(i, OUTPUT); // from 3 to 13 as outputs
  pinMode(buttonLeft, INPUT); // setting the button pins
  pinMode(buttonRight, INPUT); // as inputs

  digitalWrite(buttonLeft, HIGH); // turning internal pull-up
  digitalWrite(buttonRight, HIGH); // resistors on
  randomSeed(analogRead(0)); // initializing the pseudorandom number generator
  // with the value read from analog input no. 0 
  // - each time it's different
  clearLeds();
}

If you are wondering what stands for such a strange notation:

 a ? b : c

here is the explanation.

The main loop of our program looks like the following:

void loop()
{
  effect();
  clearLeds();
  if(waitForButton() == 3)
    return;

  for(int j=3;j<=8;j++)
    if(!playLevel(7, j))
      return;
}

Consecutively: at the beginning, effect() function turns on visual and light effects, clearLeds turns all the LEDs off. After that, unless the user pushes both buttons at the same time, what will cause a reset, playLevel function starts successive levels of the game, increasing the second parameter (number of the lights to remember) by 1. The first parameter denotes the total number of colours (from 0 to 7). Of course, we can lower it, but then the game will become too easy. It’s better to add another colours using setColor function (changing parameters in setRGB) and upgrade it. Then, we can put the total number of colours (remember – the numeration starts from 0!) as the first parameter of playLevel function to make the game more difficult and hence – more addicting ;).

The whole program code with comments is available here. I think it’s clear enough and doesn’t require any additional explanations.

Arduino’s eye, or about a photoresistor, not Tolkien’s mythology

Arduino’s eye – sounds like a quote from J.R.R. Tolkien, while it’s about connecting photoresistor to Arduino. With this article I would to begin short course dealing with basics of electronics.

Photoresistor is a element whose resistance is dependent of amount of light falling on it. What only remains is to measure this resistance. In which way?

For a playful use we will ignore accuracy, assuming simplified model.

How it looks?

Simple circuit built on breadboard

Simple circuit built on breadboard (click on the picture to see notes on Flickr)

Circuit diagram looks like the following:

Circuit diagram

Circuit diagram

What is depicted here is so called voltage divider. You can read on Wikipedia, how it works.

Briefly speaking, voltage on the junction point between two resistors will be varying according to resistance of the photoresistor. And this is what we will be measuring with our program.

This is its code:

// pin, from which we read the voltage
int photoPin = 0;
//the reading
int val = 0;

//initial configuration
void setup()
{
  pinMode(photoPin, INPUT); // setting the pin as input
  Serial.begin(57600);
}

void loop()
{
  //reading and standarizing to range 0-7
  val = analogRead(photoPin)/128;
  for (int i=0;i<=val;i++) {
    Serial.print(".");
  }
  Serial.println();
  delay(90);
}

Program (as hardly every on Arduino) consists of the section preparing the device to work (setup) and main loop (loop) evoked as long as the power is available…

In main loop the voltage is measured, then the reading is converted in range 0-7 (because Arduino reads voltage as a number from 0 to 1023, so dividing it by 128 and rounding down gives digit from 0 to 7) and appropiate number of diodes is lighted up. Short break, and loop again…

The result:

Arduino IDE during execution of our program

Arduino IDE during execution of our program

While the program is running, I’m covering the photoresistor with my hand, changing its resistance, and thus the voltage being read by Arduino from pin 0. The number of dots is proportional to the reading.

Part list:

Coming soon – how to physically show varying reading.

LED diodes – how to flash and light

Recently there was silence on Starter Kit, because before holidays I was preparing to Bootstrap 9.4, what took most of my free time. Now, taking advantage of free day I’m making up for it.

Previously, we were dealing with reading from external sensor (photoresistor). Now we will try to show this reading.

We will use 7 LED diodes arranged in a line making a kind of indicator – the more light falls on the photoresistor, the more diodes will light. Beginning from the end – how it looks in action:

Arduino’s Eye from Starter Kit on Vimeo.

Forgive poor quality, but mobile phone camera is the only tool I can use to perpetuate my experiments with Arduino.

But despite of that the effect is visible. Behind the frame I zoom in and out the photoresistor to a lamp as the diodes are lighting up and fading out.
Continue reading

Indicator – how to use a button

Weekend month ago became rainy, so I decided to stay at home with my three children. It was a kind of challenge to survive it without strained nerves :)

My son (3 years and a half) is on the stage of racking the carpet in every way with every toy car and train available, so I came up with idea of dicator, as he called it…

Indicator in action

Indicator in action

Thirty minutes and I succeded to make such a thing. I hoped it would arouse interest for the next half an hour, but it turned out to be essential component of obstacle courses builded on the carpet for a couple of days.

How it works?

The literature first – the idea was to make a simple indicator changing its state after pressing a button.

The red LED diode we connect to digital input number 3, the green to no. 3 and the button to no. 4 input.

Circuit diagram:

Code:

int buttonPin = 4;

int LED = HIGH;

int greenPin = 02;                // LED connected to digital pin 13
int redPin = 03;
int infraPin = 0;
int val = LOW;
int prev = LOW;

#define ST_OFF 0
#define ST_ON 1
#define ST_PULSE 2
int status = ST_OFF;

//initial configuration
void setup()
{
  pinMode(greenPin, OUTPUT);      // sets the digital pin as output
  pinMode(redPin, OUTPUT);      // sets the digital pin as output
  pinMode(buttonPin, INPUT); // sets the digital pin as input
  Serial.begin(57600);
  digitalWrite(redPin,HIGH);
  digitalWrite(greenPin,HIGH);
}

void display_status() {
  int static pulse = LOW;
  int static count = 5;
  switch (status) {
    case ST_OFF:
    off(redPin);
    on(greenPin);
    break;
    case ST_ON:
    on(redPin);
    off(greenPin);
    break;
    case ST_PULSE:
    off(redPin);
    digitalWrite(greenPin, pulse);
    pulse = !pulse;
    delay(400);
    count--;
    if (count==0) {
      status=ST_ON;
      count=5;
    }
  }
}

void next_status() {
  switch (status) {
    case ST_OFF:
    status = ST_PULSE;
    break;
    case ST_ON:
    status = ST_OFF;

    break;
    case ST_PULSE:

    break;
  }
}

void on( int pin) {
  digitalWrite(pin,HIGH);
}
void off( int pin) {
  digitalWrite(pin,LOW);
}
void loop()
{
  val = digitalRead(buttonPin);
  if (val == HIGH && prev == LOW) {
    next_status();
  }
  prev = val;
  display_status();
  delay(50);

}

The indicator can be in three states: off (ST_OFF, green light), on (ST_ON, red light) and pulse (ST_PULSE, green flashes).

In the main loop is performed a test, if the button was pressed. If yes, it goes to next state (next_status) excluding ST_PULSE, in which it stubbornly stays. Thanks to it, pressing the button during flashing don’t cause jumping to next state.

After that, the state according to current status (display_status) is displayed. Here the flashing is implemented and, after a proper number of loops, the state is changed to ST_ON, which stands for red.

display_status could be source of problems if it was more complicated circuit. It adds its own delay in ST_PULSE (delay(400)), changes state – asking for trouble. But in such a simple circuit – it can be.

What new?

The new part, which we weren’t using is a switch. Simple switch, whose pressing Arduino should detect.

Indicator - simple but works

Indicator - simple but works

Potential trap – why is there a resistor? Why don’t like this:

Don't try it at home :)

Don't try it at home :)

Well, when the switch is turned off, everything is all right. Digital input is connected to the supply voltage and remains in high state, but after turning the switch on, supply voltage is shorted to the ground, what may damage the supply device.

In case of not using the supply voltage, we should realize that when the button isn’t pressed, then the digital input remains not connected and it may be improperly detected by Arduino. The state of this input is random, high or low. Pulling it up to +5V with resistor makes us sure that the state will be stable and device not damaged.

Arduino – play!

A long silence on the blog is just broken! During the last few months I was strongly engaged in projects not connected with Arduino, whole time was absorbed by keeping the Arduino shields shop, servicing customers, searching for new suppliers, etc.

Now I have a bit time which I can spend by documenting experiments with Arduino, thus I hope to add some examples with Starter Kit soon.

Silence, go away!

As we have to break the silence, so więc let’s begin from controlling so called buzzer a tiny speaker able to produce not very loud sounds, with the Arduino. Let’s start from final product:

Arduino buzzer from Starter Kit on Vimeo.

Buzzer is controlled by Arduino, playing simple melody. Playing speed depend on reading from photoresistor (the more light, the slower it plays).

How it’s made?

I wrote, how to read values from photoresistor in one of previous posts. Buzzer is stimulated to play by properly applied high and low voltage. For (Arduino output port) safety a 220 Ohm resistor is added to lower the current passing through (from PDF with documentation appears that it’s not obligatory – at least 14 Ohm resistance should not generate current higher than 36 mA – lower than the limit value for Arduino I/O port – 40 mA).
Thankfully Arduino is open source and there are many ready to use elements. So, playing melody (note) issue is described in Melody tutorial. We will take advantage of it and add only part reading value from photoresistor and converting it to adequate delay.

The only modification of Melody tutorial (except from setup) is a line in loop:

 tempo=analogRead(analog)/3+100;

It divides the voltage reading by 3 and adds 100. This way we will obtain value in range from 100 to 1023/3+100=441. Original tempo was delay 300. Only the schematic remains:

The device schematic

The device schematic

For beginners – a detailed description, how to connect the wires:

Physical connections

Physical connections

Detailed part list (Fritzing has only piezo buzzer in basic library, so it is used on the schematics):

Fritzing Bill of Materials

Sketch: 	buzzer.fz
Date: 	Tue Sep 29 01:11:59 2009

Arduino1      Arduino Diecimila
Breadboard1   Tiny Breadboard
J1            Piezo Speaker
R1            220 Ω Resistor
R2            10k Ω Resistor
R3            Basic Photo-Resistor (Photocell)

Shopping List

Quantity	Part

1		10k Ω Resistor
1		220 Ω Resistor
1		Arduino Diecimila
1		Basic Photo-Resistor (Photocell)
1		Piezo Speaker
1		Tiny Bre

And finally – the code used in example: melody.zip

PS
How do you like the diagrams and pictures designed in Frizting? For me, it’s perfect software for documenting experiments with Arduino. I reccommend – check it.

How to measure temperature with Arduino and MCP9700

One of the sensors included in Arduino Starter Kit is a temperature sensor. It’s analog device which doesn’t need any additional elements to work (it’s exactly MCP9700-E/TO). So, briefly speaking, after connecting ground and power supply we are already able to measure temperature. On the sensor’s page in Nettigo shop there is PDF datasheet in Files bookmark. So, let’s begin from terminals:

MCP9700 pinout

MCP9700 pinout

Important – the terminals are showed from the bottom of the sensor. To the first we connect supply voltage (both 3.3V and 5V from Arduino would be OK), ground (GND) goes to the third and the second terminal should be connected to Arduino Analog0 pin. As we can read from the datasheet (first page), sensitivity equals 10 mV/ºC. On the second page we have Output Voltage, 500 mV for 0°C. That’s all we should know to write a simple program:

float temp;

void setup() {
  Serial.begin(57600);
};

void loop () {
  temp = analogRead(0)*5/1024.0;
  temp = temp - 0.5;
  temp = temp / 0.01;
  Serial.println(temp);
  delay(500);
};

The program measures temperature and sends the reading to Serial, from which we can read it using serial port monitor in Arduino IDE. How the temperature is calculated? I divded the whole process in three steps to make it easier to understand:

  1. temp = analogRead(0)*5/1024.0; Reading value from analog input and converting it to voltage. Maximal voltage Arduino is able to measure equals 5V and the A/D converter’s resolution is 10 bits, which means 1024 values, thus voltage value on Analog0 input is the value returned by analogRead multiplied by the voltage equaling one step of A/D converter. You have to remember, that dividing 5 by 1024 would be interpreted by compiler as integer operation and the result in this case will equal 0. Because of this in the code appears 1024.0 and thanks to this entry, the compiler will treat the dividing as floating point operation.
  2. temp = temp - 0.5; Calibrating to 0°C – the difference between voltage read from the sensor and 500 mV is linearily dependent on temperature.
  3. temp = temp / 0.01; This difference is divided by 10mV/step and now we have temperature

As you can see, the MCP9700 sensor is very simple to use and beginners-friendly. Now everyone can measure temperature with Arduino.

Let’s play, daddy – Family Feud

I was inspired by a simple DIY game seen on the web (I can’t find the link now) and despite the fact that original version was based on greatly simpler PIC I decided to build it with Arduino Starter Kit.

The rules are simple – the one, who will press his button faster after a diode lights up, gets a score. If the button will be pressed before the diode lights up, a point goes to opponent. The game continues, until someone gets five points. Let’s see it in action:

Simple game with Arduino from Starter Kit on Vimeo.

Beside the diode, which will be watched, we need also two diodes to indicate who gets the point and who has won the whole duel, buzzer to play simple melody at the end of the game and two buttons.

Continue reading

When it lacks of current

Sooner or later in experiments with Arduino we will want to drive some kind of device, which needs more than 40 mA to work (40 mA is limitary value, because it’s maximum current that Arduino digital output can supply) It may be a relay, lighting bulb or other current-greedy device.

What to do?

We can use a transistor as an amplifier. Analog techniques is very broad issue, which I don’t know very much (everyone who has studied with me know what I mean :) ), but here I will attempt to present a few informations about driving devices with higher current demand. This example is based on materials from Arduino Starter Kit available on Nettigo.eu.

What you have to know about transistors.

Types – regarding of structure. Basic types are bipolar and field-effect (unipolar). We will focus on the bipolar. They have two groups: NPN and PNP. The difference in structure is other arrangement of semiconductor. Difference in using them is other way the current flows in the transistor, so there are two configurations of the supply voltage.

It seems easier to use NPN transistors with Arduino, because they need to be polarised with positive voltage that Arduino provides on digital output.

NPN type bipolar transistor symbol

NPN type bipolar transistor symbol

Bipolar transistors can work in many applications and each of them can be used in other way. We need to switch high current with low current from Arduino output. To precise, the application we will use is called transistor switch. In this application the transistor is switched between two states: saturation and cutoff.

  • cutoff: UBE is too low to bias the base-emiter junction. Thus, no current flows through the collector.
  • saturation: such a large current flows into the base, that the law IC=hFE*IB isn’t true anymore. In this case, UCE voltage hardly reach zero.

Virtually, the collector current is limited only by the load – what’s connected between the collector and the ground. Current efficiency of our power supply also is a limitation. You must also be careful to not exceed the maximum allowed collector current, which, for currently supplied with Starter Kit MPS2222A transistor equals 600mA.

How it looks in practice?

If we supply a 1 mA current to base, the maximal collector current will be 35 mA (1mA * 35). In case of 10 mA – IC will be max. 350 mA. If IB equals 20 mA, IC will be max…. 600 mA. 600, not 700, because 600 mA is maximum collector current of MPS2222A.

It is worst case, because hFE may vary for different copy of transistor, for one can equal 35, for another 40 – then maximum transistor current 600 mA will be achieved already for 15 mA IB.

Before sketching the circuit – rules of polarising NPN transistor to work as switch:

  • emitter connected to ground
  • positive voltage on base (switch is on) or 0 (switch is off)
  • collector connected to supply voltage through load

So:

NPN transistor - using method

NPN transistor - using method

When we close S1 switch (it equals setting Arduino digital output in high state) UCB will be 0.6V, a current starts to flow into the base. It must be high enough to saturate the transistor, so UCE must fall below UCE(sat) (collector-emiter junction saturation voltage). According to the datasheet UCE(sat) value depends on IC and IB currents. We can estimate it as about 0.3V.
In Arduino Starter Kit there isn’t any part, which could receive current higher than 40 mA, but we can join parallelly some LEDs to demonstrate this issue in practice.

Example of using 4 diodes to exceed 40 mA load

Example of using 4 diodes to exceed 40 mA load

Let’s estimate current flowing through one diode – voltage drop between collector and emitter in saturation state is 0V, voltage drop on red diode – about 1.8V so on the resistor – 5-1.8 = 3.2. For resistors R2-R5 of value 280 Ohm the current flowing through them should be 3.2/280 = 11.42 mA. For 4 diodes it should give about 45 mA.

Before connecting the transistor we should know which terminal is which:

MPS2222A - pinout

MPS2222A - pinout

  1. emitter
  2. base
  3. collector

Physically connections illustrated in Fritzing (frankly admitting – this time it won’t be easy to read. The images probably will be better illustration):

Physically connected circuit

Physically connected circuit

And two photos (on the left of breadboard there is other circuit assembled, so everything what is visible under 25th row doesn’t matter):

Physical construction - take one

Physical construction - take one

Physical construction - take two

Physical construction - take two

Assembly warning – cutted off resistor terminals are perfectly suitable for making short jumpers on breadboard – they are clearly visible in the picture.

Bill of materials generated from Fritzing (all the required parts are included in Arduino Starter Kit from Nettigo):

Fritzing Bill of Materials

Sketch: 	tranzistor.fz
Date: 	Mon Dec 14 10:30:33 2009

Arduino       Arduino Diecimila
Breadboard1   Generic Breadboard
LED1          Red LED - 5mm
LED2          Red LED - 5mm
LED3          Red LED - 5mm
LED4          Red LED - 5mm
Q1            NPN-Transistor
R1            220Ω Resistor
R2            220Ω Resistor
R3            220Ω Resistor
R4            220Ω Resistor
R5            220Ω Resistor

Shopping List

Quantity	Part

5		220Ω Resistor
1		Arduino Diecimila
1		Generic Bajillion Hole Breadboard
1		NPN-Transistor
4		Red LED

Of course, 220Ω resistors are the ones supplied with Arduino Starter Kit for LED diodes and may have other values – from 200Ω to 280Ω. Regardless of the values, this circuit will work.

Sketch for Arduino is classical example of Blink:

/*
 * Blink
 *
 * The basic Arduino example.  Turns on an LED on for one second,
 * then off for one second, and so on...  We use pin 13 because,
 * depending on your Arduino board, it has either a built-in LED
 * or a built-in resistor so that you need only an LED.
 *
 * http://www.arduino.cc/en/Tutorial/Blink
 */

int ledPin = 02;                // LED connected to digital pin 13

void setup()                    // run once, when the sketch starts
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin as output
}

void loop()                     // run over and over again
{
  digitalWrite(ledPin, HIGH);   // sets the LED on
  delay(4000);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off
  delay(2000);                  // waits for a second
}

At the end, the values measured in real circuit, for 280Ω resistors:

  • supply voltage – 4.96 V
  • UCE voltage – 0.03 V
  • voltage drop on lighted LEDs – from 1.86 to 1.93 V
  • current flowing through LEDs (without incorporating real resistance R2-R5, each of them has 5% tolerance) – from 10.7 mA to 10.9 mA

As homework – it is possible to change pin used to drive transistor to any supporting PWM and control brightness of the diodes using analogWrite instead of digitalWrite. Movie illustrating such a modification:

Driving over 40mA of current – PWM version from Starter Kit on Vimeo.

Today I wrote slightly long article, but I had to include some theory here. I tried hard to avoid inaccuracies, but analog techniques aren’t my strong point :) If you notice any errors, please let me know, I will try to correct them.

Shifted LEDs

The last piece of Arduino Starter Kit, which still remains undescribed is small, but powerful integrated circuit – shift register. It is 74HC595, SIPO type (Serial-In, Parallel-Out) shift register with 8 parallel outputs.

Shift register has serial input and some (usually 8) parallel outputs. What it means? With every clock impulse the output states are shifted by one position. So, logic level of the 1st output appears on the 2nd one. Content of the 1st output is changed depending on serial input content.

How can we take advantage of this?

We should consider using shift register in every place which needs many logic outputs. You can say that three pins can control many more outputs. Let’s see it on the simplest example – LED diodes.

This article is based on tutorial published on Arduino.cc, which is source of illustrations. These are depicting more than one breadboard, but I decided to use them to speed up writing this article – making a clear illustration in Fritzing is rather impossible.

Let’s begin from wiring it up:

Circuit diagram with shift register - assembled on breadboard

Circuit diagram with shift register - assembled on breadboard

Power is spreaded by rails on the breadboard. On the left board the LEDs are connected with resistors. On the right one is connected the 74HC595 itself.
Its pinout:

74HC595 pinout

74HC595 pinout

On the preceding diagram is shoved the pinout according to datasheet from manufacturer (ST Microelectronics), whose chips are in offer of Nettigo and are part of Starter Kit. On the Arduino site there is description suitable to ICs marked according to Philips nomenclature. What it means? Fortunately the only difference is signals naming (for example, outputs – in STM: QA to QH, in Philips version: Q0 to Q7), placing and functions of pins is the same. In this tutorial I will be using naming compliant with STM.

The simplest connection between Arduino and shift register looks like the following:

  • +5V power to register pin 16
  • Ground to register pin 8

If we don’t mind the fact that after turning the power on the LEDs may light in random configuration and we want to use less digital outputs, we can connect it like this:

  • G – register pin 13 to ground
  • SCLR – register pin 10 to ground

The minimal configuration to control a shift register from Arduino is (all Arduino pins are digital inputs):

  • SI – register pin 14 to Arduino pin 11
  • SCK – register pin 11 to Arduino pin 12
  • RCK – register pin 12 to Arduino pin 8

Between RCK pin and ground there should be capacitor (1 μF – not supplied with Starter Kit). A number of experiments I made prove that it isn’t necessary :)

Here is logic diagram of the connection:

Logic diagram of the connection

Logic diagram of the connection

What remains? The code:

//**************************************************************//
//  Name    : shiftOutCode, One By One                          //
//  Author  : Carlyn Maw, Tom Igoe                           //
//  Date    : 25 Oct, 2006                                      //
//  Version : 1.0                                               //
//  Notes   : Code for using a 74HC595 Shift Register           //
//          : to count from 0 to 255                            //
//****************************************************************

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

//holder for infromation you're going to pass to shifting function
byte data = 0; 

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

}

void loop() {

  //function that blinks all the LEDs
  //gets passed the number of blinks and the pause time
  blinkAll(1,500); 

 // light each pin one by one using a function A
  for (int j = 0; j < 8; j++) {
    lightShiftPinA(j);
    delay(1000);
  }

  blinkAll(2,500);  

 // light each pin one by one using a function A
  for (int j = 0; j < 8; j++) {
    lightShiftPinB(j);
    delay(1000);
  } 

}

//This function uses bitwise math to move the pins up
void lightShiftPinA(int p) {
  //defines a local variable
  int pin;

  //this is line uses a bitwise operator
  //shifting a bit left using << is the same
  //as multiplying the decimal number by two.
  pin = 1<< p;

  //ground latchPin and hold low for as long as you are transmitting
  digitalWrite(latchPin, LOW);
  //move 'em out
  shiftOut(dataPin, clockPin, MSBFIRST, pin);
  //return the latch pin high to signal chip that it
  //no longer needs to listen for information
  digitalWrite(latchPin, HIGH);

}

//This function uses that fact that each bit in a byte
//is 2 times greater than the one before it to
//shift the bits higher
void lightShiftPinB(int p) {
  //defines a local variable
  int pin;

  //start with the pin = 1 so that if 0 is passed to this
  //function pin 0 will light.
  pin = 1;

  for (int x = 0; x < p; x++) {
    pin = pin * 2;
  }

  //ground latchPin and hold low for as long as you are transmitting
  digitalWrite(latchPin, LOW);
  //move 'em out
  shiftOut(dataPin, clockPin, MSBFIRST, pin);
  //return the latch pin high to signal chip that it
  //no longer needs to listen for information
  digitalWrite(latchPin, HIGH);

}

//blinks the whole register based on the number of times you want to
//blink "n" and the pause between them "d"
//starts with a moment of darkness to make sure the first blink
//has its full visual effect.
void blinkAll(int n, int d) {
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, MSBFIRST, 0);
  digitalWrite(latchPin, HIGH);
  delay(200);
  for (int x = 0; x < n; x++) {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 255);
    digitalWrite(latchPin, HIGH);
    delay(d);
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, 0);
    digitalWrite(latchPin, HIGH);
    delay(d);
  }
}

This will light all the diodes, then light every diode one by one, flash all the LEDs twice and light again using another method.

The most important informations:

  • shiftOut is a function from Arduino library, serially sending selected byte to specified output
  • we can send data to the register by setting RCK (connected to Arduino pin 8) in low state