Help with two 7-segment displays & pot code

:)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

// 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 :slight_smile:

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.

:)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 :slight_smile:

//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);
  Serial.begin(9600);

 
  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);
	j++;
    }
  }
 

// 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?
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //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);

}
if (j=val)

That's why you can't get zero.

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

Yes, an if is fine, just don't do an assignment in it.

:)Sorry AWOL, could you give me an example? Should I be changing what follows my "if"?

Yes, you should be changing it from an assignment to a comparison.

:)Cool!! Thanks for your help AWOL!! This what I did

void loop(){

    if (j= map(analogRead (0), 0, 1024, 0, 10));
    {
	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);

  }

And that gets you a zero?

You'll have to explain how this:if (j= map(analogRead (0), 0, 1024, 0, 10));

differs from this:

val = map(analogRead (0), 0, 1024, 0, 10);
  
    if (j=val)

Edi: Yes, I suppose it does get you your zero. Odd way of going about it though.
if (j= map(analogRead (0), 0, 1024, 0, 10));

:slight_smile: Yes, I am getting the zero that I wanted now. I see your point though, essentially its still the same no? and I thought for a moment there, I knew what I was doing!!!!! :slight_smile: However, I was getting the zero before but only when the sketch uploaded and when the pot was at one extreme. As soon as I swept the pot up and down, "1" was the minimum that I could get! So the small change I made appeared to have solved this. After that, I took out other parts of the code that didn't appear to be needed and ended up with this...

//**************************************************************//
//  Name    : shiftOutCode, Predefined Array Style		  //
//  Author  : Carlyn Maw, Tom Igoe				   //
//  Date    : 25 Oct, 2006						  //
//  Version : 1.0							     //
//  Notes   : Code for using a 74HC595 Shift Register	     //
//	    : to count from 0 to 255				    //
//****************************************************************

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;
int j;
//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);
  Serial.begin(9600);

 
  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


}

void loop(){

    if (j= map(analogRead (0), 0, 1023, 0, 10));
    {
	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 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?
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //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);

}

So the small change I made appeared to have solved this

Yes, "appeared" is exactly le mot juste.

You've changed the logic of your original sketch, so maybe it only appears to work.

Did you look carefully at my last post (hint: highlight)?

hmmmm.... I see that but I'm afraid I don't really understand what is happening there. I took out the ";" to see what would happen and I don't get my zero! Why is that AWOL? What would be a more suitable way of writing that possibly? I'm afraid "odd way" is my default setting! :slight_smile:

When you do an if:-
if(j= map(analogRead (0), 0, 1023, 0, 10))

it should be
if(j== map(analogRead (0), 0, 1023, 0, 10))

I tried that ...if(j== map(analogRead (0), 0, 1023, 0, 10))....and she stays stuck on zero! For the code that I have it only works this way...if(j= map(analogRead (0), 0, 1023, 0, 10));

For the code that I have it only works this way...if(j= map(analogRead (0), 0, 1023, 0, 10));

Then you don't understand how a program works. Please re read the page on the if statement and then re write your code. At your delicate state of knowledge NEVER use a single = inside an if statement. If you require that statement then perform it outside the if statement and use the if statement properly.

Extremely delicate!!!! :slight_smile:

:slight_smile: Hello there. This is the result of my understand so far on using the "if" statement.....

 if ((i==2) && (h==2)){
     mapMode == 11;
                 syncPhaseInc = mapPentatonic11(analogRead(SYNC_CONTROL));
   }

it's working for me.

And yes I managed to get three 7-segment displays working together. Two with one pot to select a musical scale (0-99 "i") and a separate one to select the key of that scale (A-G "h"). In case anyone has a need for something similar here it is (three 7-segment common cathode displays using three 595's).

int dataPin = 11;
int clockPin = 12;
int latchPin = 8;

byte dataRED;
byte dataArrayRED[10];
byte dataArray2[7];



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

         
  dataArrayRED[0] = 0x3F; //00111111 - 0
  dataArrayRED[1] = 0x06; //00000110 - 1
  dataArrayRED[2] = 0x5B; //01011011 - 2
  dataArrayRED[3] = 0x4F; //01001111 - 3
  dataArrayRED[4] = 0x66; //01100110 - 4
  dataArrayRED[5] = 0x6D; //01101101 - 5
  dataArrayRED[6] = 0x7D; //01111101 - 6
  dataArrayRED[7] = 0x07; //00000111 - 7
  dataArrayRED[8] = 0x7F; //01111111 - 8
  dataArrayRED[9] = 0x6F; //01100111 - 9
  
  // Key Selection
  dataArray2[0] = B01110111; // A  Check display wiring, this has been modified!
  dataArray2[1] = B01111100; // b
  dataArray2[2] = B00111001; // C
  dataArray2[3] = B01011110; // d
  dataArray2[4] = B01111001; // E
  dataArray2[5] = B01110001; // F
  dataArray2[6] = B01101111; // g
  
  
}
  
  void loop() {
  
    int val = 10; //this is for how many digits
    int i =  map(analogRead (0), 0, 1023, 0, 20); // Scale Selection Display
    int h =  map(analogRead (1), 0, 1023, 0, 7);  // Key Selection Display
   
   {
       digitalWrite(latchPin, 0);

  
    int digit2 = i % val;
    int digit1 = ( i - digit2 ) / val;
    int digit3 = h;
  
    shiftOut(dataPin, clockPin, dataArray2[digit3]);
    shiftOut(dataPin, clockPin, dataArrayRED[ digit2 ]);
    shiftOut(dataPin, clockPin, dataArrayRED[ digit1 ]);

    
      digitalWrite(latchPin, 1);

   }
  }
  
  void shiftOut( int myDataPin,  int myClockPin,  byte myDataOut) {

  
    int i=0;
    int pinState;
    
    pinMode(myClockPin, OUTPUT);
    pinMode(myDataPin, OUTPUT);
  
  
    digitalWrite(myDataPin, 0);
    digitalWrite(myClockPin, 0);
  
  
       for (i=7; i>=0; i--) 
        
    {
      
          digitalWrite(myClockPin, 0);

          
              if ( myDataOut & (1<<i) )  
       
     {
	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);
    }
    
      digitalWrite(myClockPin, 0);
  
  }
 dataArrayRED[0] = 0x3F; //00111111 - 0
  dataArrayRED[1] = 0x06; //00000110 - 1
  dataArrayRED[2] = 0x5B; //01011011 - 2
  dataArrayRED[3] = 0x4F; //01001111 - 3
  dataArrayRED[4] = 0x66; //01100110 - 4
  dataArrayRED[5] = 0x6D; //01101101 - 5
  dataArrayRED[6] = 0x7D; //01111101 - 6
  dataArrayRED[7] = 0x07; //00000111 - 7
  dataArrayRED[8] = 0x7F; //01111111 - 8
  dataArrayRED[9] = 0x6F; //01100111 - 9

Constant array initialisers are probably easier.
You can also write them in binary if you find conversion to and from hex difficult.
Saves typing.

It is much easier to initialises an array like this:-
byte dataArrayRED[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6F };