Hallo,
bin Anfänger und dann gleich ein solches Problem. In einem größeren Sketch (deshalb Mega) will ich auf die SD zugreifen. Dies gelingt nicht.
Steckt das Ethernet Shield auf einem Uno, dann funktioniert es, auf dem Mega nicht. Das gleiche sketch, nur die Board-Einstellung in der IDE angepasst. Welchen dummen Fehler mache ich?
Gruß Gerd
ich kann keinen offensichtlichen Fehler feststellen.
Probiere mal "meinen" Code. Nackter Mega 2560 mit WiFi Shield und sd Karte drin.
Die LEDs und Taster mußte nicht beschalten.
/*
Arduino Mega 2560
Arduino WiFi Shield
SD card test
This example shows how use the utility libraries on which the'
SD library is based in order to get info about your SD card.
Very useful for testing a card when you're not sure whether its working or not.
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11 on Arduino Uno/Duemilanove/Diecimila
** MISO - pin 12 on Arduino Uno/Duemilanove/Diecimila
** CLK - pin 13 on Arduino Uno/Duemilanove/Diecimila
** CS - depends on your SD card shield or module.
Pin 4 used here for consistency with other Arduino examples
created 28 Mar 2011
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
*/
// include the SD library:
#include <SD.h>
int LED = 5; // an Pin 5
int analogPin = 0; // Poti am analog pin A0
int val = 0; // Potiwert speichern in val
long Zeitstempel = 0; // aktueller millis() Wert
int Taster = 44; // Taster an Pin 44
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
File myFile;
String valString = "";
String ZeitstempelString = "";
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
const int chipSelect = 4;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
/*
pinMode(LED, OUTPUT); // LED Pins auf Ausgang setzen
digitalWrite(LED, HIGH); // kurzer LED Funktionstest
delay(500);
digitalWrite(LED, LOW); // Ende des LED Funktionstest
// digitalen Eingang mit PullUp aktivieren
pinMode(Taster, INPUT); // setzt pin als Eingang
digitalWrite(Taster, HIGH); // aktiviert den PullUp Widerstand
*/
Serial.print("\nInitializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(53, OUTPUT); // change this to 53 on a mega, normal 10
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card is inserted?");
Serial.println("* Is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.print("\nCard type: ");
switch(card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.println("\nFiles found on the card (name, date and size in bytes): ");
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
val = analogRead(analogPin);
Zeitstempel = millis();
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
// pinMode(53, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
SD.remove("test.txt"); // Datei löschen
Serial.println("test.txt deleted!");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE); // Datei wird erzeugt wenn nicht vorhanden
myFile.close();
// re-open the file for reading:
Serial.println("setup open");
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("new test.txt done");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop(void) { }
digitalWrite(10, HIGH);
Das hat's gebracht. Danke
Ich meine es auch schon mehrmals versucht zu haben. Aber vielleicht nicht so.
OK. Dann bin ich erstmal soweit.
Jetzt muss ich den PC verlassen. Melde mich Morgen vermutlich wieder. Der nächste Schritt ist Zwischen Ethernet und Sd zu wechseln. Vom Ethernet hole ich die Zeit. Das funktioniert sehr gut. Mal sehn wie der Wechsel jetzt funktioniert.
erstmal vielen, vielen Dank
Gruß Gerd
So, nun bin ich wieder da.
Ich schaffe einfach nicht, mit Mega2560 und Ethernet Shield den Wechsel zwischen Ethernet und SD und wieder zurück. Mein Probiersketch ist angehangen.
Bin für jeden hilfreichen Tipp dankbar.
Gruß Gerd
Beim wechsel der von SD auf Ethernet musst du unbedingt die ChipSelect Leitungungen umschalten und evtl ein bisschen warten bevor du mit Daten feuerst.
Gruß
Der Dani
Du solltest SD.begin() nicht mehr als einmal aufrufen, schon gar nicht mehrmals innerhalb von loop(). Und setze die beiden CS-Leitungen im setup() HIGH, bevor Du die jeweiligen begin()-Methoden aufrufst. Nach den begin()-Aufrufen solltest Du sie nicht mehr anrühren, denn dann sind die Bibliotheken dafür verantwortlich (sie sollten das auch korrekt machen).
Du solltest SD.begin() nicht mehr als einmal aufrufen, schon gar nicht mehrmals innerhalb von loop(). Und setze die beiden CS-Leitungen im setup() HIGH, bevor Du die jeweiligen begin()-Methoden aufrufst. Nach den begin()-Aufrufen solltest Du sie nicht mehr anrühren, denn dann sind die Bibliotheken dafür verantwortlich (sie sollten das auch korrekt machen).
Habe mein Sketch so geändert. Bin sehr skeptisch. Aber bisher funktioniert's. Toll! Danke!
Gruß Gerd
Hallo,
ich glaub, die Änderung selbst vorzunehmen überfordert mich. Das traue ich mir nicht zu. Gibt es evtl. eine fertig-geänderte SD-Library?
Gruß Gerd