# Problem with data logging in arduino environment

#### Sunandhan S

Joined Oct 9, 2020
7
HI guys, iam recently making a projects that include arduino, mpu6050 and bmp280 and finally i have to log that data so, iam using a microSD card module

info of the the microSD card:

Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC
Clusters: 1914880
Blocks x Cluster: 8
Total Blocks: 15319040

Volume type is: FAT32
Volume size (Kb): 7659520
Volume size (Mb): 7480
Volume size (Gb): 7.30

Files found on the card (name, date and size in bytes):

first i always try to test every modules one by one to find whether they are working good and i will do that with the help Arduibo IDE example codes and my every modules including microSD card module is working well and for microSD card first used cardinfo and it works perfectly and then i used datalogger code and i also logged the data perfectly and it is actually working very very well. Then i started to write my code first for mpu6050 and bmp280 and there is no mistakes in that code and i also so got good values and then i just included the datalogger code from example in my code and after that also there is no mistakes, actually everything is perfect and i just uploaded that code to my arduino nano and see the serial monitor and it shows :

⸮?>8>?⸮8⸮>>⸮>⸮⸮?8⸮?⸮??⸮?⸮⸮?⸮

and this is the only thing that serial monitor shows !!

iam also posting image of upload pane, the ones that say how much program and RAM memory iam using so it will help u :

and the baud rate in serial monitor is equal to baud rate in Serial.begin() and i choosed the baud rate 9600

and if i delete the data logging code from my main code then it works perfectly but if i include the data logging code in my main code then only it is not working and it shows ⸮?>8>?⸮8⸮>>⸮>⸮⸮?8⸮?⸮??⸮?⸮⸮?⸮ in serial monitor

and main thing is, this problem comes after iam adding a countdown code(basically if a press a button then for loop starts) in my main code after that only the serial shows ⸮?>8>?⸮8⸮>>⸮>⸮⸮?8⸮?⸮??⸮?⸮⸮?⸮ but my countdown code doesn't have any mistakes in it and i also checked it with out adding in my main code and it works well but when i add that in my main code it shows like that and if i delete the data logging code from my main code then everything works well including countdown and if i delete countdown code from my main code then everything works well including data logging and if i add both(data logging and countdown) in my main code then only it serial monitor shows like ⸮?>8>?⸮8⸮>>⸮>⸮⸮?8⸮?⸮??⸮?⸮⸮?⸮ and then it is not working properly

code:
C++:
void setup() {

Serial.begin(9600);

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buzz, OUTPUT);
pinMode(chipSelect, OUTPUT);
pinMode(but, INPUT_PULLUP);

s.attach(6);

imuconfig();

checking();

countdown();

delay(1000);

}

void loop() {

oldtime = newtime;
newtime = millis();
elapsedTime = (newtime - oldtime) / 1000 ;

imu();

Serial.print(" X axis = ");
Serial.print(totalX);
Serial.print(" | ");
Serial.print(" Y axis = ");
Serial.print(totalY);
Serial.print(" | ");

Serial.print(F("Temperature = "));
Serial.print(" *C");
Serial.print(" | ");

Serial.print(F("Pressure = "));
Serial.print(" Pa");
Serial.print(" | ");

Serial.print(F("Approx altitude = "));
Serial.print(" m");
Serial.print(" | ");

Serial.println("");

data();
}

void countdown() {

if (v == 0) {
for (int b = 0; b < 6; b++) {
int buz;
buz = buz + 1000;
tone(buzz, buz);
delay(100);
noTone(buzz);
delay(100);
}

Serial.println("COUNTDOWN STARTS !!");
Serial.println("DURATION IS 60s");

for (int x = 0; x < 60; x++) {

Serial.println(y);

if (v == 0) {

Serial.println("LAUNCH SCRUBBED !!");
tone(buzz, 100);
delay(1000);
noTone(buzz);
while (w) {

if ( v == 0) {

w = 0;

}
}
}
if (y <= 60 && y > 45) {
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
delay(500);
}
else if (y <= 55 && y > 30) {
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(500);
}
else if (y <= 30 && y > 10) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(500);
}
else if (y <= 10 && y > 1 ) {
digitalWrite(buzz, HIGH);
delay(500);
digitalWrite(buzz, LOW);
delay(500);
}
else if (y = 1) {
tone(buzz, 600);
delay(1000);
noTone(buzz);
delay(500);
}
y = y - 1;
}
}
}

void data() {

File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(totalX);
dataFile.close();
}

else {

Serial.println("error opening datalog.txt");

}
}
and this not my full code but this is where the problem is

and i really don't know why this is happening and if anyone know what is really going on pls pls pls pls say and pls help me to solve this problem, pls pls

#### KeithWalker

Joined Jul 10, 2017
2,299
The problem appears only when you include both the data logging and countdown modules. That would indicate that there is some kind of inter-action between the two. My immediate thoughts are:
It could be a variable or memory location that is modified by both software modules.
It could cause a conflict if the two modules use the same timer in the microprogrammer.
Regards,
Keith

#### Sunandhan S

Joined Oct 9, 2020
7
The problem appears only when you include both the data logging and countdown modules. That would indicate that there is some kind of inter-action between the two. My immediate thoughts are:
It could be a variable or memory location that is modified by both software modules.
It could cause a conflict if the two modules use the same timer in the microprogrammer.
Regards,
Keith
ok i will show my full code, i mean top to bottom and now i hope u will figure out the problem

C++:
#include <Wire.h>
#include <SPI.h>
#include <Servo.h>
#include <SD.h>
#include "MPU6050.h"

MPU6050 accelgyro;

int elapsedTime;
int newtime;
int oldtime;

const int MPU = 0x68;
const int chipSelect = 4;

unsigned long t;

int a = 0;
int i = 0;
int v;
int y = 60;
int w = 1;
int but = 2;

float gyroerror = 0;
float gyroX, gyroY;
float gyroangleX, gyroangleY, gyroangleZ;
float gyroerrorX, gyroerrorY, gyroerrorZ;

float accelerror = 0;
float accelX, accelY, accelZ;
float accelangleX, accelangleY, accelangleZ;
float accelerrorX, accelerrorY, accelerrorZ;

int totalX, totalY;

float radianstodeg = 180 / 3.141592654;

int led1 = 3;
int led2 = 5;
int buzz = 9;

Servo s;

void setup() {

Serial.begin(9600);

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buzz, OUTPUT);
pinMode(chipSelect, OUTPUT);
pinMode(but, INPUT_PULLUP);

s.attach(6);

imuconfig();

checking();

countdown();

delay(1000);

}

void loop() {

oldtime = newtime;
newtime = millis();
elapsedTime = (newtime - oldtime) / 1000 ;

imu();

Serial.print(" X axis = ");
Serial.print(totalX);
Serial.print(" | ");
Serial.print(" Y axis = ");
Serial.print(totalY);
Serial.print(" | ");

Serial.print(F("Temperature = "));
Serial.print(" *C");
Serial.print(" | ");

Serial.print(F("Pressure = "));
Serial.print(" Pa");
Serial.print(" | ");

Serial.print(F("Approx altitude = "));
Serial.print(" m");
Serial.print(" | ");

Serial.println("");

data();

parachute();

}

void ledbuzz() {

for (int b = 0; b < 6; b++) {

int buz;
buz = buz + 500;
tone(buzz, buz);
delay(100);
noTone(buzz);
delay(100);

}

for (int i = 0; i < 255; i++) {

analogWrite(led1, i);
analogWrite(led2, i);
delay(10);

}

for (int i = 255; i > 0; i--) {

analogWrite(led1, i);
analogWrite(led2, i);
delay(10);

}

digitalWrite(led1, LOW);
digitalWrite(led2, LOW);

}

void checking() {

Serial.println("Electricity started to pass through me !!!!");

ledbuzz();

delay(1000);

Serial.println("Hi buddy, iam your rocket computer !!");
delay(500);
Serial.println("Let's make a GOO/NO GOO !!");
delay(1000);
Serial.println("IMU !!");
delay(1000);
Serial.println(accelgyro.testConnection() ? "IMU GO !!" : "Bro IMU has some problems !!!");
accelgyro.testConnection() ? digitalWrite(buzz, HIGH) : tone(buzz, 100);
delay(500);
digitalWrite(buzz, LOW);
noTone(buzz);
delay(1000);
Serial.println("DATA LOGGING SYSTEM !!");
delay(1000);
if (SD.begin(chipSelect)) {
Serial.println("DATA LOGGING SYSTEM GOO !! ");
digitalWrite(buzz, HIGH);
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(buzz, LOW);
digitalWrite(led1, LOW);
delay(500);
}

else {
Serial.println("Bro Data logging system has some problems !!!");
tone(buzz, 300);
digitalWrite(led2, HIGH);
delay(500);
noTone(buzz);
digitalWrite(led2, LOW);
}

delay(1000);
Serial.println("TPA SYSTEM !!");
delay(1000);

if (bmp.begin()) {
Serial.println("TPA GOO !!");
digitalWrite(buzz, HIGH);
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(buzz, LOW);
digitalWrite(led1, LOW);
delay(500);
}

else {
Serial.println("Bro TPA system has some problems !!!");
tone(buzz, 300);
digitalWrite(led2, HIGH);
delay(500);
noTone(buzz);
digitalWrite(led2, LOW);
}

delay(1000);

Serial.println("RECOVERY SYSTEM !!");

s.write(0);
delay(500);
s.write(180);
delay(500);
s.write(90);

Serial.println("IF THE SERVO IS MOVING NO PROBLEM.......IF NOT SOMETHING WENT WRONG");
delay(1000);
digitalWrite(buzz, HIGH);
Serial.println("LET'S");
delay(500);
digitalWrite(buzz, LOW);
delay(500);
digitalWrite(buzz, HIGH);
Serial.println("SET");
delay(500);
digitalWrite(buzz, LOW);
delay(500);
tone(buzz, 600);
Serial.println("GOOO !!");
delay(1000);
noTone(buzz);
delay(500);
Serial.println("IF U WANT TO LAUNCH U NEED A COUNTDOWN SO PRESS THE BUTTON OR  IF U JUST TESTING DON'T DO NOTHING ");
delay(3000);
}

void imuconfig() {

Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0x00);
Wire.endTransmission(true);
//Gyro config
Wire.beginTransmission(MPU);
Wire.write(0x1B);
Wire.write(0x10);
Wire.endTransmission(true);
//Acc config
Wire.beginTransmission(MPU);
Wire.write(0x1C);
Wire.write(0x10);
Wire.endTransmission(true);
delay(20);

if (accelerror == 0)
{

for (int a = 0; a < 200; a++)
{

Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(0x68, 6, true);
accelerrorX = accelerrorX + ((atan((accelY) / sqrt(pow((accelX), 2) + pow((accelZ), 2))) * radianstodeg));
accelerrorY = accelerrorY + ((atan((accelX) / sqrt(pow((accelY), 2) + pow((accelZ), 2))) * radianstodeg));

if (a == 199)

{
accelerrorX = accelerrorX / 200;
accelerrorY = accelerrorY / 200;
accelerrorZ = accelerrorZ / 200;
accelerror = 1;

}
}
}

if (gyroerror == 0)

{

for (int i = 0; i < 200; i++)
{
Wire.beginTransmission(MPU);
Wire.write(0x43);
Wire.endTransmission(false);
Wire.requestFrom(0x68, 6, true);
gyroerrorX = gyroerrorX + (gyroX / 32.8);
gyroerrorY = gyroerrorY + (gyroY / 32.8);
gyroerrorZ = gyroerrorZ + (gyroY / 32.8);

if (i == 199)

{
gyroerrorX = gyroerrorX / 200;
gyroerrorY = gyroerrorY / 200;
gyroerrorZ = gyroerrorZ / 200;
gyroerror = 1;
}
}
}
}

void imu() {

Wire.beginTransmission(MPU);
Wire.write(0x43);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true);

gyroX = (gyroX / 32.8) - gyroerrorX;
gyroY = (gyroY / 32.8) - gyroerrorY;

gyroangleX = gyroX * elapsedTime;
gyroangleY = gyroY * elapsedTime;

Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true);

accelangleX = (atan((accelY) / sqrt(pow((accelX), 2) + pow((accelZ), 2))) * radianstodeg) - accelerrorX;
accelangleY = (atan((accelX) / sqrt(pow((accelY), 2) + pow((accelZ), 2))) * radianstodeg) - accelerrorY;

totalX = 0.98 * (totalX + gyroX) + 0.02 * accelangleX ;
totalY = 0.98 * (totalY + gyroY) + 0.02 * accelangleY;
}

void countdown() {

if (v == 0) {
for (int b = 0; b < 6; b++) {
int buz;
buz = buz + 1000;
tone(buzz, buz);
delay(100);
noTone(buzz);
delay(100);
}

Serial.println("COUNTDOWN STARTS !!");
Serial.println("DURATION IS 60s");

for (int x = 0; x < 60; x++) {

Serial.println(y);

if (v == 0) {

Serial.println("LAUNCH SCRUBBED !!");
tone(buzz, 100);
delay(1000);
noTone(buzz);
while (w) {

if ( v == 0) {

w = 0;

}
}
}

if (y <= 60 && y > 45) {

digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
delay(500);
}

else if (y <= 55 && y > 30) {

digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(500);
}

else if (y <= 30 && y > 10) {

digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(500);
}

else if (y <= 10 && y > 1 ) {

digitalWrite(buzz, HIGH);
delay(500);
digitalWrite(buzz, LOW);
delay(500);
}

else if (y = 1) {

tone(buzz, 600);
delay(1000);
noTone(buzz);
delay(500);

}

y = y - 1;

}
}

Serial.println("HYDRO GO FOR LAUNCH");

}

void data() {

File dataFile = SD.open("datalog.txt", FILE_WRITE);

if (dataFile) {

dataFile.println(totalX);
dataFile.close();

}

else {

Serial.println("error opening datalog.txt");

}

}

void parachute() {

t = millis();

if ( t > 96000UL ) {

s.write(180);

}

}

#### Sunandhan S

Joined Oct 9, 2020
7
HI guys, iam recently making a projects that include arduino, mpu6050 and bmp280 and finally i have to log that data so, iam using a microSD card module

info of the the microSD card:

Initializing SD card...Wiring is correct and a card is present.

Card type: SDHC
Clusters: 1914880
Blocks x Cluster: 8
Total Blocks: 15319040

Volume type is: FAT32
Volume size (Kb): 7659520
Volume size (Mb): 7480
Volume size (Gb): 7.30

Files found on the card (name, date and size in bytes):

first i always try to test every modules one by one to find whether they are working good and i will do that with the help Arduibo IDE example codes and my every modules including microSD card module is working well and for microSD card first used cardinfo and it works perfectly and then i used datalogger code and i also logged the data perfectly and it is actually working very very well. Then i started to write my code first for mpu6050 and bmp280 and there is no mistakes in that code and i also so got good values and then i just included the datalogger code from example in my code and after that also there is no mistakes, actually everything is perfect and i just uploaded that code to my arduino nano and see the serial monitor and it shows :

⸮?>8>?⸮8⸮>>⸮>⸮⸮?8⸮?⸮??⸮?⸮⸮?⸮

and this is the only thing that serial monitor shows !!

iam also posting image of upload pane, the ones that say how much program and RAM memory iam using so it will help u :
View attachment 221181

and the baud rate in serial monitor is equal to baud rate in Serial.begin() and i choosed the baud rate 9600

and if i delete the data logging code from my main code then it works perfectly but if i include the data logging code in my main code then only it is not working and it shows ⸮?>8>?⸮8⸮>>⸮>⸮⸮?8⸮?⸮??⸮?⸮⸮?⸮ in serial monitor

and main thing is, this problem comes after iam adding a countdown code(basically if a press a button then for loop starts) in my main code after that only the serial shows ⸮?>8>?⸮8⸮>>⸮>⸮⸮?8⸮?⸮??⸮?⸮⸮?⸮ but my countdown code doesn't have any mistakes in it and i also checked it with out adding in my main code and it works well but when i add that in my main code it shows like that and if i delete the data logging code from my main code then everything works well including countdown and if i delete countdown code from my main code then everything works well including data logging and if i add both(data logging and countdown) in my main code then only it serial monitor shows like ⸮?>8>?⸮8⸮>>⸮>⸮⸮?8⸮?⸮??⸮?⸮⸮?⸮ and then it is not working properly

code:
C++:
void setup() {

Serial.begin(9600);

pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(buzz, OUTPUT);
pinMode(chipSelect, OUTPUT);
pinMode(but, INPUT_PULLUP);

s.attach(6);

imuconfig();

checking();

countdown();

delay(1000);

}

void loop() {

oldtime = newtime;
newtime = millis();
elapsedTime = (newtime - oldtime) / 1000 ;

imu();

Serial.print(" X axis = ");
Serial.print(totalX);
Serial.print(" | ");
Serial.print(" Y axis = ");
Serial.print(totalY);
Serial.print(" | ");

Serial.print(F("Temperature = "));
Serial.print(" *C");
Serial.print(" | ");

Serial.print(F("Pressure = "));
Serial.print(" Pa");
Serial.print(" | ");

Serial.print(F("Approx altitude = "));
Serial.print(" m");
Serial.print(" | ");

Serial.println("");

data();
}

void countdown() {

if (v == 0) {
for (int b = 0; b < 6; b++) {
int buz;
buz = buz + 1000;
tone(buzz, buz);
delay(100);
noTone(buzz);
delay(100);
}

Serial.println("COUNTDOWN STARTS !!");
Serial.println("DURATION IS 60s");

for (int x = 0; x < 60; x++) {

Serial.println(y);

if (v == 0) {

Serial.println("LAUNCH SCRUBBED !!");
tone(buzz, 100);
delay(1000);
noTone(buzz);
while (w) {

if ( v == 0) {

w = 0;

}
}
}
if (y <= 60 && y > 45) {
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
delay(500);
}
else if (y <= 55 && y > 30) {
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(500);
}
else if (y <= 30 && y > 10) {
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(500);
}
else if (y <= 10 && y > 1 ) {
digitalWrite(buzz, HIGH);
delay(500);
digitalWrite(buzz, LOW);
delay(500);
}
else if (y = 1) {
tone(buzz, 600);
delay(1000);
noTone(buzz);
delay(500);
}
y = y - 1;
}
}
}

void data() {

File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(totalX);
dataFile.close();
}

else {

Serial.println("error opening datalog.txt");

}
}
and this not my full code but this is where the problem is

and i really don't know why this is happening and if anyone know what is really going on pls pls pls pls say and pls help me to solve this problem, pls pls

#### KeithWalker

Joined Jul 10, 2017
2,299
ok i will show my full code, i mean top to bottom and now i hope u will figure out the problem
I don't have a day or so to spare, going through your code visually and I can't test it by running it, so it's up to you to do the trouble shooting.
You wrote the code so you understand what is supposed to interact and what is not. There are a number of things you could do:
Copy the listing to a word processor and search for all occurrences of the variables used in the two problem modules to see if a variable is common to them both.
Introduce a long pause or a stop into different places, one at a time, in the two modules and list the variable values on the monitor when the program pauses. That will let you know what is happening when and will help narrow down the location of the code that is causing the problem.
If that doesn't solve it, I will try to suggest other tests you can do.

#### Sunandhan S

Joined Oct 9, 2020
7
thanks for saying

#### KeithWalker

Joined Jul 10, 2017
2,299
thanks for saying
I glanced through your code but it has no documentation so it is very difficult to follow. I noticed that you are using a number of libraries for the different functions. You should check the libraries used by your data logging and countdown modules to make sure that they are compatible with each other. Let me know which libraries are used by which of the two modules.. They may be setting shared timers, writing into the same memory area or setting shared registers that cause them to interfere with each other.

#### Sunandhan S

Joined Oct 9, 2020
7
I glanced through your code but it has no documentation so it is very difficult to follow. I noticed that you are using a number of libraries for the different functions. You should check the libraries used by your data logging and countdown modules to make sure that they are compatible with each other. Let me know which libraries are used by which of the two modules.. They may be setting shared timers, writing into the same memory area or setting shared registers that cause them to interfere with each other.
THANKS U NOW MY PROBLEM IS SOLVED !!

Basically my sketch prints a lot of stuff on the Serial Monitor, and it can easily fill the SRAM of arduino and that's it behaves little strange but then i used F()(so it stored in flash memory of arduino) and changed some data types into byte data type and now it works perfectly and it is all done with your help and once again thank you very much !! you guys are awesome

PROBLEM SOLVED !