I purchased a P254 MicroSD card from Adafruit and got it working with my UNO R3.
I ran the Card Info example and it worked.
However, when I tried the Read Write example the “testing 1,2,3.” text was written twice. I put the SD card in my laptop just to be sure.
I though it may have something to do with the Serial Monitor Window being opened or closed during upload, maybe the program ran twice but only “testing 1,2,3,3.” was written twice. The other status messages were displayed once.
Line 46 to 56 of the Read Write example (from the Documentation –>Programing section) is shown here:
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
I do have a second UNO R3 and P254 to try but though I would post here first.
In setup(), create a new file with SD.open() named "test.txt". FILE_WRITE enables read and write access to the file, starting at the end. If a file "test.txt" was already on the card, that file would be opened.
mode (optional): the mode in which to open the file. Mode can be FILE_READ (open the file for reading, starting at the beginning of the file) or FILE_WRITE (open the file for reading and writing, starting at the end of the file).
Nowhere does it say that opening a file with FILE_WRITE truncates an existing file.
Which you could have discovered youself by looking in the SD.h header file.
Digging into libraries is not quite the way that beginners expect to work with Arduino
Some eventually will learn to do so to try to understand what is happening.
I’m definitely a novice on this stuff, from what I could research the define statement is saying to allow reading and writing to the file, create it if it doesn’t exist and append to the existing file instead of over writing.
I’m lost on “No O_TRUNC. O_APPEND instead.” Are you saying to replace APPEND with TRUNC?
Yes, I agree snippets are bad but I could not find the code example in the Arduino IDE or my hard drive. I will use GitHub next time.
Yes I think this is the problem but I don’t fully understand what’s going on.
As a test I ejected the SD card from my Arduino setup and inserted it into my Windows 10 laptop to delete the file. So now the SD card in empty.
Then I inserted it back into my setup. I clicked on the Serial Monitor icon in the IDE (top right). The code ran and displayed “testing 1,2,3.” one time as it should.
I also noticed the red led on the P254C SD board flashed one time.
I ejected the card, erased it and repeated the test, this time using the “Upload” button. The Serial Monitor is still open at this time.
The red led flashes TWICE and “testing 1,2,3.” is displayed twice on in the Serial Monitor window. I put the SD card into my laptop to verify the file was written to twice.
So I’m a little confused because if I make a change to the code and click “Upload” the red led will flash twice when the Serial Monitor is open and I need it open to see what’s going on.
The only way I can see to get one flash on the red led it to close the Serial Monitor window hit “Upload” then eject the card and read it in my computer but there must be another way.
One monitor is "Console" "Output" monitor and shows the compiling, and uploading... the other monitor is "Serial Monitor" and shows what your program is transmitting after the upload is complete.
Thanks for all your responses. I was able to get the SD card / ADC working with my TMP36 temp sensor for my project. Now I can log data over the next few weeks to see what’s happening in my cold box.
I wanted to ask about making something similar to a toggle flip flop.
My cold box has a primary fan that runs all the time. I’ve added a second fan as a backup incase the primary fan fails., so most of time the secondary fan will not run.
I would like to change this setup so that that fans alternate at power up so both fans get run time.
So if the primary fan is running and the secondary is off, the next time the Uno power cycles the secondary fan will run and the primary fan will be off. On the next power cycle only the primary fan runs etc…
I’m assuming I need a flag of some soft that is stored in memory but sure how to implement this. The only coding experience I’ve had is BASIC in the 1980s and a tiny bit of C with a pic about 10 years ago.