74HC595 - multiplexed LEDs controlled by a button for a simple GUI

Hi

I’m currently working on a data logger that uses an external 12bit ADC and 3 internal ADC channels. Before it starts logging the user has to choose the interval time required for logging. and which channels he wants to log. I’m using 16 LEDs multiplexed with a 74HC595 as shown in this tutorial: http://www.instructables.com/id/Multiplexing-with-Arduino-and-the-74HC595/?ALLSTEPS

Now, the board also uses two buttons. One is a “plus” button and the other one is a “function” button. The plus button is used to toggle between 13 interval time LEDs and then, after accepting with the function button, use 4 out of 13 of these LEDs to represent the desired channel (and toggle in the same way). The other 3 LEDs are the state LEDs.

Here’s how it looks on the PCB side of things:

and here’s my code so far:

//include the SD card library
#include <SD.h>
#include <SPI.h>
//define SPI interface pins
#define MOSI 11
#define MISO 12
#define SCK 13
#define SD_CS 9
#define ADC_CS 10
//define the internal ADC pins
#define ADC1 A0
#define ADC2 A1
#define ADC3 A2
//define the 74HC595 pins
#define latchPin A3
#define clockPin A4
#define dataPin A5
//define the battery measuring input
#define BATT A6
//define the remaining pins
#define SD_DET 8 //SD card detection
#define SD_WP A7 //SD write protect
//Switches
#define FUN_SW 3
#define PLUS_SW 4

//looping variables
byte i;
byte j;

int ROW1;
int ROW2;
int ROW3;
int ROW4;

//storage for led states, 4 bytes
byte ledData[] = {ROW1, ROW2, ROW3, ROW4};
//define the plus button variables
int plusPushCounter = 0;
int plusState = 0;
int lastPlusState = 0;


int interval;

void setup () {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  
  pinMode(SD_CS, OUTPUT);
  pinMode(ADC_CS, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(SCK, OUTPUT);
  pinMode(MISO, INPUT);
  
  pinMode(BATT, INPUT);
  
  pinMode(SD_DET, INPUT);
  pinMode(SD_WP, INPUT);
  
  pinMode(FUN_SW, INPUT);
  pinMode(PLUS_SW, INPUT);
  
  pinMode(ADC1, INPUT);
  pinMode(ADC2, INPUT);
  pinMode(ADC3, INPUT);
  
  Serial.begin(9600);
  
  delay(100);
  
  if (analogRead(BATT) <400) {
    //TURN ON THE BAT LED
    int ROW4 = 1;
  }
   
  
  if (!SD.begin(SD_CS)) {
    Serial.println("Card failed, or not present");  
    int ROW4 = 3;
    // don't do anything more:
    return;
  }
  Serial.println("Card ready");
  //BUTTON, INTERVAL SELECTION, CHANNEL SELECTION, START LOGGING IN THE LOOP
  
  
  
}

void loop() {
    for (i=0;i<4;i++){
    
    //send data from ledData to each row, one at a time
    byte dataToSend = (1 << (i+4)) | (15 & ~ledData[i]);
      
    // setlatch pin low so the LEDs don't change while sending in bits
    digitalWrite(latchPin, LOW);
    
//    // shift out the bits of dataToSend to the 74HC595
//    shiftOut(dataPin, clockPin, LSBFIRST, dataToSend);
// the code below is the equivalent of the two lines above
    for (j=0;j<8;j++){
      digitalWrite(clockPin,LOW);
      digitalWrite(dataPin,((dataToSend>>j)&1));
      digitalWrite(clockPin,HIGH);
     
    }
}

}

Now I would appreciate some help with my code. How do I actually get the button to toggle between those LEDs? (I want the LEDs to flash when a given interval time or channel is selected for edit and stay on, when you accept it with the function button, also I want them to all turn off when the logging is started to reduce current consumption). I assume all of this has to be done in the void setup () part of the code, as it actually is setting up the data logger before logging, right? The main problem is that it has to toggle like this :

  1. ROW1 = 1, then 2, then 4, then 8 (the corresponding LED is blinking)
  2. ROW2 = 1, then 2, then 4, then 8
  3. ROW3 = 1, then 2, then 4, then 8
  4. ROW 4 = 4 (the other values are the 3 state LEDs)
    and go back to the first row.
    5.When a user presses the function button it accepts it as the interval, it turns on the LED (stops the blinking) and goes to the channel selection mode
  5. ROW1 = 1 - 1st channel
  6. ROW2 = 8 - 2nd channel
  7. ROW3 = 8 - 3rd channel
  8. ROW4 = 4 - 4th channel
    The channel LED should blink, which means it can be edited and stay on when the channel is turned on
    Something like this: http://www.youtube.com/watch?v=aF0z0qT7OUE&feature=youtu.be&t=2m24s
  9. After long pressing the function button the data logger starts logging (sets up the SPI interface for the ADC, creates the file on the SD card etc.)

My other concern is, that the code will end up being too big for the 30kB of flash on Arduino UNO.

All help is much appreciated.