Binary counter with 16 Leds

Hello

I am following the shiftout tutorial on the site and have completed the counting upto 256 using one 595 shift register. I have added the second 595 and connected a further 8 Led’s. This counts up on the Red Led’s and down on the Green Led’s still using 256, what I want to do is count up using all the Led’s. So thats Bin 1111111111111111 = 65535. How would I change the code to achieve this?

//**************************************************************//
//  Name    : shiftOutCode, Dual Binary Counters                 //
//  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;



void setup() {
  //Start Serial for debuging purposes	
  Serial.begin(9600);
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);

}

void loop() {
  //count up routine
  for (int j = 0; j < 256; j++) {
    //ground latchPin and hold low for as long as you are transmitting
    digitalWrite(latchPin, 0);
    //count up on GREEN LEDs
    shiftOut(dataPin, clockPin, j); 
    //count down on RED LEDs
    shiftOut(dataPin, clockPin, 255-j);
    //return the latch pin high to signal chip that it 
    //no longer needs to listen for information
    digitalWrite(latchPin, 1);
    delay(1000);
  }
}

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 (i=7; i>=0; i--)  {
    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<<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);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}

Thanks

Geoff

use unsigned int iso int as it can go to 65535 = 1111 ...1 (16x)

Thanks that looks like what I need. Any example of how i would incorporate this into the shiftout code?

//**************************************************************//
//  Name    : shiftOutCode, Hello World                                
//  Author  : Carlyn Maw,Tom Igoe, David A. Mellis 
//  Date    : 25 Oct, 2006    
//  Modified: 23 Mar 2010                                 
//  Version : 2.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;



void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() {
  // count from 0 to 255 and display the number 
  // on the LEDs
  for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay++) {
    // take the latchPin low so 
    // the LEDs don't change while you're sending in bits:
    digitalWrite(latchPin, LOW);
    // shift out the bits:
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);  

    //take the latch pin high so the LEDs will light up:
    digitalWrite(latchPin, HIGH);
    // pause before next value:
    delay(500);
  }
}

regards

Geoff

Read - http://www.arduino.cc/en/Tutorial/ShiftOut - carefully especially the part about red /green leds at the end.

The code should be something like below (not tested)

void loop()
{
  for (unsigned int numberToDisplay = 0;  ; numberToDisplay++)  // it will automagically wrap around ..
  {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay % 256);  lower byte
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay / 256);  higher byte
    digitalWrite(latchPin, HIGH);
    delay(500);
  }
}

be aware 65535 * 500 millisec = 32767 seconds, thats about 9 hours for one loop…

thanks that seemed to work. i was surprised to see the second 595 counts first. so i swapped the LED inputs about. So the second 595 has the first LED's attached and the first 595 has the second LED's. I have the serial monitor printing and it counts up fine.

Just out of interest since I have this setup how would I specify a single number to display like "43690" which would hopefully display 1010101010101010 (On, off, On off etc)?

Regards

Geoff

void display(unsigned int numberToDisplay){
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay % 256);  lower byte
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay / 256);  higher byte
    digitalWrite(latchPin, HIGH);
}

// then use
   display(43690);

Thanks Mike I'll give that a go.

Geoff

Wow this doesn't want to go in??

I keep getting a

"core.a(main.cpp.o): In function `main':
C:\Program Files\ARDUINO\arduino-0022\hardware\arduino\cores\arduino/main.cpp:10: undefined reference to `loop'

Cant see where its gone wrong?

I have read you right Mike Void (display) instead of Void (loop)?

//**************************************************************//
//  Name    : shiftOutCode, Hello World                                
//  Author  : Carlyn Maw,Tom Igoe, David A. Mellis 
//  Date    : 25 Oct, 2006    
//  Modified: 23 Mar 2010                                 
//  Version : 2.0                                             
//  Notes   : Code for using a 74HC595 Shift Register           //
//          : Display specified value                          
//****************************************************************

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


void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
    Serial.begin(9600);
  Serial.println("reset");
}

void display(unsigned int numberToDisplay){
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay % 256);  //lower byte
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay / 256); // higher byte
    digitalWrite(latchPin, HIGH);
     display(43690);
}

Thanks

Geoff

I have read you right Mike Void (display) instead of Void (loop)?

No you always need a loop, you need the display as well, and you call it anywhere you want. Here I have put it in the setup();

//**************************************************************//
//  Name    : shiftOutCode, Hello World                                
//  Author  : Carlyn Maw,Tom Igoe, David A. Mellis 
//  Date    : 25 Oct, 2006    
//  Modified: 23 Mar 2010                                 
//  Version : 2.0                                             
//  Notes   : Code for using a 74HC595 Shift Register           //
//          : Display specified value                          
//****************************************************************

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


void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
    Serial.begin(9600);
  Serial.println("reset");
       display(43690);
}

void loop(){
}

void display(unsigned int numberToDisplay){
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay % 256);  //lower byte
    shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay / 256); // higher byte
    digitalWrite(latchPin, HIGH);
}

Thanks Mike, still learning only had it a week :blush:

Geoff