Step 5Project 1[pt 2]: '2 Wire' bargraph LED display controller software
Open the file " _164_bas_ex.pde" Inside the arduino IDE, Its a simple sketch that just lets you define on or off LED's in the bargraph display
The first 2 lines define the pin numbers we will be using for data and clock, I use #define over const integer, I find it easier to remember, and there is no advantage to one or the other once compiled
#define data 2
#define clock 3
next is the void setup function, it only runs once, so the arduino turns on, sets the shift register and has nothing else to do. Inside the void setup function we set the clock and data pins as OUTPUT pins, then using the shiftOut function we send the data to the shift register
void setup()
{
pinMode(clock, OUTPUT); // make the clock pin an output
pinMode(data , OUTPUT); // make the data pin an output
shiftOut(data, clock, LSBFIRST, B10101010); // send this binary value to the shift register
}
In the shiftOut function you can see its arguments
data is the data pin, clock is the clock pin
LSBFIRST refers to what order its in, when writing it out in binary notation (Bxxxxxxxx) the 7th element past the B is the Least Signifigant Bit First, this is fed in first so it ends up on the last output once all 8 bits are fed in
B10101010 is the Binary value being sent to the shift register, and it will turn on every odd light, try playing with different values to turn on or off different patterns
and finally a empty void loop (because you need one even if your not using it)
void loop(){} // empty loop for now
Example 2:
the first 8 lines are the same as the first 8 lines of the first example, in fact they will not change for any of the other projects, so
#define data 2
#define clock 3
void setup()
{
pinMode(clock, OUTPUT); // make the clock pin an output
pinMode(data , OUTPUT); // make the data pin an output
But now in void setup there is an 8 count for loop, its taking an empty byte and shifting 1 bit in at a time starting from the leftmost bit and moving right. This is backwards from the first example where we started from the rightmost bit and worked left, but using MSBFIRST the shift out function sends the data the correct way
Also we add a delay in the for loop so it slows down enough to be visible.
for(int i = 0; i < 8; ++i) //for 0 - 7 do
{
shiftOut(data, clock, MSBFIRST, 1 << i); // bit shift a logic high (1) value by i
delay(100); // delay 100ms or you would not be able to see it
}
}
void loop(){} // empty loop for now
upload the script and you should now see the bargraph light up each light one at a time
| « Previous Step | Download PDFView All Steps | Next Step » |



















![Project 1[pt 2]: \](/image/FGMBT2PG1ZGOS4F/Project-1pt-2-2-Wire-bargraph-LED-display-con.jpg)





























thank you :)