Pages: [1]   Go Down
Author Topic: Missing values from data string  (Read 586 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please view towards the end of my sketch.  I am able to return the IR value to my datacard as a .csv file.

The problem is that the date is only returned as a 3 - I guess for March.  Can anyone see why it is not bringing the full time and date across.  Is there an error with the datastring?

Code:
//Based on Example by Tom Igoe

#include <SD.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68

//Set by default for the SD Card Library
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
//We always need to set the CS Pin
int CS_pin = 8;
int pow_pin = 8;
//IR Distance Sensor Pins
  int IR1_pin = 0;
float refresh_rate = 0.0;

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println("Initializing Card");
  //CS Pin is an output
  pinMode(CS_pin, OUTPUT);
 
  //Card will Draw Power from Pin 8, so set it high
  pinMode(pow_pin, OUTPUT); 
  digitalWrite(pow_pin, HIGH);
 
  //check if card is ready
   
  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");
  }
  else
  {
    Serial.println("Could not read command file.");
    return;
  }
 
}

void loop()
{printDate();
  delay(1000);
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);

  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(month);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
 
  int value = analogRead(IR1_pin);

  if (value > 375 || value < 300)
  {
     Serial.println(value);
     

     delay(50);   //wait half a second, then check again.
String datastring = String(IR1_pin) + "," + (monthDay) + "/" + (month) + "/" + (year) + "," + (hour) + ":" + (minute) + ":" +(second);

     //Open a file to write to
     //Only one file can be open at a time
     int IR1_val = Serial.println(value);
     int dataString = Serial.println(dataString);
     File logFile = SD.open("LOG.CSV", FILE_WRITE | O_APPEND);
     if (logFile)
     {
        logFile.println(analogRead(IR1_pin));
       
        if (value > 375 || value < 300)
        {
         Serial.println(value);
          Serial.println(dataString);
  logFile.println(dataString);
 
     delay(50);   
       logFile.close();
     
     }
  }
}}
 


Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33419
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A very quick glance shows you are doing this:-
Code:
int dataString = Serial.println(dataString);
That is you are making a new integer variable called dataString that is replacing the old one. What is more you are assigning it to the value returned by the Serial.print statement.
Then you try and write this variable to the SD card which is not the variable you think it is.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the response.

When I remove the line you mention I have the following error -

'dataString' was not declared with scope.

How do I declare this please?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
String datastring = String(IR1_pin) + "," + (monthDay) + "/" + (month) + "/" + (year) + "," + (hour) + ":" + (minute) + ":" +(second);

     //Open a file to write to
     //Only one file can be open at a time
     int IR1_val = Serial.println(value);
     int dataString = Serial.println(dataString);
You really need to pay attention to what you are doing. Serial.println() returns a value. But that value is useless, as far as you are concerned. Why you think that the meaning of the value is somehow related to dataString is baffling, too.

Using names that differ only in case is also not a good idea. Now, what from this code snippet do toy really want to print to the serial port. Hint: it isn't dataString or datastrinG or DatAString or any other combination of upper and lower case letters.

Stop storing the return value from Serial.print() or Serial.println() in a variable.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 35
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Many thanks to you all - it works!  removed the serials and changed the capitals.
Logged

Pages: [1]   Go Up
Jump to: