Go Down

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

GoatBoy

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

Grumpy_Mike

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.

GoatBoy

#2
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);
 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);

}

AWOL

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.

GoatBoy

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

AWOL

Yes, an if is fine, just don't do an assignment in it.
"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.

GoatBoy

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

AWOL

Yes, you should be changing it from an assignment to a comparison.
"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.

GoatBoy

:)Cool!! Thanks for your help AWOL!!  This what I did
Code: [Select]
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);

  }

AWOL

#9
Apr 23, 2011, 12:11 pm Last Edit: Apr 23, 2011, 02:36 pm by AWOL Reason: 1
And that gets you a zero?


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

differs from this:
Code: [Select]
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));
"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.

GoatBoy

:) 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!!!!! :) 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...
Code: [Select]

//**************************************************************//
//  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);

}

AWOL

Quote
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)?
"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.

GoatBoy

#12
Apr 23, 2011, 08:08 pm Last Edit: Apr 23, 2011, 08:11 pm by GoatBoy Reason: 1
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! :)

Grumpy_Mike

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

GoatBoy

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

Go Up