Poll
Question: Problems with flashing 7 output
q - 0 (0%)
a - 2 (100%)
s - 0 (0%)
Total Voters: 2

Pages: [1]   Go Down
Author Topic: Shift Registers  (Read 988 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

New to Arduino, but been coding for a while. I have an o level in electronics from 25 years ago! I've put together the shift register example and what ever i do output 7, the last one, flashes every time the shift register is "latched".

Any ideas?

Keepersgain
Logged

Western New York, USA
Online Online
Faraday Member
**
Karma: 40
Posts: 4403
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I've put together the shift register example ...
When I do a Google search for 'arduino shift register example' I get about 5,890,000 results.  I guess we need more information.

Don
Logged

Newcastle, England
Offline Offline
Sr. Member
****
Karma: 3
Posts: 489
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I've put together the shift register example ...
When I do a Google search for 'arduino shift register example' I get about 5,890,000 results.  I guess we need more information.

Don

I'd assume it is this tutorial.

Onions.
Logged

My website: http://www.harryrabbit.co.uk/electronics/home.html Up and running now! (Feel free to look round!) smiley-grin

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi,

This is the code.
Code:
/*     ---------------------------------------------------------
 *     |  Arduino Experimentation Kit Example Code             |
 *     |  CIRC-05 .: 8 More LEDs :. (74HC595 Shift Register)   |
 *     ---------------------------------------------------------
 *
 * We have already controlled 8 LEDs however this does it in a slightly
 * different manner. Rather than using 8 pins we will use just three
 * and an additional chip.
 *
 *
 */


//Pin Definitions
//Pin Definitions
//The 74HC595 uses a serial communication
//link which has three pins
int data = 2;
int clock = 3;
int latch = 4;

//Used for single LED manipulation
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;
                        

/*
 * setup() - this function runs once when you turn your Arduino on
 * We set the three control pins to outputs
 */
void setup()
{
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);  
  pinMode(latch, OUTPUT);  
}

/*
 * loop() - this function will start after setup finishes and then repeat
 * we set which LEDs we want on then call a routine which sends the states to the 74HC595
 */
void loop()                     // run over and over again
{
  int delayTime = 100; //the number of milliseconds to delay between LED updates
  for(int i = 0; i < 256; i++){
   updateLEDs(i);
   delay(delayTime);
  }
}



/*
 * updateLEDs() - sends the LED states set in ledStates to the 74HC595
 * sequence
 */
void updateLEDs(int value){
  digitalWrite(latch, LOW);     //Pulls the chips latch low
  shiftOut(data, clock, MSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latch, HIGH);   //Pulls the latch high displaying the data
}

/*
 * updateLEDsLong() - sends the LED states set in ledStates to the 74HC595
 * sequence. Same as updateLEDs except the shifting out is done in software
 * so you can see what is happening.
 */
void updateLEDsLong(int value){
  digitalWrite(latch, LOW);    //Pulls the chips latch low
  for(int i = 0; i < 8; i++){  //Will repeat 8 times (once for each bit)
  int bit = value & B10000000; //We use a "bitmask" to select only the eighth
                               //bit in our number (the one we are addressing this time through
  value = value << 1;          //we move our number up one bit value so next time bit 7 will be
                               //bit 8 and we will do our math on it
  if(bit == 128){digitalWrite(data, HIGH);} //if bit 8 is set then set our data pin high
  else{digitalWrite(data, LOW);}            //if bit 8 is unset then set the data pin low
  digitalWrite(clock, HIGH);                //the next three lines pulse the clock pin
  delay(1);
  digitalWrite(clock, LOW);
  }
  digitalWrite(latch, HIGH);  //pulls the latch high shifting our data into being displayed
}


//These are used in the bitwise math that we use to change individual LEDs
//For more details http://en.wikipedia.org/wiki/Bitwise_operation
int bits[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000};
int masks[] = {B11111110, B11111101, B11111011, B11110111, B11101111, B11011111, B10111111, B01111111};
/*
 * changeLED(int led, int state) - changes an individual LED
 * LEDs are 0 to 7 and state is either 0 - OFF or 1 - ON
 */
 void changeLED(int led, int state){
   ledState = ledState & masks[led];  //clears ledState of the bit we are addressing
   if(state == ON){ledState = ledState | bits[led];} //if the bit is on we will add it to ledState
   updateLEDs(ledState);              //send the new LED state to the shift register
 }



It counts in Binary and every time the register is "latched" the 8 led flashes briefly.

So i wrote my own code just loading the shift register up to create a chaser light display. It works well and is simpler for me to understand.

Code:
//Pin Definitions
//Pin Definitions
//The 74HC595 uses a serial communication
//link which has three pins
int data = 2;
int clock = 3;
int latch = 4;
int Position = 0;

//Used for single LED manipulation
int ledState = 0;
const int ON = HIGH;
const int OFF = LOW;
          
/*
 * setup() - this function runs once when you turn your Arduino on
 * We set the three control pins to outputs
 */
void setup()
{
  pinMode(data, OUTPUT);
  pinMode(clock, OUTPUT);  
  pinMode(latch, OUTPUT);  
  Serial.begin(9600);
}

/*
 * loop() - this function will start after setup finishes and then repeat
 * we set which LEDs we want on then call a routine which sends the states to the 74HC595
 */
void loop()                     // run over and over again
{
  int delayTime = 2000; //the number of milliseconds to delay between LED updates


  DoLED();
   Position++;
   if (Position == 8){
    Position = 0;
    }
   delay(delayTime);

}

 void DoLED(){
  
   digitalWrite(latch, LOW);    //Pulls the chips latch low
    
   for(int i = 0; i < 8; i++){              // set everything to low
  digitalWrite(data, LOW);
  digitalWrite(clock, HIGH);                //the next three lines pulse the clock pin
  delay(1);
  digitalWrite(clock, LOW);
  
  if (i == Position){                      // if it's the lED that should be alight then light it up.
  digitalWrite(data, HIGH);
  digitalWrite(clock, HIGH);                //the next three lines pulse the clock pin
  delay(1);
  digitalWrite(clock, LOW);
    }  
 }
 
  digitalWrite(latch, HIGH);  //pulls the latch high shifting our data into being displayed  

}


This also flashes the 8 LED each time the array is "latched", just a flash not staying on the same time as the lit LED just a brief flash. I've tried a second shift register but no change. It is undoubtably me but can't for the life of me see what it is.

Thanks for the prompt replies!

Keepersgain
« Last Edit: June 25, 2011, 01:11:17 pm by keepersgain » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 224
Posts: 13917
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Thanks for posting the code, but please modify your post, select the code part and press the # button. Then it will get tagged correctly, looks so much better ...

Rob
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 129
Posts: 8602
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your DoLED func will output 9 pulses, one for every bit and another for the "Position" bit. Try
Code:
void DoLED(){
 
   digitalWrite(latch, LOW);   
     
   for(int i = 0; i < 8; i++){             
if (i == Position)                     
  digitalWrite(data, HIGH);
else
  digitalWrite(data, LOW);

  digitalWrite(clock, HIGH);               
  digitalWrite(clock, LOW);

}
 
digitalWrite(latch, HIGH); 

}

______
Rob

« Last Edit: June 25, 2011, 07:16:57 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Thanks for that code, that makes more sense. So the LED is still flashing but the code's correct so it's time to look at my bread boarding. I'll start again on that and see if the flashing still happens.

Keepersgain
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

So i de-breadboarded it and started again and lo and behold i find there are two pins marked as O7. One is O7 and the other is O7'. So i use the correct one and it all worked.

Thanks for all the help.

Keepersgain
Logged

Pages: [1]   Go Up
Jump to: