Thanks for that fast respond Jack Christensen!
i still can't fix my problem and still have no clue why it won't work properly. probably you can help me again?
i'm getting three value from analog.read and store them on my adafruit sd breakout board. i've included a reset function which should reset the entire board if the sd is not inserted when i start the program (which works fine except that it won't print my serial.println anymore when i connect my resetPin with the RST) but once the program has started it doesnt stop even when i remove the sd. with the card detect option i wanted to try another version of checking if there is an sd but it should actually already post "error opening datalog.txt" without the cd!? and also since i've implemented that reset-function ( digitalWrite(resetPin, HIGH); ) serial.println("test"); doesnt show anymore on my monitor....
here's my code:
#include <SD.h>
const int chipSelect = 10;
const int akkuA = 9;
const int akkuB = 8;
const int resetPin = 7;
const int cdPin = 6;
String dataString;
int loopA;
int loopB;
int loopC;
// LED's
int redPin = 2;
int grnPin = 3;
int bluPin = 4;
//---------------------------
// setup
//---------------------------
void setup() {
digitalWrite(resetPin, HIGH);
pinMode(resetPin,OUTPUT);
Serial.begin(9600);
Serial.println("reset");
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");
delay(1000);
digitalWrite(resetPin, LOW);
}
Serial.println("card initialized");
pinMode(akkuA,OUTPUT);
pinMode(akkuB,OUTPUT);
pinMode(redPin,OUTPUT);
pinMode(grnPin,OUTPUT);
pinMode(bluPin,OUTPUT);
pinMode(cdPin,INPUT);
}
//---------------------------
// loop
//---------------------------
void loop() {
//----Messungen----
messung();
float voltA = loopA * (5.0 / 20460.0);
float diff = (loopB-loopC) * (5.0 / 20460.0);
Serial.println("TEST");
Serial.println(digitalRead(cdPin));
//----Umwandlung + Zusammenfügen----
char spanA[10];
dtostrf(voltA,1,3,spanA);
char spanB[10];
dtostrf(diff,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");
// Serial.println("resetting");
// delay(10);
digitalWrite(resetPin, LOW);
}
Serial.println("TEST");
//----Laden?---- // 3.3V=675.18 / 3.7V=757.02
if (voltA < 3.3) {
digitalWrite(akkuA,HIGH);
}
else {
digitalWrite(akkuA,LOW);
}
if (voltA > 4.1) {
digitalWrite(akkuB,HIGH);
}
else {
digitalWrite(akkuB,LOW);
}
//_____RGB______ Grün>3.7 / Gelb 3.5-3.7 / Rot<3.5 / voltA< 3.3 --> rot blinken / mehr als 4.1 - blau dauerhaft blau
if (voltA > 3.3) {
analogWrite(redPin,130);
analogWrite(grnPin,0);
analogWrite(bluPin,0);
}
if (voltA > 3.5) {
analogWrite(redPin,130);
analogWrite(grnPin,30);
analogWrite(bluPin,0);
}
if (voltA > 3.7) {
analogWrite(redPin,0);
analogWrite(grnPin,130);
analogWrite(bluPin,0);
}
if (voltA > 4.1) {
analogWrite(redPin,0);
analogWrite(grnPin,0);
analogWrite(bluPin,130);
}
//----Delay zwischen Messungen
Serial.println();
delay(1000);
}
//----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;
}
void messung() {
loopA=0;
loopB=0;
loopC=0;
for (int looper = 0; looper < 20; looper++) {
readVcc();
int sensA = analogRead(A0);
loopA = loopA+ sensA;
readVcc();
int sensB = analogRead(A1);
loopB = loopB + sensB;
readVcc();
int sensC = analogRead(A2);
loopC = loopC + sensC;
}
}
can you find any errors?