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