Go Down

Topic: Help with two 7-segment displays & pot code (Read 3 times) previous topic - next topic


:)Hello, I wonder if someone could help me change this piece of code to read a value of 0-99 from the pot on two 7-segment displays. Here is the sketch
Code: [Select]
// Digital pins connected to 74HC595
int latchPin = 8;   // pin  12 on the 74hc595
int clockPin = 12;  // pin 11 on the 74hc595
int dataPin = 11;   // pin 14 on the 74hc595

// Analog input pin
int potPin = 0;

void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);

const byte ledCharSet[128] = {
// 00-0F: Hex digits
B01111110, B00110000, B01101101, B01111001, // 0123
B00110011, B01011011, B01011111, B01110000, // 4567
B01111111, B01111011, B01110111, B00011111, // 89AB
B01001110, B00111101, B01001111, B01000111, // CDEF

// 10-1F: Figure-8 drawing (8-character cycle)
B01000000, B00100000, B00000001, B00000100, // 1-segment
B00001000, B00010000, B00000001, B00000010,

B01100000, B00100001, B00000101, B00001100, // 2-segment
B00011000, B00010001, B00000011, B01000010,

// 20-2F: Punctuation (barely recognizable!)
B00000000, B10100000, B00100010, B00111111, //  !"#
B01011010, B01001001, B00000111, B00000010, // $%&'
B01001110, B01111000, B01100011, B00110001, // ()*+
B00010000, B00000001, B10000000, B00100101, // ,-./

// 30-3F: Decimal digits (alternate) and more punctuation
B01111110, B00110000, B01101101, B01111001, // 0123
B00110011, B01011011, B00011111, B01110010, // 4567
B01111111, B01110011, B01001000, B01010000, // 89:;
B00001101, B00001001, B00011001, B11100000, // <=>?

// 40-5F: Capital letters and punctuation
B01101110, B01110111, B00011111, B01001110, // @ABC
B01111100, B01001111, B01000111, B01011110, // DEFG
B00110111, B00000110, B00111100, B01010111, // HIJK
B00001110, B01110110, B00010101, B01111110, // LMNO

B01100111, B01110011, B01000110, B01011011, // PQRS
B01110000, B00111110, B00111110, B00101011, // TUVW
B01110101, B00111011, B01101101, B01001110, // XYZ[
B00010011, B01111000, B01100010, B00001000, // \]^_

// 60-7F: Lowercase letters and punctuation
B01100000, B01111101, B00011111, B00001101, // `abc
B00111101, B01101111, B01000111, B01111011, // defg
B00010111, B00010000, B00011000, B00101111, // hijk
B00001100, B01010101, B01101010, B00011101, // lmno

B01100111, B01110011, B00000101, B00010011, // pqrs
B00001111, B00011100, B00100011, B01011101, // tuvw
B01101100, B00111011, B00100101, B01000011, // xyz{
B00110110, B01100001, B01000000, B11111111 // |}~
void loop() {
int i, j;

// Read the value from the sensor and convert to 7-bit value
i = analogRead(potPin) >> 3;

digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, MSBFIRST, ledCharSet[i]);
digitalWrite(latchPin, 1);
I haven't used two 595's together before and so I'm not sure how the code has to change to incorporate them. Any help appreciated :)


You need two shiftOut commands, one for each digit.
Split up the pot into two bytes, one for each digit. You might like to try doing it in HEX first as this is easier. Then you do your shiftOut twice with the different digit as the array index.


Apr 23, 2011, 09:15 am Last Edit: Apr 23, 2011, 09:42 am by GoatBoy Reason: 1
:)Thanks for your help Mike! I found another piece of code more suitable and with Hex values in arrays. Before I get into the shenanigans of using two 7-segment displays, I thought I'd better make sure I had one display working with a pot first. My problem with this code is that I can only display numbers 1-9. Why can I not get "0"? What should I change? Thanks :)
Code: [Select]
//Pin connected to ST_CP of 74HC595hen
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
int j;
int val;
//holders for infromation you're going to pass to shifting function
byte data;
byte dataArray[10];

void setup() {
 //set pins to output because they are addressed in the main loop
 pinMode(latchPin, OUTPUT);

 dataArray[0] = 0x3F; // 0
 dataArray[1] = 0x06; // 1
 dataArray[2] = 0x5B; // 2
 dataArray[3] = 0x4F; // 3
 dataArray[4] = 0x66; // 4
 dataArray[5] = 0x6D; // 5
 dataArray[6] = 0x7C; // 6
 dataArray[7] = 0x07; // 7
 dataArray[8] = 0x7F; // 8
 dataArray[9] = 0x67; // 9

 data = dataArray[0];
 //ground latchPin and hold low for as long as you are transmitting
 digitalWrite(latchPin, 0);
 //move 'em out
 shiftOut(dataPin, clockPin, data);
 //return the latch pin high to signal chip that it
 //no longer needs to listen for information
 digitalWrite(latchPin, 1);

void loop(){
val = map(analogRead (0), 0, 1024, 0, 10);

   if (j=val)
data = dataArray[j];
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
//move 'em out
shiftOut(dataPin, clockPin, data);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, 1);

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, 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 pinState;
 pinMode(myClockPin, OUTPUT);
 pinMode(myDataPin, OUTPUT);

 //clear everything out just in case to
 //prepare shift register for bit shifting
 digitalWrite(myDataPin, 0);
 digitalWrite(myClockPin, 0);

 //for each bit in the byte myDataOut?
 //This means that %00000001 or "1" will go through such
 //that it will be pin Q0 that lights.
 for (j=7; j>=0; j--)  {
   digitalWrite(myClockPin, 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 pinState to 1.
   if ( myDataOut & (1<<j) ) {
pinState= 1;
   else {
pinState= 0;

   //Sets the pin to HIGH or LOW depending on pinState
   digitalWrite(myDataPin, pinState);
   //register shifts bits on upstroke of clock pin
   digitalWrite(myClockPin, 1);
   //zero the data pin after shift to prevent bleed through
   digitalWrite(myDataPin, 0);

 //stop shifting
 digitalWrite(myClockPin, 0);



Code: [Select]
if (j=val)

That's why you can't get zero.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.


:)Thanks AWOL. Not sure what to do still. Is it correct to use an "if" statement there though?

Go Up