7 Seg. and 74HC595 - Page 1
Forum section: electronics / beginners

author: Brown
author_rank: Contributor
timestamp: January 04, 2014, 06:24:48 am
content: This is some Arduino code to cycle 0-9 on a 7 seg. display. Can someone explain how this works in a dumb down version, I get the first part up to pinMode(faderPin, OUTPUT); but not the rest. Thanks!const byte ledCharSet ={// 00-0F: Hex digitsB01111110, B00110000, B01101101, B01111001, // 0123B00110011, B01011011, B01011111, B01110000, // 4567B01111111, B01111011, // 89};//// Pin connected to latch pin (ST_CP) of 74HC595const int latchPin = 8;//// Pin connected to clock pin (SH_CP) of 74HC595const int clockPin = 12;//// Pin connected to Data in (DS) of 74HC595const int dataPin = 11;//// Pin connected to display's common annodeconst int faderPin = 10;///////////////////////////////////////////////////////////////////////////////////void setup(){pinMode(latchPin, OUTPUT);pinMode(clockPin, OUTPUT);pinMode(dataPin, OUTPUT);pinMode(faderPin, OUTPUT);}///////////////////////////////////////////////////////////////////////////////////void loop(){for(int i=0; i <= 128; ++i){byte bitsToSend = ledCharSet;// invert bitmas - we're using a common ANODE display.// for common cathode, comment out the following line.bitsToSend = bitsToSend ^ B11111111;// turn off the output so the pins don't light up// while you're shifting bits:digitalWrite(latchPin, LOW);// shift the bits out:shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend);// turn on the output so the LEDs can light up:digitalWrite(latchPin, HIGH);fadeInOut();}}///////////////////////////////////////////////////////////////////////////////////void fadeInOut(){for(int i = 0; i < 255; ++i){analogWrite(faderPin, i);delay(01);}for(int i = 255; i > 0; --i){analogWrite(faderPin, i);delay(01);}}

author: Rerouter
author_rank: Super Contributor
timestamp: January 04, 2014, 06:57:02 am
content: you can treat the faderpin as contrast, its writing a PWM voltage onto that pin, and as that is the common anode it lowers or increases the voltage across the leds,really it is a dumb idea as the arduino pins can only handle 40mA max, and it would not be hard to exceed this.i also agree the for loop is a bit weird, and off the top of my head i am not really seeing how it could work

author: sachleen
author_rank: Contributor
timestamp: January 04, 2014, 06:59:56 am
content: I think that's supposed to beCode:byte bitsToSend = ledCharSet;and then the loop should only be 0 to 9, of course.

author: sleemanj
author_rank: Super Contributor
timestamp: January 04, 2014, 07:18:34 am
content: NB: When pastign code you should wrap it in the code forum BB tags (see the # icon in the forum post editor), because you didn't part of your code was not visible (the array indexing).Code:const byte ledCharSet ={// 00-0F: Hex digitsB01111110, B00110000, B01101101, B01111001,// 0123B00110011, B01011011, B01011111, B01110000,// 4567B01111111, B01111011,// 89};This is an array of bits to be set on the shift register output in order to display each character (note that they are inverted later on, 1's become 0's and vice-versa), the array is defined as 128 bytes, they are only using 10, the "Bxxxxxxxx" is a way to enter a byte as a binary string specifying each bit separatelyCode:void loop(){for(int i=0; i <= 128; ++i){byte bitsToSend = ledCharSet;For each of the bytes in the array already definedCode:// invert bitmas - we're using a common ANODE display.// for common cathode, comment out the following line.bitsToSend = bitsToSend ^ B11111111;Invert the bits, eg 01111110 becomes 10000001Code:// turn off the output so the pins don't light up// while you're shifting bits:digitalWrite(latchPin, LOW);// shift the bits out:shiftOut(dataPin, clockPin, LSBFIRST, bitsToSend);// turn on the output so the LEDs can light up:digitalWrite(latchPin, HIGH);Turn the outputs of the shift register off , shift the 8 bits into the register, and turn it on againShould have read the code not the comment, they are putting latch low, shifting in data, and putting latch high. The outputs do not switch off (but they retain the previously latched data until it's relatched).Code:fadeInOut();Fade the display in, and then out again

author: Brown
author_rank: Contributor
timestamp: January 10, 2014, 07:56:08 pm
content: Thanks guys! Sorry for the late reply. Thanks!