i am currently working on a school project. i need to control a torch and read out the progression of the voltage using three different measurements. i'm logging the data from the measurement on a adafruit.com 5V ready micro-sd breakout board.
before i'll explain to much i'm just gonna post the code which i have so far:
#include <SD.h>
const int chipSelect = 4;
const int akkuA = 9;
const int akkuB = 8;
String dataString;
//----Internal Readconfig----
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
//---------------------------
// setup
//---------------------------
void setup() {
Serial.begin(9600);
analogReference(INTERNAL);
analogRead(0);
// pinMode(13,OUTPUT); //evtl löschen???
//---SD-----
Serial.print("Initializing SD card...");
pinMode(10,OUTPUT);
if(!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
return;
}
Serial.println("card initialized");
}
//---------------------------
// loop
//---------------------------
void loop() {
//----Messungen----
readVcc();
int sensA = analogRead(A1);
double voltA = sensA * (5.0 / 1023.0);
Serial.println(voltA,3);
readVcc();
int sensB = analogRead(A2);
float voltB = sensB * (5.0 / 1023.0);
Serial.println(voltB,3);
readVcc();
int sensC = analogRead(A3);
float voltC = sensC * (5.0 / 1023.0);
Serial.println(voltC,3);
//----Spannung über Widerstand----
Serial.print("Diff:");
float diff = voltB-voltA;
Serial.println(diff,4);
//----Umwandlung + Zusammenfügen----
char spanA[5];
dtostrf(diff,1,3,spanA);
char spanB[5];
dtostrf(voltC,1,3,spanB);
//----dataString zurücksetzen + Werte speichern----
dataString=""; //nötig?
dataString = String(spanA) + "," + String(spanB);
//----Speicherung-----
File dataFile = SD.open("datalog.txt", FILE_WRITE);
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else {
Serial.println("error opening datalog.txt");
}
//----Laden1?---- // 3.3V=675.18 / 3.7V=757.02
if (sensA > 680 || sensA < 755) {
digitalWrite(akkuA,HIGH);
}
else {
digitalWrite(akkuA,LOW);
}
//----Laden2?---- // 3.3V=675.18 / 3.7V=757.02
if (sensA > 678 || sensA < 755) {
digitalWrite(akkuB,HIGH);
}
else {
digitalWrite(akkuB,LOW);
}
//----Delay zwischen Messungen
delay(1000);
}
this is what it shows when i open the serial monitor:
Initializing SD card...card initialized
1.036
1.095
1.036
Diff:0.0587
0.0591.036,1.036Î
1.056
1.065
0.958
Diff:0.0098
0.0100.958,0.958ý
1.046
1.065
0.948
Diff:0.0196
error opening datalog.txt
after three iterations it resets and starts over in the same pattern. i dont understand why it finds the sd-card but cannot open the datalog.txt. i've already formatted my sd using the sdformatter and tried different unicodes (UTF-8 and UTF16) but neither works. after running the program and plug my sd back to my mac it says that my mac isn't able to open datalog.txt because it's not unicode or it shows my unreadable signs
i've also tried the chipSelect variable on 10 (on the code it's 4) but it doesnt work either. the only thing that currently works is the datalogger from the library. note that i read that the string dataString ="";
should not be in the loop since it's not necessary that it creates a new variable each iteration so i moved it up to the top of the code.
does anyone see an error?