Data Logging sketch for Mega

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
fat16lib
 
Posts: 595
Joined: Wed Dec 24, 2008 1:54 pm

Data Logging sketch for Mega

Post by fat16lib »

I have posted a sketch, MegaLogger.pde, that will run with no jumper wires on a Mega Arduino with the Data logging shield.

It will time stamp files and logs the data, time and analog data.

It uses software I2C and SPI on the Mega. It also runs on a UNO and uses hardware I2C and SPI.

I have included the libraries I used with the sketch.

The file is AdafruitMega20120329.zip located here http://code.google.com/p/beta-lib/downloads/list.

brantel
 
Posts: 10
Joined: Thu Mar 01, 2012 9:13 am

Re: Data Logging sketch for Mega

Post by brantel »

Yep it works great and can be very fast if you want!

Donziboy2
 
Posts: 32
Joined: Sun Feb 26, 2012 1:45 pm

Re: Data Logging sketch for Mega

Post by Donziboy2 »

NICE !!!!!!!

User avatar
rob drizzle
 
Posts: 127
Joined: Wed May 04, 2011 6:14 pm

Re: Data Logging sketch for Mega

Post by rob drizzle »

Not only does it work perfectly, it's easy to use and understand! Thank you for your hard work :wink:

Trueman
 
Posts: 1
Joined: Tue Nov 06, 2012 9:58 pm

Re: Data Logging sketch for Mega

Post by Trueman »

This does run well, I am using it to collect environmental data.
One question – the sync() function is not called anywhere in the code.
I have read that this must be called every time data is written to the card.
I don’t completely understand CD card programming but other examples I have seen do use the sync() function.
Possible it is called by the logger object, does nayone have any comments.

Thank you

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: Data Logging sketch for Mega

Post by adafruit_support_bill »

The sync() function flushes any partially filled buffers to the card. Otherwise, the data is only written when the buffer fills up. Calling sync() on every write prevents loss of unwritten data, but it takes time and will limit your max logging speed.

User avatar
lemuroid
 
Posts: 18
Joined: Wed Feb 02, 2011 5:35 pm

Re: Data Logging sketch for Mega

Post by lemuroid »

i am trying to your sketch to run on the DUE.

I keep getting:

In file included from C:\Documents and Settings\pcruz.CONFRMXP\My Documents\arduino-1.5.1r2\libraries\SdFat/SdFile.h:24,
from C:\Documents and Settings\pcruz.CONFRMXP\My Documents\arduino-1.5.1r2\libraries\SdFat/SdFat.h:26,
from AdafruitLogger.ino:3:
C:\Documents and Settings\pcruz.CONFRMXP\My Documents\arduino-1.5.1r2\libraries\SdFat/SdBaseFile.h:26: fatal error: avr/pgmspace.h: No such file or directory
compilation terminated.

what am i doing wrong?

gagnon_eric
 
Posts: 1
Joined: Tue Feb 19, 2013 9:55 pm

Re: Data Logging sketch for Mega

Post by gagnon_eric »

Thank you Thank you Thank you.
For a long time I was trying to make my mega1280 to use my ethernet and sd card ad the same time using SDFAT just like with my UNO and with your beta library, everything work fine now, no problem at all... :D

magican
 
Posts: 7
Joined: Tue Oct 22, 2013 10:24 am

Re: Data Logging sketch for Mega

Post by magican »

Hi,

has the file been removed?
We seem to be having issues connecting our Data Logging Shield 1141 to our Arduino Mega 2560 R3.
Is it correct that the ChipSelect Pin (10 on the Data Logger) needs to go into Pin 53 of the Arduino?

The RealTImeClock does work, the Logger however won't... Can anybody help setting this up?
Thanks in advance

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: Data Logging sketch for Mega

Post by adafruit_support_bill »

See the Mega instructions in the tutorial: http://learn.adafruit.com/adafruit-data ... d-leonardo

magican
 
Posts: 7
Joined: Tue Oct 22, 2013 10:24 am

Re: Data Logging sketch for Mega

Post by magican »

Thanks for the prompt reply!
I carefully followed the instructions and now the RTC as well as the Data Logger seem to be working fine.

Now we are unfortunately also using a RGB LCD Shield also supplied by Adafruit, connected through I2C.
The RGB Library seems to be having issues with the new SD Library, since I am getting errors as follows:

Code: Select all

/Users/XX/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp: In function 'void spiSend(uint8_t)':
/Users/XX/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:54: error: 'SPI' was not declared in this scope
/Users/XX/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp: In function 'uint8_t spiRec()':
/Users/XX/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:81: error: 'SPI' was not declared in this scope
/Users/XX/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp: In member function 'uint8_t Sd2Card::init(uint8_t, uint8_t, int8_t, int8_t, int8_t)':
/Users/XX/Documents/Arduino/libraries/SD/utility/Sd2Card.cpp:326: error: 'SPI' was not declared in this scope
I now tried to restore to my old SD library under which it compiled just fine before using the new Data Logger and Arduino Mega, unfortunately with no success... Can you help? Is there any compatible LCD Library I can use?

Your support is greatly appreciated!

User avatar
adafruit_support_bill
 
Posts: 88037
Joined: Sat Feb 07, 2009 10:11 am

Re: Data Logging sketch for Mega

Post by adafruit_support_bill »

There should not be any compatibility issues between these libraries. Post the code you are using.

User avatar
spiney
 
Posts: 214
Joined: Mon Jul 09, 2012 6:35 am

Re: Data Logging sketch for Mega

Post by spiney »

Just changed from a Uno to a Mega 2560. I followed the instructions in the post above and installed the new SD library. I get these faults. Please can you spot my error?

Code: Select all

Pressrevert2111:26: error: 'File' does not name a type
Pressrevert2111:27: error: 'File' does not name a type
Pressrevert2111.ino: In function 'void setup()':
Pressrevert2111:102: error: 'SD' was not declared in this scope
Pressrevert2111:113: error: 'SD' was not declared in this scope
Pressrevert2111:115: error: 'logfile' was not declared in this scope
Pressrevert2111:115: error: 'FILE_WRITE' was not declared in this scope
Pressrevert2111:120: error: 'logfile' was not declared in this scope
Pressrevert2111:136: error: 'SD' was not declared in this scope
Pressrevert2111:138: error: 'rainfile' was not declared in this scope
Pressrevert2111:138: error: 'FILE_WRITE' was not declared in this scope
Pressrevert2111.ino: In function 'void loop()':
Pressrevert2111:172: error: 'logfile' was not declared in this scope
Pressrevert2111:173: error: 'rainfile' was not declared in this scope
Pressrevert2111:206: error: 'rainfile' was not declared in this scope
Pressrevert2111.ino: In function 'void Repeats()':
Pressrevert2111:240: error: 'logfile' was not declared in this scope
Pressrevert2111.ino: In function 'void Repeat3()':
Pressrevert2111:325: error: 'logfile' was not declared in this scope
Here is the code up to Loop.

Code: Select all

#include <avr/wdt.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_MCP23017.h>
//#include <Adafruit_BMP085.h>
#include <RTClib.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <Adafruit_RGBLCDShield.h>
#include <PinChangeInt.h>
#include <PinChangeIntConfig.h>
#define PIN 3  // the pin for button/ rain gauge
#define STOP_SWITCH 2
 bool stopped = false;
RTC_DS1307 RTC;
time_t syncProvider()     //this does the same thing as RTC_DS1307::get()
{
  return RTC.now().unixtime();
}
uint32_t syncTime = 0; // time of last sync()

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 53;

// the logging files
File logfile;
File rainfile;
void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
  while(1);
}

volatile int flag=0;
int nflag;
int Run = 0;
int test = 1;
int nutim = 0;
int oldtim = 0;
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();

#define BMP085_ADDRESS 0x77  // I2C address of BMP085
const unsigned char OSS = 1;  // Oversampling Setting
// Calibration values
int ac1;
int ac2; 
int ac3; 
unsigned int ac4;
unsigned int ac5;
unsigned int ac6;
int b1; 
int b2;
int mb;
int mc;
int md;
// b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
// so ...Temperature(...) must be called before ...Pressure(...).
long b5; 
short temperature;
long pressure;
byte fetch_humidity_temperature(unsigned int *p_Humidity, unsigned int *p_Temperature);
float maxT;
float minT = 90;
float maxTy;
float minTy;
byte _status;
unsigned int H_dat, T_dat;
float RH, T_C;

long zerosec = millis();
//Adafruit_BMP085 bmp;

void setup()
{
  Serial.begin(9600);
   /* if (!bmp.begin()) {
	Serial.println("Could not find a valid BMP085 sensor, check wiring!");
	while (1) {}
  }
  */
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  Wire.begin();
  RTC.begin();
  if (! RTC.isrunning()) {
  RTC.adjust(DateTime(__DATE__, __TIME__));
  }
  RTC.adjust(DateTime(__DATE__, __TIME__));
  setSyncProvider(syncProvider);     //reference our syncProvider function instead of RTC_DS1307::get()

  //configure pin2 as an input and enable the internal pull-up resistor
      pinMode(STOP_SWITCH, INPUT_PULLUP);
      
  // initialize the SD card
  Serial.print("InitSD");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, 11, 12, 13 OUTPUT);

  // see if the card is present and can be initialized:
  if (!SD.begin(10,11,12,13)) 
  {
    error("Cd fail");
  }
  Serial.println("cdinit.");
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) 
  {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!    
    }
  }

  if (! logfile) {
    error("No lgfile");
  }

  Serial.print("Log to ");
  Serial.println(filename);
  Serial.println();
  
   bmp085Calibration();

  // create a new file
  char filename2[] = "RAINEY00.CSV";
  for (uint8_t i = 0; i < 100; i++) 
  {
    filename2[6] = i/10 + '0';
    filename2[7] = i%10 + '0';
    if (! SD.exists(filename2)) {
      // only open a new file if it doesn't exist
      rainfile = SD.open(filename2, FILE_WRITE); 
      break;  // leave the loop!
    }
  }
//This should work but does not - why?
 /*  if (! rainfile) {
   error("couldnt create rnfile");
   }
  */ 
  Serial.print("Log to ");
  Serial.println(filename2);
  Serial.println();

  pinMode(PIN, INPUT);     //set the pin to input
  digitalWrite(PIN, LOW); //use the internal pullup resistor
  PCintPort::attachInterrupt(PIN, burpcount,RISING); // attach a PinChange Interrupt to our pin on the rising edge
  // (RISING, FALLING and CHANGE all work with this library)
  // and execute the function burpcount when that pin changes 

 Repeats();
/*  while (minute() != 0) //Prog holds here until the hour.
  {
    Alarm.delay(1000);
  }
*/ 
  Alarm.timerRepeat(120, Repeats);
  Alarm.timerRepeat(14, Repeat2);
  Alarm.alarmRepeat(0,1,0, Repeat3);
 
  wdt_enable(WDTO_8S);

User avatar
spiney
 
Posts: 214
Joined: Mon Jul 09, 2012 6:35 am

Re: Data Logging sketch for Mega

Post by spiney »

Adding

Code: Select all

#include <SPI.h>
seems to have allowed the code to load but the LCD is not showing anything.

User avatar
spiney
 
Posts: 214
Joined: Mon Jul 09, 2012 6:35 am

Re: Data Logging sketch for Mega

Post by spiney »

I have been confused with the statement that the LCD shield is compatible with the Mega R3.
It will also work perfectly with Arduino Mega R3's which have the extra SDA/SCL I2C pins broken out. Earlier Mega's have the I2C pins in a different location and will require you to solder two wires from the I2C pins on the shield and plug them into the different I2C locations at Digital 20 & 21.
But I had to wire the Mega's SCL & SDA to pins 5 & 4 of the logger and LCD shield to make them work, so is it not really compatible? (The LCD shield is sitting on the self-built logger model.)
Anyway the LCD is now working, but the interrupt on pin 3 does not work - it did with the UNO.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”