//Libraries included in code
#include <Wire.h>
#include <SPI.h>
#include <RTC_DS1307.h>
#include <Adafruit_ADS1015.h>
#include <Ethernet2.h>
#include <EthernetUdp2.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 PROGMEM int PIXELS = 512; //number of pixels on linear sensor
byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0x21, 0xCB}; //MAC address for Ethernet Shield
unsigned int localPort = 8888; // local port to listen for UDP packets
IPAddress timeServer(128, 196, 128, 233); // UArizona NTP server
const PROGMEM int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
EthernetUDP Udp;// A UDP instance to let us send and receive packets over UDP
File myFile;
int Year;
int Month;
int Day;
int Hour;
int Minute;
int Second;
//---------------------------------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 pin 6 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)) {
return;
}
//get the day to display on lcd at open
getDayofWeek();
//set up opening menu for lcd screen
lcd.setCursor(0, 0);
lcd.print("RSG ARDUSPEC");
lcd.setCursor(0, 1);
lcd.print(Year); lcd.print("/"); lcd.print(Month); lcd.print("/"); lcd.print(Day);
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Calibration");
lcd.setCursor(0, 1);
lcd.print("Field Collect");
}
//-----------------------------------------------Begin Infinite Loop----------------------------------------------------------//
void loop() {
uint8_t buttons = lcd.readButtons();
if (buttons) {
if (buttons & BUTTON_UP) {
//get the time and date
getTimeandDate();
//Open file
myFile = SD.open("datalog.txt", FILE_WRITE);
delay(2000);
//Write time and Date to file
writeTimeandDate();
//initialize system for darks
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Taking Darks");
myFile.println("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.println("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) {
//get time and date
getTimeandDate();
//Open file
myFile = SD.open("datalog.txt", FILE_WRITE);
delay(2000);
//Write time and Date to file
writeTimeandDate();
//initialize for continuous mode
lcd.clear();
lcd.print("Continuous Mode");
delay(2000);
//notify user that Arduino is scanning array
lcd.clear();
lcd.print("Scanning");
button_press();
//Notify user that arduino is done scanning
lcd.clear();
lcd.print("Done Scanning");
//close file and wait two minutes to begin next scan
myFile.close();
delay(120000);
}
}
}
}
//--------------------------------------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.println(adc);
for (int i = 0; i <= (PIXELS - pixel); i++) {
PORTD |= _BV(2); //clock high
delayMicroseconds(4);
PORTD &= ~_BV(2); //clock low
}
}
//--------------------------------------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);
}
}
}
//----------------------------Take the Time and the Date-------------------------------//
void getTimeandDate() {
//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();
//Create new time stamp for next set of measurements
Udp.begin(localPort);
sendNTPpacket(timeServer);
delay(1000);
if (Udp.parsePacket()) {
Udp.read(packetBuffer, NTP_PACKET_SIZE);
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
unsigned long secsSince1900 = highWord << 16 | lowWord;
const unsigned long seventyYears = 2208988800UL;
unsigned long epoch = secsSince1900 - seventyYears;
//Calculate hour, minute, and second
Hour = ((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Minute = ((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Second = (epoch % 60); // print the second
}
}
void writeTimeandDate() {
//Print time and Date Stamp to file created above
myFile.print('"');
myFile.print(Year);
myFile.print('-');
myFile.print(Month);
myFile.print('-');
myFile.print(Day);
myFile.print(' ');
myFile.print(Hour); // print the hour (86400 equals secs per day)
myFile.print(':');
if (Minute < 10) {
// In the first 10 minutes of each hour, we'll want a leading '0'
myFile.print('0');
}
myFile.print(Minute); // print the minute (3600 equals secs per minute)
myFile.print(':');
if (Second < 10) {
// In the first 10 seconds of each minute, we'll want a leading '0'
myFile.print('0');
}
myFile.print(Second); // print the second
myFile.println('"');
}
void getDayofWeek() {
//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();
}
Actually I think I'm running into a compiler bug. Compiling the simple code:
void setup() {
Serial.begin(9600);
}
void loop() {
delay(1);
}
Sketch uses 4,244 bytes (14%) of program storage space. Maximum is 28,672 bytes.
Global variables use 151 bytes (5%) of dynamic memory, leaving 2,409 bytes for local variables. Maximum is 2,560 bytes.
I feel as though 4,244 bytes of program storage space is a bit too much?
Your mini sketch on a Nano on Win7/64 IDE 1.6.5:
Der Sketch verwendet 1.830 Bytes (5%) des Programmspeicherplatzes. Das Maximum sind 30.720 Bytes.
Globale Variablen verwenden 182 Bytes (8%) des dynamischen Speichers, 1.866 Bytes für lokale Variablen verbleiben. Das Maximum sind 2.048 Bytes.
The real IDE minimum sketch
void setup() {
}
void loop() {
}
gives
Der Sketch verwendet 450 Bytes (1%) des Programmspeicherplatzes. Das Maximum sind 30.720 Bytes.
Globale Variablen verwenden 9 Bytes (0%) des dynamischen Speichers, 2.039 Bytes für lokale Variablen verbleiben. Das Maximum sind 2.048 Bytes.
Thank you, I quit and restarted the application and it seems to be working as normal now. Thanks for your help!
It probably isn't, and far more to the point is the conspicuous lack of such detail about the programme that is causing the problem - not to mention what your objectives might be. As things are, the first ten lines would suggest the tip would be get a Mega. I notice though that you appear to have two timestamp subroutines, where one should suffice. Further, the srtn getTimeand Date seems to be very complicated for what it implies it is, but there may be good reason for that.
True Nick_Pyner, I lack detail in the first post, my apologies. the subroutine getTimeandDate is basically the UDPNTPclient example on the Arduino IDE. Only formatted slightly different.
Thank you for your time.