Shift register to fade LED's on and keep them on until input

ok so an update, thanks for you advice, everything is running swimmingly although I do ask for your help once again.

So have two buttons that both need the option to be pressed twice, what I am after is setting a shift register to run the LSB outputs which produces 4 led's lit, then pressing the same button runs the method for MSB outputs to be lit, the things is I have set a boolean value to show when the button is pressed and when its not but can't for the life of me work out why its not doing what I want.

Do you have any ideas?

Heres my code so far.

Cheers :slight_smile:

#include <Button.h>

int latchPin = 5;
int clockPin = 6;
int dataPin = 4;
int outputEnablePin = 3;
//Button trigger = Button(2,LOW);

byte leds = 0;
byte button[] = {2, 8, 9};
byte switch_value = 0;
boolean btnOn;

#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;
          //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 Press
 /////////////////////////////////////////////////////
if (justpressed[0]) {
  button1();
  btnOn = true;
  justpressed[0] = 0;
  }
 /////////////////////////////////////////////////////
 //This is Button 2 Press
 /////////////////////////////////////////////////////
  
if (justpressed[1]) {
  button2();
  //btnOn = false;
 justpressed[1] = 0;
  }
  
 /////////////////////////////////////////////////////
 //This is Button 1 Pressed Twice
 ////////////////////////////////////////////////////
if (justpressed[0] && (btnOn == true)) {  
  button2();
 justpressed[0] = 0;
 switch_value = 0;
}
/////////////////////////////////////////////////////
 //This is Button 2 Pressed Twice
 ////////////////////////////////////////////////////
btnOn=false;
}
    

void updateShiftRegister()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, LSBFIRST, leds);// Change the start LED to 1
   digitalWrite(latchPin, HIGH);
}

void updateShiftRegister2()
{
   digitalWrite(latchPin, LOW);
   shiftOut(dataPin, clockPin, MSBFIRST, leds);// Change the start LED to 8
   digitalWrite(latchPin, HIGH);
}

void setBrightness(byte brightness) // 0 to 255
{
  analogWrite(outputEnablePin, 255-brightness);
}

void button1(){
  for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister();
    delay(50);
    for (byte b = 0; b < 255; b++)
  {
    setBrightness(b);
    delay(20);
  }
  if (justpressed[1]) {
  break;
  }
  }
  //bitSet(leds,0);
  //delay(1000); 
   
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}

void button2(){
for (int i = 0; i < 4; i++)
  {
    bitSet(leds, i);
    updateShiftRegister2();
    delay(50);
    for (byte b = 0; b < 255; b++)
  {
    setBrightness(b);
    delay(20);
  }
  if (justpressed[0]) {
  break;
  }
  }
  //bitSet(leds,0);
  //delay(1000); 
   
 leds = 0;
   //switch_value = 0;
  setBrightness(255);
}