Using Adafruit LCD and Button Kit to make menu options

First the code:

//Libraries included in code
#include <Wire.h>
#include <SPI.h>
#include <RTC_DS1307.h>
#include <Adafruit_ADS1015.h>
#include <SD.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>

//Peripheral components RTC(DS3231), ADC(ADS1115), LCD
RTC_DS1307 RTC;
Adafruit_ADS1115 ads1115;
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

//Define several global variables
#define WHITE 0x7
const int PIXELS = 512;  //number of pixels on linear sensor
File myFile;


//---------------------------------BEGIN SETUP------------------------------------------------------\\

void setup() {

  //Begin Serial at BAUD 1115200
  Serial.begin(115200);
  
  //Begin communication with LCD
  lcd.begin(16, 2);
  lcd.setBacklight(WHITE);
  
  //Begin communication with ADS1115 ADC
  ads1115.begin();

  //Begin communication with DS3231 RTC
  Wire.begin();
  RTC.begin();

  //initialize pins
  pinMode(2, OUTPUT); //clock pin
  pinMode(3, OUTPUT); //start pin
  pinMode(6, OUTPUT); //2.9V reference pin for ADC

  //Assert Default setting:
  analogReference(DEFAULT);

  // Set all IO pins low:
  for ( int i = 0; i < 14; i++ ) {
    digitalWrite(i, LOW);
  }

  //Set pins 6 and 12 high
  digitalWrite(6, HIGH); //2.9V reference for ADC
  
  //check to see if the SD card is there and will initialize
    if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }

  
    
    lcd.print("RSG Arduino Spec");
    lcd.setCursor(0, 1);
    delay(4000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Up=Calibrate");
    lcd.setCursor(0, 1);
    lcd.print("Down=Field Coll");
    delay(3000);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Calibration");
    lcd.setCursor(0, 1);
    lcd.print("Field Collect");
    
  }


//--------------------------------------PIXEL-SCAN--------------------------------------------------------\\

int startTime(int pixel) {
  int16_t adc;
  PORTD |= _BV(3); //start high
  PORTD |= _BV(2); //clock high
  delayMicroseconds(4); //delay for start pulse width
  PORTD &= ~_BV(2); //clock low
  PORTD &= ~_BV(3); //start low

  for (int i = 0; i < pixel; i++) { //clock goes from low to high to
    //shuffle through pixels, find the one we want
    PORTD |= _BV(2); //clock high
    delayMicroseconds(4);
    PORTD &= ~_BV(2); //clock low, need to read now
  }
  adc = ads1115.readADC_Differential_0_1() + 32787;
  myFile.print(adc); 
  myFile.print(',');
  for (int i = 0; i <= (PIXELS - pixel); i++) {
    PORTD |= _BV(2); //clock high
    delayMicroseconds(4);
    PORTD &= ~_BV(2); //clock low
  }
}

//-----------------------------------------------Begin Infinite Loop----------------------------------------------------------//

void loop() {
  uint8_t buttons = lcd.readButtons();
  if (buttons) {
    if (buttons & BUTTON_UP) {
      //initialize system for darks
      myFile.print("DARKS:");
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Taking Darks");
      
      //take measurements
      button_press();
      
      //now it is time to take lights
        lcd.clear();
        lcd.print("Press Select");
        lcd.setCursor(0, 1);
        lcd.print("to take Lights");
    }
    
    if (buttons & BUTTON_SELECT) {
   
      //begin lights for cal
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Taking Lights");
      myFile.print("LIGHTS:");
      
      //take measurements
        button_press();
      
      //end calibration, do nothing
      lcd.clear();
      lcd.print("Cal Done");
      myFile.close();
      while (1) {} 
    }
    
    if (buttons & BUTTON_DOWN) {
      while (1) {
        button_down();
  }
    }
  }
}
 
 //--------------------------------------Button Press Function-----------------------------------------------------// 
void button_press() {
  
      for (int i=0; i <= 6; i++) {
      //first value is discarded from each scan
      startTime(0);
      delay(1);

      for (int i = 0; i < PIXELS; i++) {
        startTime(i);
        delay(1);
      }
      } 
}

//-------------------------------------Button Down Function---------------------------------------------------------//
void button_down() {
      //begin continuous mode
      lcd.clear();
      lcd.print("Continuous Mode");
      delay(2000);
      Serial.println("Writing Data to File");
      
      //notify user that Arduino is scanning array
      lcd.clear();
      lcd.print("Scanning");
  
     for (int i=0; i <= 6; i++) {
      //first value is discarded from each scan
      startTime(0);
      delay(1);

      for (int i = 0; i < PIXELS; i++) {
        startTime(i);
        delay(1);
      }
      } 
  
    //Notify user that arduino is done scanning
      lcd.clear();
      lcd.print("Done Scanning");
      
      //now wait two minutes to begin next scan
      myFile.close();
      delay(120000);
      
    //create new timestamp
    RTC.begin();
    DateTime now = RTC.now();
    DateTime compiled = DateTime(__DATE__, __TIME__);
    if (now.unixtime() < compiled.unixtime()) {
      // following line sets the RTC to the date & time this sketch was compiled
      RTC.adjust(DateTime(__DATE__, __TIME__));
    }

    //Get Date from RTC
    Year = now.year();
    Month = now.month();
    Day = now.day();

    
    myFile = SD.open("datalog.txt", FILE_WRITE);
    lcd.clear();
    
}

I am using my Arduino to log data coming from a spectrometer. Among other things, the Arduino is using an external ADC to read data and write onto a microSD card. This all works, I've tried it several times. The issue now is that my project requires an LCD screen with some menu options. In the loop you can see I want two menu options "Calibration" and "Field Collect". I want to initiate "Calibration" by pressing the up button and "Field Collect" by pressing the down button. I am able to do this to an extent. The issue is the LCD displays what I want in order...for example, when I press up when prompted, the LCD reads the lcd.blah commands under

if (buttons & BUTTON_UP) {

but does not write data to the sd card. It seems that all the other commands are read like lcd.print and setCursor

So my question is: why aren't the data writing onto the SD card? Is it because the buttons are using some sort of interrupt? I know the SD card is working fine, I used it right before I added the LCD code to my script. Thanks for the help

Is it because the buttons are using some sort of interrupt?

Not that I can see.

You need to add some debug prints to see what is happening to your code.

Ok so I've added debug prints throughout the code and it seems that as soon as the IDE runs the

if (buttons) {

part, I lose all ability to interact with the Arduino except for through the LCD. Nothing else seems to work. I'm wondering if it has something to do with the SDA and SCL lines getting crossed somewhere or instructions not getting through in that respect. All else works it just seems that the LCD does not work with the other components of my set-up.

If I may add to this...I believe it might have something to do with memory (i.e. low memory stability problems may occur). If seems that the code stops working around the time the Arduino asks to open a file on the SD card. I think this is the stability concern.

One quick way to free up some memory is to use the F function on all literal strings in your code.
http://playground.arduino.cc/Learning/Memory

You have two 'while (1)' which will stop the code.

Awesome thanks Grumpy Mike

greenwald- yes the idea is that once a selection is confirmed (i.e. calibration or field collection) these will be the only settings until the arduino is reset. The point of the calibration for example is to take two sets of measurements, write to an sd and then its done, and the arduino must be reset. Field collection on the other hand has the idea of entering a loop that goes on forever, ie take measurements wait two minutes, take measurements, wait two minutes, over and over again.