Get 8 LEDs to light up in a sequence using a shift register.
Equipment:
•1 x Arduino Uno
•1 x Breadboard
•20 x Wire
•8 x LED
•1 x Shift Register 74HC959
•8 x 560 Ohm Resistor
Reference:
http://www.oomlout.com/a/products/ardx/circ-05
Program Details:
Shift register and shift bit values were introduced in the code.
Time to Complete:
25 minutes
Results:
The circuit was built and the code was created, but it didn't work. After inspecting the circuit, we found that the 5V and ground wires were switched around by accident. Once the wires were changed, the LEDs lit up in a sequence.
Photos of Project:
Tips:
Again, watch out for the positive and negative parts of the LED. There's many parts to this lab, try to put the same piece of equipment consistently spaced out.
Further Work:
Make a different sequence rather than the one provided by the website.
Program Modifications:
The program is exactly the one from http://www.oomlout.com/a/products/ardx/circ-05
Program:
void loop() // continuously runs
{
int delayTime = 100; //sets a delay time
for(int i = 0; i < 256; i++){
updateLEDs(i);
delay(delayTime); //delay equal to the value of delayTime
}
}
//used to set three control pins
int data = 2;
int clock = 3;
int latch = 4;
int data = 2;
int clock = 3;
int latch = 4;
//Sets to variables as constants
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;
//initializes the three control pins as outputs
void setup()
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
}
void setup()
{
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
}
void loop() // continuously runs
{
int delayTime = 100; //sets a delay time
for(int i = 0; i < 256; i++){
updateLEDs(i);
delay(delayTime); //delay equal to the value of delayTime
}
}
/*creates a void-type function, updateLEDs. Which Receives a parameter of type int from loop()*/
void updateLEDs(int value){
digitalWrite(latch, LOW); //changes the state of latch to low
shiftOut(data, clock, MSBFIRST, value);
digitalWrite(latch, HIGH); // changes the state of latch to high
}
shiftOut(data, clock, MSBFIRST, value);
digitalWrite(latch, HIGH); // changes the state of latch to high
}
void updateLEDsLong(int value){
digitalWrite(latch, LOW); //changes the state of latch to low
for(int i = 0; i < 8; i++){ //loop that will repeat 8 times
int bit = value & B10000000;
value = value << 1;
if(bit == 128){digitalWrite(data, HIGH);} /*if bit is set to 128 then the state of data will be changed to HIGH */
else{digitalWrite(data, LOW);} /*if bit isn't set to 128 then the state of data will be changed to LOW */
digitalWrite(clock, HIGH); //sets the state of clock to High
delay(1);
digitalWrite(clock, LOW); //sets the state of clock to High
}
digitalWrite(latch, HIGH); ////sets the state of latch to High }
digitalWrite(latch, LOW); //changes the state of latch to low
for(int i = 0; i < 8; i++){ //loop that will repeat 8 times
int bit = value & B10000000;
value = value << 1;
if(bit == 128){digitalWrite(data, HIGH);} /*if bit is set to 128 then the state of data will be changed to HIGH */
else{digitalWrite(data, LOW);} /*if bit isn't set to 128 then the state of data will be changed to LOW */
digitalWrite(clock, HIGH); //sets the state of clock to High
delay(1);
digitalWrite(clock, LOW); //sets the state of clock to High
}
digitalWrite(latch, HIGH); ////sets the state of latch to High }
//Creates an array, bits
int bits[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000};
//Creates an array, marks
int masks[] = {B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111};
int masks[] = {B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111};
//Creates a void-type function with to int type parameters
void changeLED(int led, int state){
ledState = ledState & masks[led];
ledState = ledState & masks[led];
if(state == ON){ledState = ledState | bits[led];}
updateLEDs(ledState); //sends ledState to updateLEDs
}
updateLEDs(ledState); //sends ledState to updateLEDs
}
No comments:
Post a Comment