Ok, sono arrivato ad un binario morto, ho riscritto parte del codice del sismografo, mancano ancora tutte le funzioni di conversione ma quelle non sono un problema.
Non capisco dove sbaglio, i dati d (xyz) dichiarati alla fine non vengono salvati nel file che viene creato, sapreste dirmi dove sbaglio ? la sintassi è corretta l'unica mia perplessità riguarda la scrittura sul file usando appunto la SDfat...
#include <Wire.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#define address 0x40
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
void initBMA180()
{
int temp, result, error;
Wire.beginTransmission(address);
Wire.send(0x00);
Wire.requestFrom(address, 1);
while(Wire.available())
{
temp = Wire.receive();
}
Serial.print("Id = ");
Serial.println(temp);
result = Wire.endTransmission();
checkResult(result);
if(result > 0)
{
error = 1;
}
delay(10);
if(temp == 3)
{
Wire.beginTransmission(address);
Wire.send(0x0D);
Wire.send(B0001);
result = Wire.endTransmission();
checkResult(result);
if(result > 0)
{
error = 1;
}
delay(10);
Wire.beginTransmission(address);
Wire.send(0x20);
Wire.send(B00001000);
result = Wire.endTransmission();
checkResult(result);
if(result > 0)
{
error = 1;
}
delay(10);
Wire.beginTransmission(address);
Wire.send(0x35);
Wire.send(B0100);
result = Wire.endTransmission();
checkResult(result);
if(result > 0)
{
error = 1;
}
delay(10);
}
if(error == 0)
{
Serial.print("BMA180 Init Successful");
}
}
void checkResult(int result)
{
if(result >= 1)
{
Serial.print("PROBLEM..... Result code is ");
Serial.println(result);
}
else
{
Serial.println("Read/Write success");
}
}
void readId()
{
int temp, result;
Wire.beginTransmission(address);
Wire.send(0x00);
Wire.requestFrom(address, 1);
while(Wire.available())
{
temp = Wire.receive();
}
Serial.print("Id = ");
Serial.println(temp);
result = Wire.endTransmission();
checkResult(result);
delay(10);
}
int readAccel3(byte msbAddr, byte lsbAddr)
{
short temp, result, temp2;
byte lsb, msb = 0;
temp2 = 0;
temp = 0;
while(temp != 1)
{
Wire.beginTransmission(address);
Wire.send(msbAddr);
Wire.requestFrom(address, 1);
while(Wire.available())
{
msb = Wire.receive();
temp = msb & 0x01;
}
}
Wire.beginTransmission(address);
Wire.send(lsbAddr);
Wire.requestFrom(address, 1);
while(Wire.available())
{
lsb = Wire.receive();
temp |= lsb;
temp = temp >> 2;
}
result = Wire.endTransmission();
msb = msb >> 2;
if(lsb & B10000000){
lsb &= B01111111;
temp2 = lsb << 6;
temp2 |= msb;
temp2 = 0 - temp2;
}else{
temp2 = lsb << 6;
temp2 |= msb;
}
return temp2;
}
int readAccel2(byte lsbAddr, byte msbAddr){
byte lsb, msb = 0;
short result;
while(!(lsb & 1)){
Wire.beginTransmission(address);
Wire.send(lsbAddr);
Wire.requestFrom(address, 1);
while(Wire.available()){
lsb = Wire.receive();
}
Wire.endTransmission();
}
Serial.print("lsb: ");
Serial.println(lsb, BIN);
if(lsb & 1){
Serial.println("new_data");
}
lsb = lsb >> 2;
Serial.print("lsb: ");
Serial.println(lsb, BIN);
Wire.beginTransmission(address);
Wire.send(msbAddr);
Wire.requestFrom(address, 1);
while(Wire.available()){
msb = Wire.receive();
}
Wire.endTransmission();
Serial.print("msb: ");
Serial.println(msb, BIN);
result = msb << 6;
Serial.print("res: ");
Serial.println(result, BIN);
result |= lsb;
Serial.print("res: ");
Serial.println(result, BIN);
Serial.print("res: ");
Serial.println(result, DEC);
return 0;
}
int readAccel(byte msb, byte lsb)
{
int temp, result;
temp = 0;
while(temp != 1)
{
Wire.beginTransmission(address);
Wire.send(msb);
Wire.requestFrom(address, 1);
while(Wire.available())
{
temp = Wire.receive() & 0x01;
}
}
Wire.beginTransmission(address);
Wire.send(lsb);
Wire.requestFrom(address, 1);
while(Wire.available())
{
temp |= Wire.receive();
temp = temp >> 2;
}
result = Wire.endTransmission();
return temp;
}
#define error(s) error_P(PSTR(s))
void error_P(const char* str) {
PgmPrint("error: ");
SerialPrintln_P(str);
if (card.errorCode()) {
PgmPrint("SD error: ");
Serial.print(card.errorCode(), HEX);
Serial.print(',');
Serial.println(card.errorData(), HEX);
}
while(1);
}
void writeNumber(SdFile& f, uint32_t n) {
uint8_t buf[10];
uint8_t i = 0;
do {
i++;
buf[sizeof(buf) - i] = n%10 + '0';
n /= 10;
} while (n);
f.write(&buf[sizeof(buf) - i], i);
file.close();
Serial.println("Done");
}
void setup() {
Wire.begin();
Serial.begin(9600);
initBMA180();
delay(2000);
Serial.println();
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
if (!card.init(SPI_HALF_SPEED)) error("card.init failed");
// initialize a FAT volume
if (!volume.init(&card)) error("volume.init failed");
// open the root directory
if (!root.openRoot(&volume)) error("openRoot failed");
// create a new file
char name[] = "WRITE00.TXT";
for (uint8_t i = 0; i < 100; i++) {
name[5] = i/10 + '0';
name[6] = i%10 + '0';
if (file.open(&root, name, O_CREAT | O_EXCL | O_WRITE)) break;
}
if (!file.isOpen()) error ("file.create");
Serial.print("Writing to: ");
Serial.println(name);
}
void loop()
{
int X,Y,Z=0;
Serial.println(" X: ");
X = readAccel3(0x03, 0x02);
double dX;
dX = X * 0.00025;
Serial.println(dX);
Serial.println(" Y: ");
Y = readAccel3(0x05, 0x04);
double dY;
dY = Y * 0.00025;
Serial.println(dY);
Serial.println(" Z: ");
Z = readAccel3(0x07, 0x06);
double dZ;
dZ = Z * 0.00025;
Serial.println(dZ);
writeNumber(file, dX);
writeNumber(file, dY);
writeNumber(file, dZ);
delay(200);
}
Da notare che nel serial monitor mi appaiono le stringhe contenenti le letture ed anche il done di scrittura solo che poi dentro al file TXt o CSV che sia non cè niente :~