Linking Four SHift Register

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

Post by bob500000 »

Thanks mike,

Yeah I seen that mistake, made the changes however still got some problems.

but i'll show you what I'm getting in terms of LED's lighting, confusing the hell out of me now.

Also for some reason the buttons are constantly on, any ideas on that one?
LED's.jpg
LED's.jpg (352.84 KiB) Viewed 819 times
cheers

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Linking Four SHift Register

Post by adafruit_support_mike »

Post a copy of the code you're using now and we'll see what we can find.

bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

Post by bob500000 »

Hi Mike,

As requested here is a copy of the code.

Code: Select all

#include <Button.h>

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int outputEnablePin = 3;
int ledPin = 9;
//Button trigger = Button(2,LOW);
int sr1Colour = 0;  // Set for ShiftReg1 as 0 = off, 1 = green, 2 = red
int sr2Colour = 0;

byte leds = 0;
byte button[] = {11, 12};
byte switch_value = 0;
boolean btnOn = false;
int count = 0;
int bright = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

#define NUMBUTTONS sizeof(button)
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

void setup() {
  pinMode(10, OUTPUT);
  setBrightness(0);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  pinMode(outputEnablePin, OUTPUT); 
  //pinMode(2,INPUT); //Trigger switch sits on pin 2
  Serial.begin(9600);
  Serial.println("Welcome");
  byte i;
  
   for (i=0; i< NUMBUTTONS; i++) {
    pinMode(button[i], INPUT);
    digitalWrite(button[i], HIGH);
  }
    //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;
}
//byte pressCount1 = 0;
SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  byte index;

  for (index = 0; index < NUMBUTTONS; index++) {
    currentstate[index] = digitalRead(button[index]);   // read the button
    /*    
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */
    
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
          count = count+1;
          //switch_value = switch_value + 1;

      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}


void loop() {
  idle(); 
 /////////////////////////////////////////////////////
 //This is Button 1 Set Press
 /////////////////////////////////////////////////////
  
  if(justpressed[0] && btnOn)
  {
    idleoff();
    count;
    sequence2();
    justpressed[0] = 0;
    btnOn = false;
  }  
  if(justpressed[0]) {
    Serial.println("Button 0 pressed");
    btnOn = true;
    idleoff();
    count;
    justpressed[0] = 0;
    sequence1();
   }
//  /////////////////////////////////////////////////////
// //This is Button 2 Set Press
// /////////////////////////////////////////////////////
if(justpressed[1] && btnOn)
  {
    idleoff();
    count;
    sequence1();
    justpressed[1] = 0;
    btnOn = false;
  }  
  if(justpressed[1]) {
    Serial.println("Button 1 pressed");
    btnOn = true;
    idleoff();
    count;
    justpressed[1] = 0;
    sequence2();
   }
//  /////////////////////////////////////////////////////
// //Run final Sequence1
// ///////////////////////////////////////////////////// 

if (sr1Colour ==1 && sr2Colour==1){
  sequenceFinal1();
  ledPin = HIGH;
}
//  /////////////////////////////////////////////////////
// //Run final Sequence2
// ///////////////////////////////////////////////////// 

if (sr1Colour ==1 && sr2Colour==2){
  sequenceFinal2();
  ledPin = HIGH;
}
//  /////////////////////////////////////////////////////
// //Run final Sequence3
// ///////////////////////////////////////////////////// 

if (sr1Colour ==1 && sr2Colour==1){
  sequenceFinal3();
  ledPin = HIGH;
}
//  /////////////////////////////////////////////////////
// //Run final Sequence4
// ///////////////////////////////////////////////////// 

if (sr1Colour ==1 && sr2Colour==2){
  sequenceFinal4();
  ledPin = HIGH;
}
}
/////////////////////////////////////////////////////
 //This the idle setup
 ////////////////////////////////////////////////////
 void idle()
 {
  // set the brightness of pin 9:
  analogWrite(10, bright);    

  // change the brightness for next time through the loop:
  bright = bright + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (bright == 0 || bright == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(70);                            
}/////////////////////////////////////////////////////
 //This the idle off switch
 ////////////////////////////////////////////////////
 void idleoff()
 {
  // set the brightness of pin 9:
  analogWrite(10,0);                        
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister
 ////////////////////////////////////////////////////
void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 1
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister2
 ////////////////////////////////////////////////////
void updateShiftRegister2()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister3
 ////////////////////////////////////////////////////
void updateShiftRegister3()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 1
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister4
 ////////////////////////////////////////////////////
void updateShiftRegister4()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister5
 ////////////////////////////////////////////////////
void updateShiftRegister5()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister6
 ////////////////////////////////////////////////////
void updateShiftRegister6()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister7
 ////////////////////////////////////////////////////
void updateShiftRegister7()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister8
 ////////////////////////////////////////////////////
void updateShiftRegister8()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is setBrightness
 ////////////////////////////////////////////////////
void setBrightness(byte brightness) // 0 to 255
{
  analogWrite(outputEnablePin, 255-brightness);
}
/////////////////////////////////////////////////////
 //This is Sequence 1
 ////////////////////////////////////////////////////
void sequence1(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000); 
   sr1Colour = 1; //Set SR1 to green
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 2
 ////////////////////////////////////////////////////
void sequence2(){
  Serial.println("button 2");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister2();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3)
    {
      Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister2();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[1] && btnOn && (count >=3)) {
      leds = 0;
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000); 
   sr1Colour = 2; //Set SR1 to red
   
  leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 3
 ////////////////////////////////////////////////////
void sequence3(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister3();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister3();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000); 
  sr1Colour = 2; //Set SR1 to red
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 4
////////////////////////////////////////////////////
void sequence4(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister3();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister4();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000); 
  sr1Colour = 1; //Set SR1 to Green
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
} 
/////////////////////////////////////////////////////
 //This is Sequence 5
////////////////////////////////////////////////////
void sequence5(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister5();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister5();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000); 
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 6
////////////////////////////////////////////////////
void sequence6(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister6();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister6();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000); 
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 7
////////////////////////////////////////////////////
void sequence7(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister7();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister7();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000); 
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 8
////////////////////////////////////////////////////
void sequence8(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister8();
    delay(50);
    for (byte b = 0; b < 255; b++)
    {
      setBrightness(b);
      delay(20);
    }
    Serial.println(count);
    if(count>=3){
     Serial.println("Count =");
      Serial.print(count);
      leds = 0;
      //for (int i = 0; i < 4; i++)
  {
    bitSet(leds, -5);
    updateShiftRegister8();
    delay(50);
      setBrightness(255);
  }
      count = 0;
      break;
    }
    if (justpressed[0] && btnOn && (count >=3)) {
      Serial.println("Button 0 pressed again");
      count;
      leds = 0;
      setBrightness(255);
      break;
    }
  }
  //bitSet(leds,0);
  //delay(1000); 
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Final Sequence 1
////////////////////////////////////////////////////
 void sequenceFinal1(){
   sequence5();
   sequence7();
 }
/////////////////////////////////////////////////////
 //This is Final Sequence 2
////////////////////////////////////////////////////
void sequenceFinal2(){
   sequence5();
   sequence8();
 }
/////////////////////////////////////////////////////
 //This is Final Sequence 3
////////////////////////////////////////////////////
void sequenceFinal3(){
   sequence6();
   sequence7();
 }
/////////////////////////////////////////////////////
 //This is Final Sequence 4
////////////////////////////////////////////////////
void sequenceFinal4(){
   sequence6();
   sequence8();
 }
Cheers

bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

Post by bob500000 »

Hi mike,

Any ideas on this one?

Cheers

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Linking Four SHift Register

Post by adafruit_support_mike »

Okay, the logic here is a bit convoluted:

Code: Select all

    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
          count = count+1;
          //switch_value = switch_value + 1;

      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
You seem to be looking for rising and falling edges, but you're using three values to do it.. currentstate[index], previousstate[index], and pressed[index]. I assume you're using currentstate and previousstate to prevent glitching from switch noise.

There's an easier way to do that:

Code: Select all

byte state;

for (byte index=0 ; index < NUMBUTTONS ; i++) {
    currentstate[index] <<= 1;                          //  shift the previous state left one bit
    currentstate[index] += digitalRead(button[index]);  //  add in the new reading
    
    state = currentstate[index] & 7;                    //  get the last three reaadings
    
    justpressed[index] = (6 == state) ? 1 : 0;          //  bit pattern xxxxx110
    justreleased[index] = (1 == state) ? 1 : 0;         //  bit pattern xxxxx001
}
The logic in these chunks is also a bit unweildy:

Code: Select all

  if(justpressed[0] && btnOn)
  {
    idleoff();
    count;
    sequence2();
    justpressed[0] = 0;
    btnOn = false;
  }  
  if(justpressed[0]) {
    Serial.println("Button 0 pressed");
    btnOn = true;
    idleoff();
    count;
    justpressed[0] = 0;
    sequence1();
   }
This is a cleaner way of saying the same thing:

Code: Select all

if (justpressed[0]) {
    justpressed[0] = 0;
    idleoff();
    
    if (btnOn) {
        btnOn = false;
        sequence2();
    } else {
        btnOn = true;
        sequence1();
    }
}
and the part that clears the value of justpressed[N] isn't necessary if you use the main loop above.

A basic rule of programming is that branching creates bugs. Straight "do this, then do that, then do that" sequences rarely cause trouble, and when they do, the trouble is easy to find. The more alternate branches of execution you have, and the more widely spaced out the decisions are, the easier it is for something to go wrong, and the harder it is to spot the problem.

When you have a variable that controls branching, it's best to make all the decisions about it and assign every possible value to it in one place. Setting justpressed[N] in the loop that checks the buttons and clearing it later, inside a condition that checks to see if it's set, invites trouble.

Closely related to that, tests that check more than one variable should provide an action for every possible combination of values. The "oh, I forgot to say what happens when X=something but Y doesn't equal something-else" bug is extremely common but frequently hard to find.

Putting the same code in both cases of a conditional is a flat-out no-no. Either do that business before you branch, or after you branch.

When the same variable appears in several tests, it's usually a sign that you need to rearrange your tests. A variable that appears in several tests and gets assigned new values as a result of the tests is a bug just waiting to happen. I really don't like that global boolean 'btnOn'.. the fact that it gets tested and reassigned for every value in the justpressed[] array (but only if the justpressed[N] value is nonzero) creates a pattern of interdependencies that's hard to describe.

What are you trying to represent with that value?

bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

Post by bob500000 »

Hi mike,

Thanks for that will try it tomorrow, however been testing some other ways of achieving the same results,

I was wondering how you would tell the shift register to stop updating and break out of it loop for example to turn the LED's off.

I have using the example code from the following site, but can't seem to get it break out of the loop once all the LEDs are lit, bear in mind I am using 2 shift registers daisy chained.

http://www.oomlout.com/oom.php/products/ardx/circ-05

Cheers.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Linking Four SHift Register

Post by adafruit_support_mike »

bob500000 wrote:I was wondering how you would tell the shift register to stop updating and break out of it loop for example to turn the LED's off.
That much is easy.. just take the Memory Reset signal (pin 10) LOW, then HIGH again. Strobing that zeros out the internal shift register.

bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

Post by bob500000 »

ok, so if the loop currently runs like so,

Code: Select all

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

where would I break out of the loop when all LED's are lit and how would I do it?

cheers

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Linking Four SHift Register

Post by adafruit_support_mike »

If I'm reading the oomlout code correctly, it simply runs all 256 combinations of ON/OFF states through the register. The state where all the LEDs are on would be the value 255, which is where things stand after the for() loop ends.

The reason it happens over and over again is that the Arduino bootloader keeps calling the function loop() as long as the device has power. To stop after a single pass, move the for() loop out of the loop() function and over to setup().

If you want to do the same thing for two shift registers in series, be aware that there are 65536 OF/OFF states for 16 bits. At 10 patterns per second (a 100ms delay between shift register updates) the whole cycle will take about 110 minutes.

Doing it would just be a matter of adjusting updateLEDs to accept and shift two bytes:

Code: Select all

void updateLEDs(byte value1, byte value2) {
  digitalWrite(latch, LOW);
  shiftOut(data, clock, MSBFIRST, value1);
  shiftOut(data, clock, MSBFIRST, value2);
  digitalWrite(latch, HIGH);
}

bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

Post by bob500000 »

Hi Mike,

The current button press is this, I want it to break out of the loop when the the button is pressed twice how would I do this, also can I change this?

Code: Select all

void loop() {
 /////////////////////////////////////////////////////
 //This is Button 1 Set Press
 /////////////////////////////////////////////////////
  updateLEDs(ledState == -5);
  idle();
  if(justpressed[0])
  {
    idleoff();
  int delayTime = 500; //the number of milliseconds to delay between LED updates
  for(int i = 0; i < 8; i++){
  updateLEDs(i);
  delay(delayTime);
  if(i == 9){
   updateLEDs(ledState == -5);
   Result();
  }
//    count;
//    sequence2();
//    justpressed[0] = 0;
//    btnOn = false;
//  }  
//  if(justpressed[0]) {
//    Serial.println("Button 0 pressed");
//    btnOn = true;
//    count;
//    justpressed[0] = 0;
//    sequence1();
   }
Cheers

bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

Post by bob500000 »

Hi Mike,

OK so engineered what ommlout used and got the following, your simplification of the switches didn't work so went back to the original idea, however, I am trying to run the opposite using another set of shift registers and they are constantly live, can you take a look at the test 2 method and tell me what is wrong please mate.

Cheers

Code: Select all

#include <Button.h>

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int latchPin2 = 0;
int clockPin2 = 1;
int dataPin2 = 2;
int outputEnablePin = 3;
int ledPin = 10;
//Button trigger = Button(2,LOW);
int sr1Colour = 0;  // Set for ShiftReg1 as 0 = off, 1 = green, 2 = red
int sr2Colour = 0;
int ledState = 0;
int ledState2 = 0;
const int ON = HIGH;
const int OFF = LOW;
int bright = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

byte leds = 0;
byte button[] = {7, 8};
byte switch_value = 0;
boolean btnOn = false;
int count = 0;

#define NUMBUTTONS sizeof(button)
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];

void setup() {
  
  
  setBrightness(0);
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  pinMode(outputEnablePin, OUTPUT); 
  //pinMode(2,INPUT); //Trigger switch sits on pin 2
  Serial.begin(9600);
  Serial.println("Welcome");
  byte i;
  
   for (i=0; i< NUMBUTTONS; i++) {
    pinMode(button[i], INPUT);
    digitalWrite(button[i], HIGH);
  }
    //Timer2 Overflow Interrupt Enable
  TIMSK2 |= 1<<TOIE2;
}
//byte pressCount1 = 0;
SIGNAL(TIMER2_OVF_vect) {
  check_switches();
}

void check_switches()
{
  static byte previousstate[NUMBUTTONS];
  static byte currentstate[NUMBUTTONS];
  byte index;

  for (index = 0; index < NUMBUTTONS; index++) {
    currentstate[index] = digitalRead(button[index]);   // read the button
    /*    
    Serial.print(index, DEC);
    Serial.print(": cstate=");
    Serial.print(currentstate[index], DEC);
    Serial.print(", pstate=");
    Serial.print(previousstate[index], DEC);
    Serial.print(", press=");
    */
    
    if (currentstate[index] == previousstate[index]) {
      if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
          // just pressed
          justpressed[index] = 1;
          count = count+1;
          //switch_value = switch_value + 1;

      }
      else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
          // just released
          justreleased[index] = 1;
      }
      pressed[index] = !currentstate[index];  // remember, digital HIGH means NOT pressed
    }
    //Serial.println(pressed[index], DEC);
    previousstate[index] = currentstate[index];   // keep a running tally of the buttons
  }
}


void loop() {
 /////////////////////////////////////////////////////
 //This is Button 1 Set Press
 /////////////////////////////////////////////////////
 updateLEDs(ledState == -5);
  idle();
  if(justpressed[0])
  {
    updateLEDs2(ledState == -5);
    Serial.print("Button 1 pressed");
    btnOn = true;
    idleoff();
  int delayTime = 2000; //the number of milliseconds to delay between LED updates
  for(int i = 0; i < 16; i++){
  updateLEDs(i);
  delay(delayTime);
  if(i >= 16){
    //btnOn = false;
   updateLEDs(ledState == -5);
   Result();
  }
  }
  }
//   else {
//        btnOn = true;
//        
//    }
//    count;
//    sequence2();
//    justpressed[0] = 0;
//    btnOn = false;
//  }  
//  if(justpressed[0]) {
//    Serial.println("Button 0 pressed");
//    btnOn = true;
//    count;
//    justpressed[0] = 0;
//    sequence1();

//  /////////////////////////////////////////////////////
// //This is Button 2 Set Press
// /////////////////////////////////////////////////////
if(justpressed[1])
  {
    updateLEDs2(ledState == -5);
    updateLEDs(ledState == -5);
    Serial.print("Button 2 pressed");
    btnOn = true;
    idleoff();
  int delayTime = 2000; //the number of milliseconds to delay between LED updates
  for(int i = 0; i < 16; i++){
    Serial.print("For");
  updateLEDs2(i);
  delay(delayTime);
  }
}
}
/////////////////////////////////////////////////////
//This the idle setup
////////////////////////////////////////////////////
void idle()
{
  // set the brightness of pin 9:
  analogWrite(10, bright);    

  // change the brightness for next time through the loop:
  bright = bright + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (bright == 0 || bright == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(100);                            
}
/////////////////////////////////////////////////////
//This the idle off switch
////////////////////////////////////////////////////
void idleoff()
{
  // set the brightness of pin 9:
  analogWrite(10,0);                        
}
/////////////////////////////////////////////////////
//This the idle setup
////////////////////////////////////////////////////
void Result()
{
  // set the brightness of pin 9:
  analogWrite(11, bright);    

  // change the brightness for next time through the loop:
  bright = bright + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (bright == 0 || bright == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(70);                            
}
////  /////////////////////////////////////////////////////
//// // Test1
//// /////////////////////////////////////////////////////

void updateLEDs(int value){
  digitalWrite(latchPin, LOW);     //Pulls the chips latch low
  shiftOut(dataPin, clockPin, MSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latchPin, 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(latchPin, LOW);    //Pulls the chips latch low
  for(int i = 0; i < 9; i++){  //Will repeat 8 times (once for each bit)
  int bit = value & B1000000; //We use a "bitmask" to select only the eighth 
                               //bit in our number (the one we are addressing this time thro
                    //ugh
  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(dataPin, HIGH);} //if bit 8 is set then set our data pin high
  else{digitalWrite(dataPin, LOW);}            //if bit 8 is unset then set the data pin low
  digitalWrite(clockPin, HIGH);                //the next three lines pulse the clock pin
  delay(10);
  digitalWrite(clockPin, LOW);
  }
  digitalWrite(latchPin, 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 le
                    //dState
   updateLEDs(ledState);              //send the new LED state to the shift register
 }

////  /////////////////////////////////////////////////////
//// // Test2
//// /////////////////////////////////////////////////////
void updateLEDs2(int value){
  digitalWrite(latchPin2, LOW);     //Pulls the chips latch low
  shiftOut(dataPin2, clockPin2, MSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latchPin2, 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 updateLEDsLong2(int value){
  digitalWrite(latchPin2, LOW);    //Pulls the chips latch low
  for(int i = 0; i < 16; i++){  //Will repeat 8 times (once for each bit)
  int bit = value & B1000000; //We use a "bitmask" to select only the eighth 
                               //bit in our number (the one we are addressing this time thro
                    //ugh
  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(dataPin2, HIGH);} //if bit 8 is set then set our data pin high
  else{digitalWrite(dataPin2, LOW);}            //if bit 8 is unset then set the data pin low
  digitalWrite(clockPin2, HIGH);                //the next three lines pulse the clock pin
  delay(1);
  digitalWrite(clockPin2, LOW);
  }
  digitalWrite(latchPin2, 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 bits2[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000};
int masks2[] = {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 changeLED2(int led, int state){
   ledState2 = ledState2 & masks2[led];  //clears ledState of the bit we are addressing
   if(state == ON){ledState2 = ledState2 | bits2[led];} //if the bit is on we will add it to le
                    //dState
   updateLEDs2(ledState2);              //send the new LED state to the shift register
 }

////  /////////////////////////////////////////////////////
//// //Run final Sequence1
//// ///////////////////////////////////////////////////// 
//
//if (sr1Colour ==1 && sr2Colour==1){
//  sequenceFinal1();
//  ledPin = HIGH;
//}
////  /////////////////////////////////////////////////////
//// //Run final Sequence2
//// ///////////////////////////////////////////////////// 
//
//if (sr1Colour ==1 && sr2Colour==2){
//  sequenceFinal2();
//  ledPin = HIGH;
//}
////  /////////////////////////////////////////////////////
//// //Run final Sequence3
//// ///////////////////////////////////////////////////// 
//
//if (sr1Colour ==1 && sr2Colour==1){
//  sequenceFinal3();
//  ledPin = HIGH;
//}
////  /////////////////////////////////////////////////////
//// //Run final Sequence4
//// ///////////////////////////////////////////////////// 
//
//if (sr1Colour ==1 && sr2Colour==2){
//  sequenceFinal4();
//  ledPin = HIGH;
//}
/////////////////////////////////////////////////////
 //This is updateShiftRegister
 ////////////////////////////////////////////////////
void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 1
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister2
 ////////////////////////////////////////////////////
void updateShiftRegister2()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister3
 ////////////////////////////////////////////////////
void updateShiftRegister3()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 1
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister4
 ////////////////////////////////////////////////////
void updateShiftRegister4()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister5
 ////////////////////////////////////////////////////
void updateShiftRegister5()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister6
 ////////////////////////////////////////////////////
void updateShiftRegister6()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister7
 ////////////////////////////////////////////////////
void updateShiftRegister7()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is updateShiftRegister8
 ////////////////////////////////////////////////////
void updateShiftRegister8()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}
/////////////////////////////////////////////////////
 //This is setBrightness
 ////////////////////////////////////////////////////
void setBrightness(byte brightness) // 0 to 255
{
  analogWrite(outputEnablePin, 255-brightness);
}
/////////////////////////////////////////////////////
 //This is Sequence 1
 ////////////////////////////////////////////////////
void sequence1(){
  Serial.println("button 1");
  for (int i = 0; i < 4; i++)
  {
    
    Serial.println(i);
  }
    
    
//    bitSet(leds, i);
//    updateShiftRegister();
//    delay(50);
//    for (byte b = 0; b < 255; b++)
//    {
//      Serial.println(i);
//      setBrightness(b);
//      delay(20);
//    }
//    Serial.println(count);
//    if(count>=3){
//     Serial.println("Count =");
//      Serial.print(count);
//      leds = 0;
//      //for (int i = 0; i < 4; i++)
//  {
//    bitClear(leds, 0);
//    updateShiftRegister();
//    delay(50);
//      setBrightness(255);
//  }
//      count = 0;
//      break;
//    }
//    if (justpressed[0] && btnOn && (count >=3)) {
//      Serial.println("Button 0 pressed again");
//      count;
//      leds = 0;
//      setBrightness(255);
//      break;
//    }
//  }
//  //bitSet(leds,0);
//  //delay(1000); 
//   sr1Colour = 1; //Set SR1 to green
// leds = 0;
//   //switch_value = 0;
//  setBrightness(255);
}
/////////////////////////////////////////////////////
 //This is Sequence 2
 ////////////////////////////////////////////////////
void sequence2(){
  Serial.println("button 2 blah");
  leds = B00101010;
  
  updateShiftRegister();
  
  setBrightness(125);
//  
//  
//  for (int i = 0; i < 4; i++)
//  {
//    updateShiftRegister();
//    bitSet(leds, i);
//    delay(50);
//    for (byte b = 0; b < 255; b++)
//    {
//      setBrightness(125);
//      delay(20);
//    }
//    Serial.println(count);
//    if(count>=3)
//    {
//      Serial.println("Count =");
//      Serial.print(count);
//      leds = 0;
//      //for (int i = 0; i < 4; i++)
//  {
//     bitClear(leds, i);
//    bitSet(leds, -5);
//    updateShiftRegister2();
//    delay(50);
//      setBrightness(255);
//  }
//      count = 0;
//      break;
//    }
//    if (justpressed[1] && btnOn && (count >=3)) {
//      leds = 0;
//      break;
//    }
//  }
//  //bitSet(leds,0);
//  //delay(1000); 
//   sr1Colour = 2; //Set SR1 to red
//   
//  leds = 0;
//   //switch_value = 0;
//  setBrightness(255);
}
///////////////////////////////////////////////////////
// //This is Sequence 3
// ////////////////////////////////////////////////////
//void sequence3(){
//  Serial.println("button 1");
//  for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, i);
//    updateShiftRegister3();
//    delay(50);
//    for (byte b = 0; b < 255; b++)
//    {
//      setBrightness(b);
//      delay(20);
//    }
//    Serial.println(count);
//    if(count>=3){
//     Serial.println("Count =");
//      Serial.print(count);
//      leds = 0;
//      //for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, -5);
//    updateShiftRegister3();
//    delay(50);
//      setBrightness(255);
//  }
//      count = 0;
//      break;
//    }
//    if (justpressed[0] && btnOn && (count >=3)) {
//      Serial.println("Button 0 pressed again");
//      count;
//      leds = 0;
//      setBrightness(255);
//      break;
//    }
//  }
//  //bitSet(leds,0);
//  //delay(1000); 
//  sr1Colour = 2; //Set SR1 to red
// leds = 0;
//   //switch_value = 0;
//  setBrightness(255);
//}
///////////////////////////////////////////////////////
// //This is Sequence 4
//////////////////////////////////////////////////////
//void sequence4(){
//  Serial.println("button 1");
//  for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, i);
//    updateShiftRegister3();
//    delay(50);
//    for (byte b = 0; b < 255; b++)
//    {
//      setBrightness(b);
//      delay(20);
//    }
//    Serial.println(count);
//    if(count>=3){
//     Serial.println("Count =");
//      Serial.print(count);
//      leds = 0;
//      //for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, -5);
//    updateShiftRegister4();
//    delay(50);
//      setBrightness(255);
//  }
//      count = 0;
//      break;
//    }
//    if (justpressed[0] && btnOn && (count >=3)) {
//      Serial.println("Button 0 pressed again");
//      count;
//      leds = 0;
//      setBrightness(255);
//      break;
//    }
//  }
//  //bitSet(leds,0);
//  //delay(1000); 
//  sr1Colour = 1; //Set SR1 to Green
// leds = 0;
//   //switch_value = 0;
//  setBrightness(255);
//} 
///////////////////////////////////////////////////////
// //This is Sequence 5
//////////////////////////////////////////////////////
//void sequence5(){
//  Serial.println("button 1");
//  for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, i);
//    updateShiftRegister5();
//    delay(50);
//    for (byte b = 0; b < 255; b++)
//    {
//      setBrightness(b);
//      delay(20);
//    }
//    Serial.println(count);
//    if(count>=3){
//     Serial.println("Count =");
//      Serial.print(count);
//      leds = 0;
//      //for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, -5);
//    updateShiftRegister5();
//    delay(50);
//      setBrightness(255);
//  }
//      count = 0;
//      break;
//    }
//    if (justpressed[0] && btnOn && (count >=3)) {
//      Serial.println("Button 0 pressed again");
//      count;
//      leds = 0;
//      setBrightness(255);
//      break;
//    }
//  }
//  //bitSet(leds,0);
//  //delay(1000); 
// leds = 0;
//   //switch_value = 0;
//  setBrightness(255);
//}
///////////////////////////////////////////////////////
// //This is Sequence 6
//////////////////////////////////////////////////////
//void sequence6(){
//  Serial.println("button 1");
//  for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, i);
//    updateShiftRegister6();
//    delay(50);
//    for (byte b = 0; b < 255; b++)
//    {
//      setBrightness(b);
//      delay(20);
//    }
//    Serial.println(count);
//    if(count>=3){
//     Serial.println("Count =");
//      Serial.print(count);
//      leds = 0;
//      //for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, -5);
//    updateShiftRegister6();
//    delay(50);
//      setBrightness(255);
//  }
//      count = 0;
//      break;
//    }
//    if (justpressed[0] && btnOn && (count >=3)) {
//      Serial.println("Button 0 pressed again");
//      count;
//      leds = 0;
//      setBrightness(255);
//      break;
//    }
//  }
//  //bitSet(leds,0);
//  //delay(1000); 
// leds = 0;
//   //switch_value = 0;
//  setBrightness(255);
//}
///////////////////////////////////////////////////////
// //This is Sequence 7
//////////////////////////////////////////////////////
//void sequence7(){
//  Serial.println("button 1");
//  for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, i);
//    updateShiftRegister7();
//    delay(50);
//    for (byte b = 0; b < 255; b++)
//    {
//      setBrightness(b);
//      delay(20);
//    }
//    Serial.println(count);
//    if(count>=3){
//     Serial.println("Count =");
//      Serial.print(count);
//      leds = 0;
//      //for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, -5);
//    updateShiftRegister7();
//    delay(50);
//      setBrightness(255);
//  }
//      count = 0;
//      break;
//    }
//    if (justpressed[0] && btnOn && (count >=3)) {
//      Serial.println("Button 0 pressed again");
//      count;
//      leds = 0;
//      setBrightness(255);
//      break;
//    }
//  }
//  //bitSet(leds,0);
//  //delay(1000); 
// leds = 0;
//   //switch_value = 0;
//  setBrightness(255);
//}
///////////////////////////////////////////////////////
// //This is Sequence 8
//////////////////////////////////////////////////////
//void sequence8(){
//  Serial.println("button 1");
//  for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, i);
//    updateShiftRegister8();
//    delay(50);
//    for (byte b = 0; b < 255; b++)
//    {
//      setBrightness(b);
//      delay(20);
//    }
//    Serial.println(count);
//    if(count>=3){
//     Serial.println("Count =");
//      Serial.print(count);
//      leds = 0;
//      //for (int i = 0; i < 4; i++)
//  {
//    bitSet(leds, -5);
//    updateShiftRegister8();
//    delay(50);
//      setBrightness(255);
//  }
//      count = 0;
//      break;
//    }
//    if (justpressed[0] && btnOn && (count >=3)) {
//      Serial.println("Button 0 pressed again");
//      count;
//      leds = 0;
//      setBrightness(255);
//      break;
//    }
//  }
//  //bitSet(leds,0);
//  //delay(1000); 
// leds = 0;
//   //switch_value = 0;
//  setBrightness(255);
//}
///////////////////////////////////////////////////////
// //This is Final Sequence 1
//////////////////////////////////////////////////////
// void sequenceFinal1(){
//   sequence5();
//   sequence7();
// }
///////////////////////////////////////////////////////
// //This is Final Sequence 2
//////////////////////////////////////////////////////
//void sequenceFinal2(){
//   sequence5();
//   sequence8();
// }
///////////////////////////////////////////////////////
// //This is Final Sequence 3
//////////////////////////////////////////////////////
//void sequenceFinal3(){
//   sequence6();
//   sequence7();
// }
///////////////////////////////////////////////////////
// //This is Final Sequence 4
//////////////////////////////////////////////////////
//void sequenceFinal4(){
//   sequence6();
//   sequence8();
// }

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Linking Four SHift Register

Post by adafruit_support_mike »

Are you referring to the 8-count loop here?

Code: Select all

        for(int i = 0; i < 8; i++){
            updateLEDs(i);
            delay(delayTime);
            if(i == 9){
                updateLEDs(ledState == -5);
                Result();
            }
        }
If so, there are several ways to break out of a loop, but the best option is to use a while() loop rather than a for() loop:

Code: Select all

int i = 0;
int buttonPressCount = 0;

while ((i < 8) && (buttonPressCount != 2)) {
    updateLEDs(i);
    delay(delayTime);
    i++;
    buttonPressCount = checkButtons();
}
I stripped out this conditional:

Code: Select all

            if(i == 9){
                updateLEDs(ledState == -5);
                Result();
            }
because there's no way the variable 'i' can ever equal 9 in a loop that stops when 'i' equals 8. You probably just want to move the two lines in the conditional out past the end of the loop.

The general idea is to check the buttons inside the loop, then carry the information about the button state back to the loop's "should I keep going?" condition.

bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

Post by bob500000 »

Mike, Thanks, you've been really helpful,

Just one last question, when I press the second button I need it to run the test2 method which is shown below, as you can see I have re-enigneered the code so that it could run on shift registers 3 & 4,

However when I run them nothing happens, I know its running through the loop as I have put a serial.println on the method, still no joy, any ideas what is going wrong?

Also on your last sample, you identified a method checkbuttons which hasn't been define in the code, did you mean check_switches?

Code: Select all

////  /////////////////////////////////////////////////////
//// // Test2
//// /////////////////////////////////////////////////////
void updateLEDs2(int value){
  digitalWrite(latchPin2, LOW);     //Pulls the chips latch low
  shiftOut(dataPin2, clockPin2, MSBFIRST, value); //Shifts out the 8 bits to the shift register
  digitalWrite(latchPin2, 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 updateLEDsLong2(int value){
  digitalWrite(latchPin2, LOW);    //Pulls the chips latch low
  for(int i = 0; i < 16; i++){  //Will repeat 8 times (once for each bit)
  int bit = value & B1000000; //We use a "bitmask" to select only the eighth 
                               //bit in our number (the one we are addressing this time thro
                    //ugh
  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(dataPin2, HIGH);} //if bit 8 is set then set our data pin high
  else{digitalWrite(dataPin2, LOW);}            //if bit 8 is unset then set the data pin low
  digitalWrite(clockPin2, HIGH);                //the next three lines pulse the clock pin
  delay(1);
  digitalWrite(clockPin2, LOW);
  }
  digitalWrite(latchPin2, 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 bits2[] = {B00000001, B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000};
int masks2[] = {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 changeLED2(int led, int state){
   ledState2 = ledState2 & masks2[led];  //clears ledState of the bit we are addressing
   if(state == ON){ledState2 = ledState2 | bits2[led];} //if the bit is on we will add it to le
                    //dState
   updateLEDs2(ledState2);              //send the new LED state to the shift register
}

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Linking Four SHift Register

Post by adafruit_support_mike »

Those three routines are built to handle 8 LEDs, not 16. Here are versions built for 16 bits:

Code: Select all

void updateLEDs2(uint16_t value){
    digitalWrite(latchPin2, LOW);                           //Pulls the chips latch low
    shiftOut(dataPin2, clockPin2, MSBFIRST, value >> 8);    //Shifts the upper 8 bits of 'value'
    shiftOut(dataPin2, clockPin2, MSBFIRST, value);         //Shifts the lower 8 bits of 'value'
    digitalWrite(latchPin2, HIGH);                          //Pulls the latch high displaying the data
}

void updateLEDsLong2(uint16_t value){
    digitalWrite(latchPin2, LOW);

    for(int i = 16 ; i >= 0 ; i++) {        //  start at the high end and move to the low end
        if((value >> i) & 1) {              //  shift the i-th bit to the 1s position
            digitalWrite(dataPin2, HIGH);
        } else { 
            digitalWrite(dataPin2, LOW);
        }
        digitalWrite(clockPin2, HIGH);
        delay(1);
        digitalWrite(clockPin2, LOW);
    }
    
    digitalWrite(latchPin2, HIGH);
}

void changeLED2(int led, int state) {
    uint16_t bit = 1 << led;                //  all zeros except for a 1 at position 'led'
    ledState2 = ledState2 | bit;            //  set the bit high by default
    
    if(state == OFF) {
        ledState2 = ledState2 ^ bit;        //  set the bit low with an exclusive-OR
    }
    updateLEDs2(ledState2);
}
And while these functions should work for 16-bit sequences, you haven't showed the code that calls them.

bob500000
 
Posts: 54
Joined: Tue Nov 20, 2012 11:16 am

Re: Linking Four SHift Register

Post by bob500000 »

Hi Mike,

Thanks for that the loop that I calling is the following,

Still not too sure what you meant by checkbuttons.

cheers

Code: Select all

void loop() {
 /////////////////////////////////////////////////////
 //This is Button 1 Set Press
 /////////////////////////////////////////////////////
 updateLEDs(ledState == -5);
  idle();
  if(justpressed[0])
  {
    updateLEDs2(ledState == -5);
    Serial.print("Button 1 pressed");
    btnOn = true;
    idleoff();
  int delayTime = 2000; //the number of milliseconds to delay between LED updates

int buttonPressCount = 0;
while ((i < 8) && (buttonPressCount != 2)) {
    updateLEDs(i);
    delay(delayTime);
    i++;
    buttonPressCount = checkButtons();
}
  
  
  }

//  /////////////////////////////////////////////////////
// //This is Button 2 Set Press
// /////////////////////////////////////////////////////
if(justpressed[1])
  {
    updateLEDs2(ledState == -5);
    updateLEDs(ledState == -5);
    Serial.print("Button 2 pressed");
    btnOn = true;
    idleoff();
  int delayTime = 2000; //the number of milliseconds to delay between LED updates
  for(int i = 0; i < 16; i++){
    Serial.print("For");
  updateLEDs2(i);
  delay(delayTime);
  }
}

if(i == 9){
                updateLEDs(ledState == -5);
                Result();
            }

}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”