Hi everyone, I want to collect microphone data in analog and adxl345 data. So I wrote a code that sd card is working on core 1 and microphone, adxl345 working on core2. Adxl345 collecting 1 sample data at 20ms and Microphone is collecting 128 samples at 63ms, when I try to use mic and accelerometer together, microphone is collecting 128 samples at 70 or more ms. So I am losing some datas.
How can I prevent this ?
#include <Wire.h> // Wire library - used for I2C communication
#define ADXL345_DATAX0 0x32
#define ADXL345_DEVICE (0x53)
#define ADXL345_TO_READ (6)
#define ADXL345_ERROR 0
#define ADXL345_READ_ERROR 1
#if !defined(ARDUINO_ARCH_RP2040)
#error For RP2040 only
#endif
#if defined(ARDUINO_ARCH_MBED)
#define PIN_SD_MOSI PIN_SPI_MOSI
#define PIN_SD_MISO PIN_SPI_MISO
#define PIN_SD_SCK PIN_SPI_SCK
#define PIN_SD_SS PIN_SPI_SS
#else
#define PIN_SD_MOSI PIN_SPI0_MOSI
#define PIN_SD_MISO PIN_SPI0_MISO
#define PIN_SD_SCK PIN_SPI0_SCK
#define PIN_SD_SS PIN_SPI0_SS
#endif
#define _RP2040_SD_LOGLEVEL_ 4
#include <SPI.h>
#include <RP2040_SD.h>
#define fileName "demo14.txt"
#define fileNameSec "demo15.txt"
#define analog_pin 26
const int samples = 128;
boolean calcTime = true;
boolean micBool1 = false;
boolean micBool2 = false;
String httpRequestData = "";
String allData = "";
String allData2 = "";
String sdCardData = "";
String secSdCardData = "";
int ADXL345 = 0x53; // The ADXL345 sensor I2C address
byte _buff[6] ; // 6 Bytes Buffer
bool status;
byte error_code;
int sayac = 0;
boolean datasep = true;
unsigned long oldMillis = 0;
unsigned long oldMillisSec = 0;
unsigned long oldMillisSecSec = 0;
unsigned long prevAccMillis = 0;
unsigned long prevReadAcc = 0;
unsigned long prevReadFromI2C = 0;
unsigned long prevReadFrom = 0;
unsigned long prevMicrosReadAccel = 0;
unsigned long prevSample = 0;
unsigned long previousMillis = 0;
String data = "";
String fullData = "";
int sampleCount = 0;
String micData1 = "";
String micData2 = "";
int counterForMic = 0;
void readFromI2C(byte address, int num, byte _buff[]) {
Wire.beginTransmission(ADXL345_DEVICE);
Wire.write(address);
Wire.endTransmission();
// Wire.beginTransmission(ADXL345_DEVICE);
// Wire.reqeustFrom contains the beginTransmission and endTransmission in it.
Wire.requestFrom(ADXL345_DEVICE, num); // Request 6 Bytes
int i = 0;
while (Wire.available())
{
_buff[i] = Wire.read(); // Receive Byte
i++;
}
if (i != num) {
status = ADXL345_ERROR;
error_code = ADXL345_READ_ERROR;
}
// Wire.endTransmission();
}
void readFrom(byte address, int num, byte _buff[]) {
readFromI2C(address, num, _buff); // If I2C Communication
}
void readAccel(int *x, int *y, int *z) {
readFrom(ADXL345_DATAX0, ADXL345_TO_READ, _buff); // Read Accel Data from ADXL345
// Each Axis @ All g Ranges: 10 Bit Resolution (2 Bytes)
*x = (int16_t)((((int)_buff[1]) << 8) | _buff[0]);
*y = (int16_t)((((int)_buff[3]) << 8) | _buff[2]);
*z = (int16_t)((((int)_buff[5]) << 8) | _buff[4]);
}
int x, y, z;
float X_out, Y_out, Z_out;
unsigned long microSeconds;
unsigned int samplingPeriod;
unsigned long curMils = 0;
void setup() {
Serial.begin(115200);
while (!Serial);
delay(2000);
Wire.setSDA(0);
Wire.setSCL(1);
Wire.begin(); // Initiate the Wire library
// Set ADXL345 in measuring mode
Wire.beginTransmission(ADXL345_DEVICE); // Start communicating with the device
Wire.write(0x31);
Wire.write(0x0B);
Wire.endTransmission();
Wire.beginTransmission(ADXL345_DEVICE);
Wire.write(0x20); // Z-axis offset register
Wire.write(-7);
Wire.endTransmission();
Wire.beginTransmission(ADXL345_DEVICE);
Wire.write(0x2D); // Access/ talk to POWER_CTL Register - 0x2D
// Enable measurement
Wire.write(0x08); // (8dec -> 0000 1000 binary) Bit D3 High for measuring enable
Wire.endTransmission();
Wire.beginTransmission(ADXL345_DEVICE);
Wire.write(0x2C);
// Enable measurement
Wire.write(0x09); //For low power 000x x pin set to 1 /1001 determine Hz
Wire.endTransmission();
delay(10);
samplingPeriod = round(1000000 * (1.0 / 2048)); //Period in microseconds
}
void setup1()
{
Serial.begin(115200); // Initiate serial communication for printing the results on the Serial monitor
while (!Serial);
delay(1000);
#if defined(ARDUINO_ARCH_MBED)
Serial.print("Starting SD Card ReadWrite on MBED ");
#else
Serial.print("Starting SD Card ReadWrite on ");
#endif
Serial.println(BOARD_NAME);
Serial.println(RP2040_SD_VERSION);
Serial.print("Initializing SD card with SS = ");
Serial.println(PIN_SD_SS);
Serial.print("SCK = ");
Serial.println(PIN_SD_SCK);
Serial.print("MOSI = ");
Serial.println(PIN_SD_MOSI);
Serial.print("MISO = ");
Serial.println(PIN_SD_MISO);
if (!SD.begin(PIN_SD_SS))
{
Serial.println("Initialization failed!");
return;
}
Serial.println("Initialization done.");
}
void loop() {
unsigned long currentMicMillis = micros();
if (currentMicMillis - previousMillis > 10)
{
previousMillis = currentMicMillis;
if (calcTime)
{
calcTime = false;
curMils = millis();
}
unsigned long millisSample = micros();
if (millisSample - prevSample >= 10)
{
prevSample = millisSample ;
if (sampleCount < samples)
{
microSeconds = micros(); //Returns the number of microseconds since the Arduino board began running the current script.
data = analogRead(analog_pin);
fullData = fullData + data + ",";
sampleCount = sampleCount + 1;
while (micros() < (microSeconds + samplingPeriod))
{
//do nothing
}
}
}
fullData = fullData + "}" + "\r\n";
if (sampleCount == 127 && counterForMic <= 50 )
{
Serial.println(millis() - curMils);
calcTime = true;
counterForMic = counterForMic + 1;
sampleCount = 0;
micData1 = micData1 + fullData;
fullData = "{micData =";
if (counterForMic >= 101)
{
micData1 = micData1 + " {millis1= " + millis() + "}";
micBool1 = true;
}
}
if (sampleCount >= 128 && counterForMic > 100 && counterForMic <= 200)
{
counterForMic = counterForMic + 1;
sampleCount = 0;
micData2 = micData2 + fullData ;
fullData = "{micData =";
if (counterForMic >= 201)
{
micData2 = micData2 + " {millis2= " + millis() + "}";
micBool2 = true;
counterForMic = 0;
}
}
}
unsigned long accMillis = millis();
if (accMillis - prevAccMillis > 18)
{
prevAccMillis = accMillis;
Wire.beginTransmission(ADXL345);
Wire.write(0x30); // Access the INT_SOURCE register (0x30)
Wire.endTransmission(false);
Wire.requestFrom(ADXL345, 1, true); // Read 1 byte
uint8_t intSource = Wire.read();
if (intSource >= 130)
{
unsigned long microsReadAccel = micros();
if (microsReadAccel - prevMicrosReadAccel >= 20)
{
readAccel(&x, &y, &z);
}
X_out = x / 256.f;
Y_out = y / 256.f;
Z_out = z / 256.f;
httpRequestData = "{\"time\":" + String( millis()) + "," + "\"x\":" + String(X_out) + "," + "\"y\":" + String(Y_out) + " ," + "\"z\":" + String(Z_out) + "}" + ",";
if (datasep)
{
allData = allData + httpRequestData;
}
else
{
allData2 = allData2 + httpRequestData;
}
sayac++;
}
}
}
void loop1() {
unsigned long currentMillis = millis();
if (currentMillis - oldMillis >= 100 && sayac >= 50)
{
oldMillis = currentMillis;
datasep = !datasep;
sayac = 0;
File myFile = SD.open(fileName, FILE_WRITE);
if (myFile)
{
if (datasep)
{
sdCardData = allData2;
}
else
{
sdCardData = allData;
}
int lastIndex = sdCardData.length() - 1;
sdCardData.remove(lastIndex);
myFile.print(sdCardData);
myFile.println("");
// close the file:
myFile.close();
}
else
{
Serial.print("Error opening ");
Serial.println("----------------------------------------------------");
Serial.println(fileName);
}
if (datasep)
{
allData2 = "";
}
else
{
allData = "";
}
}
if (micBool1)
{
micBool1 = false;
File secMyFile = SD.open(fileNameSec, FILE_WRITE);
if (secMyFile)
{
secSdCardData = micData1;
micData1 = "";
secMyFile.print(secSdCardData);
secMyFile.println("");
secMyFile.close();
}
else
{
Serial.print("Error opening ");
Serial.println("----------------------------------------------------");
Serial.println(fileNameSec);
}
}
if (micBool2)
{
micBool2 = false;
File secMyFile = SD.open(fileNameSec, FILE_WRITE);
if (secMyFile)
{
secSdCardData = micData2;
micData2 = "";
secMyFile.print(secSdCardData);
secMyFile.println("");
secMyFile.close();
}
else
{
Serial.print("Error opening ");
Serial.println("----------------------------------------------------");
Serial.println(fileNameSec);
}
}
}