Thank you everyone for your info. i understand much better.
I guess it might be helpful to someone if I explain my project a little. I am reading a linear sensor with 512 pixels. I need to record each pixel intensity using the ADS1115 16 bit ADC from Adafruit (as you will see in the code). I used the example on arduino's website (Tutorial on Data logging with Arduino)
The issue is I need to scan the sensor 6 times for each set of data that will be sent to an SD card, and this happens very fast. I need to do this every two minutes as a requirement for my project:
//Libraries included in code
#include <Wire.h>
#include <SPI.h>
#include <RTClib.h>
#include <RTC_DS1307.h>
#include <Adafruit_ADS1015.h>
#include <Ethernet2.h>
#include <EthernetUdp2.h>
//Peripheral components RTC(DS3231) and ADC(ADS1115)
RTC_DS1307 RTC;
Adafruit_ADS1115 ads1115;
//Define several global variables
const int PIXELS = 512; //number of pixels on linear sensor
int counter = 0; //counter to end after six scans
String dataString = "";
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(x,t,y,z); // UArizona NTP server
const 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
//---------------------------------BEGIN SETUP------------------------------------------------------\\
void setup(){
//Begin Serial at BAUD 1115200
Serial.begin(115200);
//Begin communication with DS3231 RTC
Wire.begin();
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__));
}
//Send Date from RTC to the Serial Port
dataString += String(now.year());
dataString += "-";
dataString += String(now.month());
dataString += "-";
dataString += String(now.day());
dataString += ",";
//Grab UDP Time and print to serial
// start Ethernet and UDP
if(Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
Udp.begin(localPort);
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
if (Udp.parsePacket()) {
// We've received a packet, read the data from it
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
// the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, extract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print the hour, minute and second:
dataString += String((epoch % 86400L)/3600);
dataString += ":";
if (((epoch % 3600) / 60) < 10) {
// In the first 10 minutes of each hour, we'll want a leading '0'
dataString += "0";
}
dataString += String((epoch % 3600) / 60);
dataString += ":";
if ((epoch % 60) < 10) {
// In the first 10 seconds of each minute, we'll want a leading '0'
dataString += "0";
}
dataString += String(epoch % 60);
dataString += ",";
}
//--------------------------------------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
}
//read ADS1115
adc = ads1115.readADC_Differential_0_1();
dataString += String(adc + 32787);
dataString += ",";
for (int i=0;i<=(PIXELS-pixel);i++) {
PORTD |= _BV(2); //clock high
delayMicroseconds(4);
PORTD &= ~_BV(2); //clock low
}
}
void loop(){
counter++; //count each time we scan the sensor
//first value is discarded from each scan
startTime(0);
delay(1);
//scan of pixel array
for (int i=0;i<PIXELS;i++) {
startTime(i);
delay(1);
}
//once sensor is scanned 6 times, wait two minutes, get time stamp and start over
if (counter == 6){
Serial.println(dataString);
delay(120000);
counter = 0;
dataString = ""; //clear dataString begin anew
//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__));
}
//Send Date from RTC to the Serial Port
dataString += String(now.year());
dataString += "-";
dataString += String(now.month());
dataString += "-";
dataString += String(now.day());
dataString += ",";
//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;
// print the hour, minute and second:
dataString += String((epoch % 86400L)/3600);
dataString += ":";
if (((epoch % 3600) / 60) < 10) {
// In the first 10 minutes of each hour, we'll want a leading '0'
dataString += "0";
}
dataString += String((epoch % 3600) / 60);
dataString += ":";
if ((epoch % 60) < 10) {
// In the first 10 seconds of each minute, we'll want a leading '0'
dataString += "0";
}
dataString += String(epoch % 60);
dataString += ",";
//}
}
}