Go Down

Topic: OpenLog questions and sample code (Read 1 time) previous topic - next topic

Clarkcontr

First let me start by saying "long time listener, first time caller"  I am trying to learn C and write good Code, easily understood and modular. With modular testing in mind I am trying to build a simple function I can use in a larger program to log time and temperature to a SD card.   So with that in mind I want create a file, and then write to that file logging time and temperature every minute.  At this point I have updated the firmware of OpenLog and have it doing the test sketch, I have written a simple sketch myself to write data to the card.  I have not been able to create a file or add to it.  Here is my code, I have tried to keep it really simple you can see what didn't work commented out.

Thank you in advance for looking at this.
Code: [Select]
/*

Arduino TX to OpenLog RXI
Arduino 5V to OpenLog VCC
Arduino GND to OpenLog GND

A simple test sketch to show writing to a file

*/
#include <SoftwareSerial.h>
#include <SdFat.h>
SdFile file;

int ledPin =  13; //Status LED connected to digital pin 13
int i =0;         // declare i an integer
SdFat sd;
SdFile myFile;

void setup()
{ //beginning void setup
  pinMode(ledPin, OUTPUT);   // set the Pin to output 
  Serial.begin(9600);        //Serial.begin(9600); //9600bps is default for OpenLog
  delay(1000);               //Wait a second for OpenLog to init
 

//Works with Arduino v1.0              This section does not work at all
// Serial.print(26);
// Serial.print(26);
// Serial.print(26);
// new myFile;
// myFile.open("AAAATEST.TXT");
 
  for(int i = 1 ; i < 10 ; i++){          // beginning for loop
  Serial.print(i);                        // prints the number of the loop
  Serial.println("Excellent First Test"); // junk line for something to print and test
  }                                       // end for loop
 
}// end void setup
void loop()
{// beginning void loop
}// end void loop

PaulS

Where have you initialized the SD device? Nothing happens automatically just because the hardware is connected.

Clarkcontr

Well actually the code does work, it prints the 1-9 for loop, so sorry for my ignorance but I didn't see any ititilization in the OpenLog setup sketch and so didn't include any.  Suggestions for code I will happily work in, compile and test. 

Clarkcontr


xazirhs

#4
Mar 22, 2013, 06:41 pm Last Edit: Mar 22, 2013, 07:00 pm by xazirhs Reason: 1
Hi, I am having the same problem, I bought the openlog and I haven't been able to use it. First how do we connect it to arduino Uno so that I can make it to create a csv file and then every 3 or 4 minutes add the data from some sensors to the file? I have searched the web but I haven't found a solution. I have an example from http://www.jeremyblum.com/2011/04/05/tutorial-11-for-arduino-sd-cards-and-datalogging/ but he is using SPI connection.
Code: [Select]
//Program by Jeremy Blum
//www.jeremyblum.com
//SD Card Demonstration
//Some code from public domain work by Tom Igoe

#include <SD.h>         //SD Card Library
#include <Wire.h>       //I2C Library

//SPI SD Card Pins
  //MOSI = Pin 11
  //MISO = Pin 12
  //SCLK = PIN 13
  int CS_pin = 10;
  int pow_pin = 8;
 
//I2C Temperature Pins
  //SDA = Analog Pin 4
  //SCL = Analog Pin 5
 
//IR Distance Sensor Pins
  int IR1_pin = 2;
  int IR2_pin = 3;
 
//Light Sensor Pins
  int light_pin = 1;

float refresh_rate = 0.0;  //Dataloger Refresh Rate
int temp_address = 72;     //Address of the I2C Temp Sensor
long id = 1;                //Use this to store the id # of our reading.

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println("Initializing Card");
  //CS Pin is an output
  pinMode(CS_pin, OUTPUT);
 
  //SD Card will Draw Power from Pin 8, so set it high
  pinMode(pow_pin, OUTPUT); 
  digitalWrite(pow_pin, HIGH);
 
  //Initialize Card
  if (!SD.begin(CS_pin))
  {
      Serial.println("Card Failure");
      return;
  }
  Serial.println("Card Ready");
 
  //Read the Configuration information (COMMANDS.txt)
  File commandFile = SD.open("COMMANDS.txt");
  if (commandFile)
  {
    Serial.println("Reading Command File");
   
    float decade = pow(10, (commandFile.available() - 1));
    while(commandFile.available())
    {
      float temp = (commandFile.read() - '0');
      refresh_rate = temp*decade+refresh_rate;
      decade = decade/10;
    }
    Serial.print("Refresh Rate = ");
    Serial.print(refresh_rate);
    Serial.println("ms");
    commandFile.close();
  }
  else
  {
    Serial.println("Could not read command file.");
    return;
  }
 
  //Write Log File Header
  File logFile = SD.open("LOG.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(", , , ,"); //Just a leading blank line, incase there was previous data
    String header = "ID, Light, Temp, IR1, IR2";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
 
}

void loop()
{
  //Check Light Level
  int light_level = analogRead(light_pin);
 
  //Read Temperature
  Wire.beginTransmission(temp_address); //Start talking
  Wire.send(0); //Ask for Register zero
  Wire.endTransmission(); //Complete Transmission
  Wire.requestFrom(temp_address, 1); //Request 1 Byte
  while(Wire.available() == 0); //wait for response
  int temp_c = Wire.receive(); // Get the temp
  int temp_f = round(temp_c*9.0/5.0 +32.0);  //Convert to stupid American units
 
  //Read Distances
  int IR1_val = analogRead(IR1_pin);
  int IR2_val = analogRead(IR2_pin);
 
  //Create Data string for storing to SD card
  //We will use CSV Format 
  String dataString = String(id) + ", " + String(light_level) + ", " + String(temp_f) + ", " + String(IR1_val) + ", " + String(IR2_val);
 
  //Open a file to write to
  //Only one file can be open at a time
  File logFile = SD.open("LOG.csv", FILE_WRITE);
  if (logFile)
  {
    logFile.println(dataString);
    logFile.close();
    Serial.println(dataString);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
 
  //Increment ID number
  id++;
 
  delay(refresh_rate);
}

can you help me please or point me something to read about it?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy