Go Down

Topic: I2C and SD issue (Read 543 times) previous topic - next topic

btricha2

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

Code: [Select]
dtostrf(headingInt,100,2,buff);

the output is this

Code: [Select]
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

PaulS

Quote
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).

btricha2

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

Code: [Select]

#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);
}

PaulS

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.

Go Up