Arduino UNO Tutorial 4 - Simple Cylon

Arduino UNO Tutorial 4 - Simple Cylon

In this Arduino UNO tutorial, we are expanding on our simple LED flash tutorial. We will use 5 LEDs to create a simple Cylon roving eye effect. To do this we will make life easier for ourselves and use direct port manipulation. That is we will write directly to one of the Arduino UNO ports rather than writing to individual pins. This allows us to set the values for each of the LEDs in one operation.

The Arduino UNO has 3 ports

  • B (digital pin 8 to 13)
  • C (analog input pins)
  • D (digital pins 0 to 7)

Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode()

We will use port B for our Cylon eye sketch. First we need to set all the port B pins as digital outputs. Port B has only six pins allocated to it. The bits in the port B register (DDRB) control whether each of the pins in PORTB are configured as inputs or outputs, Setting a pin to 1 makes it an OUTPUT whilst setting it to 0 makes it an INPUT. Port pins are numbered from 0 to 7 but not all ports have 8 pins. So, for example:

DDRB = B00111110; // sets Arduino port B pins 1 to 5 as outputs, pin 0 as input

NOTE:If you have used any of Microchips microcontrollers they use the opposite strategy, where 0 is OUTPUT and 1 is INPUT

For our Cylon eye we will use port B pins 0 to 4, so we set them as outputs with

DDRB = B00011111; // sets Arduino port B pins 0 to 4 as outputs

To write to our port we use the PORTB register. We can turn on just the first LED with

PORTB = B00000001;

or we can turn on alternate LEDs with

PORTB = B00010101;

See how much easier this is than turning on/off each LED. Now for some easy binary Bit Shift Operators

There are two bit shift operators: the left shift operator << and the right shift operator >>. These operators cause the bits in the left operand to be shifted left or right by the number of positions specified by the right operand.

variableA = 1;      // 00000001
variableA = 1 << 0; // 00000001
variableA = 1 << 1; // 00000010
variableA = 1 << 2; // 00000100

Notice how the single digit 1 is moving up the number. This is the exact effect we need for our Arduino Cylon eye sketch

Now to the code, which is shown below. We need a couple of variables, one (upDown) to indicate whether we are moving up or down the port LEDs and another (cylon) to indicate which LED should be illuminated.

In setup() we define the port pins required as OUTPUTs.

In the main loop(), if we are going up the LEDs we increase the cylon variable by 1 and test to see if we have reached the last LED, if we have then we set the upDown variable to be going down. Similarly, if we are going down we decrease the cylon variable by 1 and test to see if we have reached the first LED, if we have then we set the upDown variable to be going up.

We then light up the correct LED and wait a sort while before starting again.

  Simple Cylon
  Cylon Eye sweep using 5 LEDs

unsigned char upDown=1;	// start off going UP	
unsigned char cylon=0;  // determines which LED is on 0 to 4

void setup() {                
  // initialize the digital pins as outputs.
  DDRB = B00011111;  // sets Arduino port B pins 0 to 4 as outputs	   

void loop() {
    if(cylon>=4) upDown=0;      // Reached max LED, next time we need to go down
  else {
    if(cylon==0) upDown=1;      // Reached min LED, next time we need to go up
  PORTB = 1 << cylon;
  delay(150);              // wait for a second

Here is the circuit laid out on a breadboard

Arduino Simple Cylon Breadboard

Tutorial 3 - Timing

Tutorial 5 - Fade