I2C and SD issue

Hi,

I was wondering if anyone could see a problem with my technique or with my code that explains why my output isn't printing to my SD card (no file is created), and why my compass output is not printing to my serial monitor. I've used these bits of code before and have not had any issues. I've also verified that all of the peripherals are working properly, and are properly connected

below is the code,

[code\]
#include <NewSoftSerial.h>
#include <Wire.h>
#include <SD.h>
const int CS=8;
int HMC6352slave =0x42;
int HMC6352read = 0x41;
int heading;
long id = 1;
#include <Time.h>
#include <TimeAlarms.h>
#include <TextFinder.h>
NewSoftSerial gps(3,4);
TextFinder finder(gps);
const int NUMBER_OF_FIELDS = 8;
int values[NUMBER_OF_FIELDS];
void setup()
{
  Serial.begin(9600);
  gps.begin(9600);
  Serial.println("bloop");
  Alarm.timerRepeat(10, RepeatTask);
  Wire.begin();
  pinMode(8,OUTPUT);
  if (!SD.begin(CS)) {
    Serial.println("Card failed, or not present");
    return;
}
}
void RepeatTask()
{
 
  Serial.println(millis());
  Serial.println("");
  
  char mycharpointer[7];
  char com[]=",";
  char buff[90];
  int length=90;
  finder.find("$GPGGA,");
  finder.getString( "$GPGGA,", "\r\n", buff, length);


Wire.beginTransmission(HMC6352slave);
Wire.send(HMC6352read);
Wire.endTransmission();
delay(10);
Wire.requestFrom(HMC6352slave, 2);
byte MSB = Wire.receive();
byte LSB = Wire.receive();
float headingSum = (MSB<<8) + LSB;
float headingInt = headingSum/10;
sprintf(mycharpointer,"%f",headingInt);
strcat(buff,com);
strcat(buff,mycharpointer);
Serial.println(buff);
Serial.println(headingInt);
File datafile = SD.open("datalog.txt",FILE_WRITE);
 if (datafile) {
   datafile.println(buff);
   datafile.close();

  }
  
}

void loop()
{
  Alarm.delay(1000);
}

below is what I get on the serial monitor. All of it prints, except for the last value, which, since I've tested, should be around 78.9

bloop
240001

190106.000,bleeh.9037,N,blahhh.0006,W,2,7,1.30,161.5,M,-33.3,M,0000,0000*62,?
0.00

any advice or insights are much appreciated!

  char* prac;
strcat(buff,prac);

You are dereferencing a NULL pointer. Why?

I found some careless errors, so the code and output has been changed. The only issue I am now having is with printing the compass value, as you can see it only prints "?" where I attempted to append the value to the string "buff"

PaulS

sorry for the slopiness, that was part of something I was trying the other day, I forgot to remove the assignments. I've since cleaned it up and modified the post

sprintf(mycharpointer,"%f",headingInt);

The %f format specifier is not supported on the Arduino.

Look into dtostrf(), instead.

PaulS,

trying dtostrf() for some reason is hiding all of my output except for the compass measurement.

the rest of the code is the same, I've only replaced the sprintf with this

dtostrf(headingInt,100,2,buff);

the output is this

bloop
10001

                                                                                                0.00
0.00

any idea what is going on here? I've tried various sizes to make "buff" but the output is always the same

trying dtostrf() for some reason is hiding all of my output except for the compass measurement.

Quit stomping on what is already in buff. You should be using mycharpointer (which is a lousy name since the variable is NOT a pointer).

ok, I get the syntax for dtostrf() now. Thanks for your help! Here's the finished code. Feel free to critique it further if you like, but everything seems to be working great now

#include <NewSoftSerial.h>
#include <Wire.h>
#include <SD.h>

const int CS=8;
int HMC6352 =0x42;
int slave;
byte headingData[2];
int i,headingValue;
long id = 1;
#include <Time.h>
#include <TimeAlarms.h>
#include <TextFinder.h>
NewSoftSerial gps(3,4);
TextFinder finder(gps);
const int NUMBER_OF_FIELDS = 8;
int values[NUMBER_OF_FIELDS];
void setup()
{
  slave =HMC6352>>1;
  Serial.begin(9600);
  gps.begin(9600);
  Serial.println("bloop");
  Alarm.timerRepeat(10, RepeatTask);
  Wire.begin();
  pinMode(8,OUTPUT);
  if (!SD.begin(CS)) {
    Serial.println("Card failed, or not present");
    return;
  }
}
void RepeatTask()
{

  Serial.println(millis());
  Serial.println("");

  char mychar[7];
  char com[]=",";
  char buff[100];
  int length=100;
  finder.find("$GPGGA,");
  finder.getString( "$GPGGA,", "\r\n", buff, length);

  strcat(buff,com);

  Wire.beginTransmission(0x21);
  Wire.send("A");     
  delay(100);         
  Wire.requestFrom(0x21, 2); 
  byte MSB = Wire.receive(); 
  byte LSB = Wire.receive(); 
  Wire.endTransmission();
  float myres = ((MSB << 8) + LSB) / 10;
  delay(100);
  dtostrf(myres,6,2,mychar);
  strcat (buff,mychar);
  File datafile = SD.open("datalog.txt",FILE_WRITE);
  if (datafile) {
    datafile.print(buff);

    datafile.close();
    Serial.println(buff);
  }

}

void loop()
{
  Alarm.delay(1000);
}

I'd like to see you use some functions. The I2C stuff could be put in a function that returns a float, with a name that describes what it is getting data from.

I'd like to see a few more comments in the code.

I'd prefer to see all the #include statements together at the top.

But, the most important thing is that the code works. Learning to create and call functions is useful. Organizing the code is useful. Leaving functioning code alone is good, too, though.