Good evening. I have created the following circuit:
A magnetometer-gradimeter with 2 magnetic field sensors fgm 3 pro, one is placed horizontally for measurements on two axes and the other vertically for measurements on the vertical axis. When we click on the scan gradiometer menu with the select button, it appears on the screen ready for scanning. By pressing the measurement button, the sensors take magnetic field values on 3 axes and a total. This is done with each press of the button. When we press the select button, the detection stops and we are given the option to save the file with a manual name. In the serial monitor it shows the following messages:
1.Opening file: AAAA.CSV
2.File opened successfully
3.Saved as: AAAA.CSV
When I pass the SD card to the computer the logs folder is empty without any csv file.
Below I am uploading the Arduino code that I have used:
<CODE>##include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <SPI.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define SD_CS_PIN 10
File dataFile;
#define SENSOR_XY 8
#define SENSOR_Z 9
#define BASE_FREQUENCY 10000.0
#define SENSITIVITY 2500.0
#define BUTTON_LEFT 2
#define BUTTON_RIGHT 3
#define BUTTON_SELECT 4
#define BUTTON_MEASURE 5
#define BUZZER_PIN 6
#define LED_IRON 7
#define LED_CAVITY 11
#define LED_GOLD 12
int menuIndex = 0;
String menuItems[] = {"Scan With Gradiometer", "Magnetometer", "Last Scans"};
bool scanning = false;
float lastX, lastY, lastZ;
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(SD_CS_PIN, OUTPUT);
pinMode(BUTTON_LEFT, INPUT_PULLUP);
pinMode(BUTTON_RIGHT, INPUT_PULLUP);
pinMode(BUTTON_SELECT, INPUT_PULLUP);
pinMode(BUTTON_MEASURE, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_IRON, OUTPUT);
pinMode(LED_CAVITY, OUTPUT);
pinMode(LED_GOLD, OUTPUT);
if (!SD.begin(SD_CS_PIN)) {
Serial.println("SD Card Error!");
lcd.clear();
lcd.print("SD Card Error!");
delay(3000);
return;
}
lcd.clear();
lcd.setCursor(4, 0);
lcd.print("MFD PRO");
delay(4000);
showMenu();
}
void loop() {
if (buttonPressed(BUTTON_LEFT)) {
menuIndex = (menuIndex - 1 + 3) % 3;
showMenu();
}
if (buttonPressed(BUTTON_RIGHT)) {
menuIndex = (menuIndex + 1) % 3;
showMenu();
}
if (buttonPressed(BUTTON_SELECT)) {
selectMenu();
}
}
void showMenu() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(menuItems[menuIndex]);
}
void selectMenu() {
waitForRelease(BUTTON_SELECT);
switch (menuIndex) {
case 0: scanWithGradiometer(); break;
case 1: magnetometerMode(); break;
case 2: showLastScans(); break;
}
}
void scanWithGradiometer() {
scanning = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Ready to Scan");
waitForRelease(BUTTON_SELECT);
while (scanning) {
if (buttonPressed(BUTTON_MEASURE)) {
float x = readMagneticField(SENSOR_XY) * cos(radians(45));
float y = readMagneticField(SENSOR_XY) * sin(radians(45));
float z = readMagneticField(SENSOR_Z);
Serial.print("X: "); Serial.print(x / 100.0, 4);
Serial.print(" G\tY: "); Serial.print(y / 100.0, 4);
Serial.print(" G\tZ: "); Serial.println(z / 100.0, 4);
lastX = x;
lastY = y;
lastZ = z;
}
if (buttonPressed(BUTTON_SELECT)) {
scanning = false;
lcd.clear();
lcd.print("Scan Complete");
delay(2000);
showSavePrompt();
}
}
}
void showSavePrompt() {
lcd.clear();
lcd.print("Save? YES/NO");
bool save = false;
while (true) {
if (buttonPressed(BUTTON_LEFT) || buttonPressed(BUTTON_RIGHT)) {
save = !save;
lcd.setCursor(6, 1);
lcd.print(save ? "YES " : "NO ");
}
if (buttonPressed(BUTTON_SELECT)) {
if (save) {
lcd.clear();
lcd.print("Enter Name:");
delay(500);
String fileName = enterFileName();
saveMeasurement(lastX, lastY, lastZ, fileName);
}
showMenu();
return;
}
delay(150);
}
}
String enterFileName() {
char filename[6] = {'A', 'A', 'A', 'A', 'A', '\0'};
int pos = 0;
lcd.clear();
lcd.print("Name: ");
lcd.setCursor(6, 0);
lcd.print(filename);
while (true) {
lcd.setCursor(6 + pos, 0);
lcd.blink();
if (buttonPressed(BUTTON_RIGHT)) {
filename[pos]++;
if (filename[pos] > 'Z') filename[pos] = 'A';
lcd.setCursor(6 + pos, 0);
lcd.print(filename[pos]);
}
if (buttonPressed(BUTTON_LEFT)) {
filename[pos]--;
if (filename[pos] < 'A') filename[pos] = 'Z';
lcd.setCursor(6 + pos, 0);
lcd.print(filename[pos]);
}
if (buttonPressed(BUTTON_SELECT)) {
pos++;
if (pos > 4) {
lcd.noBlink();
break;
}
}
delay(200);
}
return String(filename) + ".csv";
}
void saveMeasurement(float x, float y, float z, String fileName) {
Serial.print("Opening file: ");
Serial.println(fileName);
if (!SD.exists("logs")) {
SD.mkdir("logs");
}
String filePath = "logs/" + fileName;
dataFile = SD.open(filePath.c_str(), FILE_WRITE);
if (dataFile) {
Serial.println("File opened successfully");
// Αποθήκευση σε CSV μορφή
dataFile.print(x / 100.0, 4); // X
dataFile.print(","); // Separator
dataFile.print(y / 100.0, 4); // Y
dataFile.print(","); // Separator
dataFile.print(z / 100.0, 4); // Z
float total = sqrt(x * x + y * y + z * z);
dataFile.print(","); // Separator
dataFile.println(total / 100.0, 4); // Total
dataFile.close();
Serial.println("Saved as: " + fileName);
} else {
Serial.println("Error opening file!");
lcd.clear();
lcd.print("Error opening file!");
delay(2000);
}
}
void magnetometerMode() {
lcd.clear();
lcd.print("Detect Magnetic");
delay(1000);
waitForRelease(BUTTON_SELECT);
while (digitalRead(BUTTON_SELECT) == HIGH) {
float field = readMagneticField(SENSOR_XY) + readMagneticField(SENSOR_Z);
digitalWrite(LED_IRON, LOW);
digitalWrite(LED_CAVITY, LOW);
digitalWrite(LED_GOLD, LOW);
if (field > 15) {
digitalWrite(LED_IRON, HIGH);
tone(BUZZER_PIN, 1500);
lcd.setCursor(0, 1);
lcd.print("IRON DETECTED ");
} else if (field < -10) {
digitalWrite(LED_CAVITY, HIGH);
tone(BUZZER_PIN, 600);
lcd.setCursor(0, 1);
lcd.print("CAVITY DETECTED ");
} else if (field >= 5 && field <= 15) {
digitalWrite(LED_GOLD, HIGH);
tone(BUZZER_PIN, 1000);
lcd.setCursor(0, 1);
lcd.print("GOLD TARGET ");
} else {
noTone(BUZZER_PIN);
lcd.setCursor(0, 1);
lcd.print("No Target Found ");
}
delay(100);
}
noTone(BUZZER_PIN);
digitalWrite(LED_IRON, LOW);
digitalWrite(LED_CAVITY, LOW);
digitalWrite(LED_GOLD, LOW);
lcd.clear();
showMenu();
}
void showLastScans() {
File dir = SD.open("/");
lcd.clear();
lcd.print("Last Scans:");
int count = 0;
while (File file = dir.openNextFile()) {
lcd.setCursor(0, 1);
lcd.print(file.name());
delay(1000);
count++;
if (count >= 20) break;
}
dir.close();
delay(3000);
showMenu();
}
float readMagneticField(int pin) {
unsigned long highTime = pulseIn(pin, HIGH);
unsigned long lowTime = pulseIn(pin, LOW);
if (highTime > 0 && lowTime > 0) {
float period = highTime + lowTime;
float frequency = 1000000.0 / period;
return (frequency - BASE_FREQUENCY) / SENSITIVITY;
}
return 0.0;
}
bool buttonPressed(int pin) {
static unsigned long lastPress[13] = {0};
if (digitalRead(pin) == LOW && millis() - lastPress[pin] > 250) {
lastPress[pin] = millis();
return true;
}
return false;
}
void waitForRelease(int pin) {
while (digitalRead(pin) == LOW) {
delay(10);
}
} <CODE/>
HELP ME!
