Hi there,
I am working on a project taking an X,Y and Z reading from an adxl335 accelerometer using the onboard ADC and am storing those values to an SD card at 500 Hz. I've had the code working on a 5v pro mini but recently switched to a 3v3 pro mini do to simplicity (not needing to step up the 3.7 volts from the battery to 5v). However, the 3v3 pro mini will not recognize the SD card! It returns errors about the format.
However, if I run the SD card on 5 volts (I did this by accident) it works fine and writes to the SD card fine.
Has anyone run into this before, or have any ideas about where to look?
Below is my code though it will work fine on a 5 volt board or when the 3v3 board is powered with 5 volts:
/*
* This sketch is a simple binary write/read benchmark.
*/
#include <SdFat.h>
#include <SdFatUtil.h>
//String r32 = "zz";
int vx = 0;
int vy = 0;
int vz = 0;
int r1 = 0;
int r2 = 0;
const int flasher = 9;
uint32_t m;
unsigned long LOG_INTERVAL;
unsigned long TWO_LOG;
char lookup32[] =
{
'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V'
};
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// SD chip select pin
const uint8_t chipSelect = SS_PIN;
// text file for logging
ofstream logfile;
#define FILE_SIZE_KB 12
//SET TO 24 before sending out
#define SAMPLES_PER_SECOND 2 // mills between entries
#define FILE_SIZE (1000UL*FILE_SIZE_KB)
#define BUF_SIZE 8
uint8_t buf[BUF_SIZE];
SdFat sd;
SdFile file;
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
// store error strings in flash to save RAM
#define error(s) sd.errorHalt_P(PSTR(s))
//------------------------------------------------------------------------------
void createbuffer()
{
// fill buf with known data
// delay(10);
vx = analogRead(A2); // 230us per line - X chan
// delay(10);
base32(vx);
buf[BUF_SIZE-8] = lookup32[r2];
buf[BUF_SIZE-7] = lookup32[r1];
// delay(10);
vy = analogRead(A1); // Y read
// delay(10);
base32(vy);
buf[BUF_SIZE-6] = lookup32[r2];
buf[BUF_SIZE-5] = lookup32[r1];
// delay(10);
vz = analogRead(A0); // Z read
// delay(10);
base32(vz);
buf[BUF_SIZE-4] = lookup32[r2];
buf[BUF_SIZE-3] = lookup32[r1];
buf[BUF_SIZE-2] = '\r';
buf[BUF_SIZE-1] = '\n';
return;
}
void base32(int number)
{
int n1 = int(number/32);
r1 = number - 32 * n1;
int n2 = int(n1/32);
r2 = n1 - n2;
return;
}
void setup() {
Serial.begin(9600);
pinMode(flasher, OUTPUT);
digitalWrite(flasher, LOW);
if (!sd.init(SPI_FULL_SPEED, chipSelect)) sd.initErrorHalt();
char name[] = "LOG00.TXT";
for (uint8_t i = 0; i < 100; i++) {
name[3] = i/10 + '0';
name[4] = i%10 + '0';
if (sd.exists(name)) continue;
file.open(name, O_CREAT | O_TRUNC | O_RDWR);
break;
}
}
//------------------------------------------------------------------------------
void loop()
{
// do write test
uint32_t n = FILE_SIZE/sizeof(buf);
for (uint32_t i = 0; i < n; i++) {
digitalWrite(flasher, HIGH);
createbuffer();
digitalWrite(flasher, LOW);
delayMicroseconds(955); //Controls 500Hz Sampling Rate
if (file.write(buf, sizeof(buf)) != sizeof(buf)) {
error("write failed");
}
}
file.sync();
}