Sunday, June 26, 2016

Playing with 24 LEDS using three 74HC595 and Intel EDISON


INTRODUCTION:
Here I present a simple circuit to control 24 LED using three 74HC595. This circuit is going to be controlled using an Intel Edison. At the end of this article you can find the codes. One code is for programming the board using the Arduino IDE and the other is a C++ code to compile with Yocto-Linux.
If you want to know more about the 74HC595 and serial to parallel circuits, I highly recommend to read the following article: https://www.arduino.cc/en/Tutorial/ShiftOut
 
COMPONENTS: 
8 x Red LED
8 x Green LED
8 x Yellow LED
24 x 1K 1/4w resistor
3 x 74HC595
3x 100nF Capacitor
1 x 5 pin strip
Wires, lead…
In the next picture   it is shown my testing board which has all the components described before and the circuit follows the schematic that you can find below.
   
     
SCHEMATIC:
     


VIDEO:
      



C++ CODE:

/*     ---------------------------------------------------------
*     |  Intel Edison C++ 3x74HC595 Shift Register Example Code |
*     |   .: 24 More LEDs :. (74HC595 Shift Register)           |
*     ---------------------------------------------------------
*makefile: g++ -lmraa –o 24_LEDS 24_LEDS.cpp
*/
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <mraa/gpio.h>

//Pin Definitions
//The 74HC595 uses a serial communication
//link which has three pins
#define DIR_CLOCK 4
#define DIR_EN        7
#define DIR_SER   8
#define DIR_LATCH 12

#define LOW  0
#define HIGH 1
#define LSBFIRST 0
#define MSBFIRST 1

using namespace std;

sig_atomic_t volatile isrunning = 1;
void sig_handler(int signum);
void shiftOut(mraa_gpio_context dataPin, mraa_gpio_context clockPin, uint8_t bitOrder, uint8_t val);

int main()
{
       signal(SIGINT, &sig_handler);

       mraa_init();

       mraa_gpio_context data_pin;
       mraa_gpio_context clock_pin;
       mraa_gpio_context latch_pin;
       mraa_gpio_context enable_pin;

       data_pin = mraa_gpio_init(DIR_SER);
       clock_pin= mraa_gpio_init(DIR_CLOCK);
       latch_pin= mraa_gpio_init(DIR_LATCH);
       enable_pin = mraa_gpio_init(DIR_EN);

       mraa_gpio_dir(data_pin,  MRAA_GPIO_OUT);
       mraa_gpio_dir(clock_pin, MRAA_GPIO_OUT);
       mraa_gpio_dir(latch_pin, MRAA_GPIO_OUT);
       mraa_gpio_dir(enable_pin, MRAA_GPIO_OUT);

       while (isrunning)
       {
             int delayTime = 100000; //the number of microseconds to delay between LED updates
            
             printf("Testing Shift register up 255...\n");
             for (int i = 0; i < 256; i++)
             {
                    mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
                    shiftOut(data_pin, clock_pin, MSBFIRST, i); //Shifts out the 8 bits to the shift register
                    mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
                    usleep(delayTime/2);//0.05 seconds
                    printf("%d\n", i);
             }
             usleep(delayTime * 10);//1 seconds

             printf("Only red LEDs...\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             usleep(delayTime * 20);//2 seconds

             printf("Only yellows LEDS...\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             usleep(delayTime * 20);//2 seconds

             printf("Only green LEDS...\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             usleep(delayTime * 20);//2 seconds

             printf("Only M1:00001100 \tM1A=HIGH\n\t\t\tM1B=HIGH\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 12); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             usleep(delayTime * 20);//2 seconds

             printf("Only M2:00010010 \tM2A=HIGH\n\t\t\tM2B=HIGH\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 18); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             usleep(delayTime * 20);//2 seconds

             printf("Only M3:01000001 \tM3A=HIGH\n\t\t\tM3B=HIGH\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 65); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             usleep(delayTime * 20);//2 seconds

             printf("Only M4:10100000 \tM4A=HIGH\n\t\t\tM4B=HIGH\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 160); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             usleep(delayTime * 20);//2 seconds

             printf("Sleep 5secs\n");
             sleep(5);//5 seconds
             printf("Sleep 5secs\n");
             sleep(5);//5 seconds
            
             printf("M4+M3: 11100001, 160 | 65\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 160 | 65); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             usleep(delayTime * 20); //2 seconds

             printf("M4+M3+M2: 11110011, 160 | 65 | 18\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 160 | 65 | 18); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             usleep(delayTime * 20);//2 seconds

             printf("(M4+M3+M2)-M4: 01010011, (65 | 18) ^ 160\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, (160|65 | 18) ^ 160); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             usleep(delayTime * 20);//2 seconds
            
             printf("All LEDS ON\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             sleep(2);//2 second

             printf("All LEDS OFF\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             sleep(2);//2 second
            
             printf("ALl LEDS ON\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             sleep(2);//2 second

             printf("All LEDS OFF\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             sleep(2);//2 second
            
             printf("sweeping all LEDS to the left \n");
             for (int i = 1; i < 4; i++)
             {
                    int t = 1;
                    for (int j = 0; j < 8; j++)
                    {           
                           mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
                           if (i == 2)
                           {
                                  shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, MSBFIRST, t); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           }
                           else if (i == 3)
                           {
                                  shiftOut(data_pin, clock_pin, MSBFIRST, t); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           }
                           else
                           {
                                  shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, MSBFIRST, t); //Shifts out the 8 bits to the shift register
                           }
                           mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
                           usleep(delayTime);
                           t = t*2;
                           printf("t= %d\n",t);
                    }
             }
             sleep(2);//2 second

             printf("LEDS OFF\n");
             mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             shiftOut(data_pin, clock_pin, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
             mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
             sleep(2);//2 second
            
             printf("sweeping all LEDS to the right \n");
             for (int i = 1; i < 4; i++)
             {
                    int t = 1;
                    for (int j = 0; j < 8; j++)
                    {
                           mraa_gpio_write(latch_pin, LOW);     //Pulls the chips latch low  
                           if (i == 2)
                           {
                                  shiftOut(data_pin, clock_pin, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, LSBFIRST, t); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           }
                           else if (i == 3)
                           {                                
                                  shiftOut(data_pin, clock_pin, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, LSBFIRST, t); //Shifts out the 8 bits to the shift register
                           }
                           else
                           {
                                  shiftOut(data_pin, clock_pin, LSBFIRST, t); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                                  shiftOut(data_pin, clock_pin, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           }
                           mraa_gpio_write(latch_pin, HIGH); //Pulls the latch high displaying the data
                           usleep(delayTime);
                           t = t * 2;
                           printf("t= %d\n", t);
                    }
             }
             sleep(2);//2 second
            
       }
       return MRAA_SUCCESS;
}

void sig_handler(int signum)
{     
       if (signum == SIGINT) isrunning = 0;
}

void shiftOut(mraa_gpio_context dataPin, mraa_gpio_context clockPin, uint8_t bitOrder, uint8_t val)
{
       uint8_t i;

       for (i = 0; i < 8; i++) {
             if (bitOrder == LSBFIRST)
                    mraa_gpio_write(dataPin, !!(val & (1 << i)));
             else
                    mraa_gpio_write(dataPin, !!(val & (1 << (7 - i))));

             mraa_gpio_write(clockPin, HIGH);
             mraa_gpio_write(clockPin, LOW);
       }
}




ARDUINO CODE:




/*  Playing with 24 LEDS AN Intel Edison
*/

//Pin Definitions
//The 74HC595 uses a serial communication
//link which has three pins
int data = 8;
int clock = 4;
int latch = 12;

//Used for single LED manipulation
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;

void setup()
{
       pinMode(data, OUTPUT);
       pinMode(clock, OUTPUT);
       pinMode(latch, OUTPUT);
       Serial.begin(115200);
}

void loop()                     // run over and over again
{
       int delayTime = 100; //the number of milliseconds to delay between LED updates

       Serial.print("Testing Shift register up 255...\n");
       for (int i = 0; i < 256; i++)
       {
             updateLEDs(i);
             delay(delayTime / 2);
       }
       delay(1000);

       Serial.print("Only red LEDs...\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("Only yellows LEDS...\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("Only green LEDS...\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("Only M1:00001100 \tM1A=HIGH\n\t\t\tM1B=HIGH\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("Only M2:00010010 \tM2A=HIGH\n\t\t\tM2B=HIGH\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("Only M3:01000001 \tM3A=HIGH\n\t\t\tM3B=HIGH\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("Only M4:10100000 \tM4A=HIGH\n\t\t\tM4B=HIGH\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("M4+M3: 11100001, 160 | 65\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("M4+M3+M2: 11110011, 160 | 65 | 18\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("(M4+M3+M2)-M4: 01010011, (65 | 18) ^ 160\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("All LEDS ON\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("All LEDS OFF\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("ALl LEDS ON\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("All LEDS OFF\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 255); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);

       Serial.print("sweeping all LEDS to the left \n");
       for (int i = 1; i < 4; i++)
       {
             int t = 1;
             for (int j = 0; j < 8; j++)
             {
                    digitalWrite(latch, LOW);     //Pulls the chips latch low
                    if (i == 2)
                    {
                           shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, MSBFIRST, t); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                    }
                    else if (i == 3)
                    {
                           shiftOut(data, clock, MSBFIRST, t); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                    }
                    else
                    {
                           shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, MSBFIRST, t); //Shifts out the 8 bits to the shift register
                    }
                    digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
                    delay(delayTime);
                    t = t * 2;
                    Serial.print("t= ");
                    Serial.println(DEC, t);
             }
       }
       delay(2000);//2 second

       Serial.print("LEDS OFF\n");
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       shiftOut(data, clock, MSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
       delay(2000);//2 second

       Serial.print("sweeping all LEDS to the right \n");
       for (int i = 1; i < 4; i++)
       {
             int t = 1;
             for (int j = 0; j < 8; j++)
             {
                    digitalWrite(latch, LOW);     //Pulls the chips latch low
                    if (i == 2)
                    {
                           shiftOut(data, clock, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, LSBFIRST, t); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                    }
                    else if (i == 3)
                    {
                           shiftOut(data, clock, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, LSBFIRST, t); //Shifts out the 8 bits to the shift register
                    }
                    else
                    {
                           shiftOut(data, clock, LSBFIRST, t); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                           shiftOut(data, clock, LSBFIRST, 0 >> 8); //Shifts out the 8 bits to the shift register
                    }
                    digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
                    delay(delayTime);
                    t = t * 2;
                    Serial.print("t= ");
                    Serial.println(DEC, t);
             }
       }
       delay(2000);//2 second  
}


/*
* updateLEDs() - sends the LED states set in ledStates to the 74HC595
* sequence
*/
void updateLEDs(int value)
{
       digitalWrite(latch, LOW);     //Pulls the chips latch low
       shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register
       digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
}

No comments:

Post a Comment