174k views
3 votes
Write Arduino code to shift data into the shift register and light up the LEDs sequentially, with a delay of 1 second between each light. Make sure that at most 1 LED can be ON at any moment.

User Meez
by
5.4k points

1 Answer

6 votes

The Arduino language is very similar to the one found in C language.

In order to make a counter e.g 0 to 255. It is necessary to take into consideration that is needed at least 8 LEDs.

For this, it can be used the IC 74HC595 counter. The datasheet you can find easily in the internet

Code

//write the following code into your Arduino IDE

//Input plugged to ST_CP of IC

int latchInput = 8;

//Input plugged to SH_CP of IC

int clockInput = 12;

////Input plugged to DS of IC

int dataInput = 11;

void

setup ()

{

Serial.begin (9600);

InputMode (latchInput, OUTPUT);

}

void

loop ()

{

//count up routine

for (int j = 0; j < 256; j++)

{

//ground latch Input and hold low for as long as you are transmitting

digitalWrite (latchInput, 0);

//countup on GREEN LEDs

shiftOut (dataInput, clockInput, j);

//countdown on RED LEDs

shiftOut (dataInput, clockInput, 255 - j);

//return the latch Input high to signal chip that it

digitalWrite (latchInput, 1);

delay (1000);

}

}

void

shiftOut (int myDataInput, int myClockInput, byte myDataOut)

{

// This shifts 8 bits out MSB first,

//on the rising edge of the clock,

//clock idles low

.. //internal function setup

int i = 0;

int InputState;

InputMode (myClockInput, OUTPUT);

InputMode (myDataInput, OUTPUT);

. //clear everything out just in case to

. //prepare shift register for bit shifting

digitalWrite (myDataInput, 0);

digitalWrite (myClockInput, 0);

//for each bit in the byte myDataOut&#xFFFD;

//NOTICE THAT WE ARE COUNTING DOWN in our for loop

//This means that %00000001 or "1" will go through such

//that it will be Input Q0 that lights.

for (i = 7; i >= 0; i--)

{

digitalWrite (myClockInput, 0);

//if the value passed to myDataOut and a bitmask result

// true then... so if we are at i=6 and our value is

// %11010100 it would the code compares it to %01000000

// and proceeds to set InputState to 1.

if (myDataOut & (1 << i))

{

InputState = 1;

}

else

{

InputState = 0;

}

//Sets the Input to HIGH or LOW depending on InputState

digitalWrite (myDataInput, InputState);

//register shifts bits on upstroke of clock Input

digitalWrite (myClockInput, 1);

//zero the data Input after shift to prevent bleed through

digitalWrite (myDataInput, 0);

}

//stop shifting

digitalWrite (myClockInput, 0);

}

User DmitryK
by
5.9k points