Hi folks, I'm trying to get an SD card running on a test MEGA. (It's a 1280, although the other boards are 2560s)
The SD card is mounted in a DF Robot MicroSD V1.0 card. The connection to the board is via soldered cables, plugged into pins 50-53, with power and ground coming from the nearby 5V and GND pins. The LED on the SD module lights up.
The SD card was known good in another application, although the module just got out of its bag this morning.
The MEGA board has been used for in-shop testing and has not had problems before.
I have been using the serial monitor extensively for troubleshooting for several months.
I can not program the board without unplugging the SD module. That's probably normal with the SPI pins being used also for programming. However, this means I can't read what's going on, on the serial port. Plus the only way to power the board is with the USB right now, so ... could it be interference between the different uses for the pins? That seems very unlikely, I've probably missed a configuration line.
(Let's not get into the quality of the soldering, it was for a quick test. )
I can see that I have:
MISO (brown) -> 50
MOSI (white) -> 51
SCK (blue) -> 52
SS (green) -> 53
The code is basically the same as the sample code. There is definitely a begin, open, write, and close in the handling. The onboard LED is flashing every 100ms. MEGA.h only defines pins.
What am I missing here?
/*******************************************************************************
*
* Test code: SD logging
*
******************************************************************************/
#include <avr/wdt.h>
#include <SPI.h>
#include <SD.h>
#include "MEGA.h"
String logData;
unsigned long timeTracker;
int intervalTimer;
int readPosition;
unsigned int heartbeat = 0;
const int SDSelect = 53;
/*******************************************************************************
*
* setup()
*
* One-time run on power-up.
*
******************************************************************************/
void setup()
{
Serial.begin( 9600 );
//This is from the hex code in the datasheet. We're going to set up the
//auto-running compare register with the 0xAF value. This gives us a
//1ms trigger rate.
//The TIM line is bit-wise operation and shorthand.
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
//Set up a four second watchdog timer. This will reset the controller, but I'll have to figure out
//a smart way to recover without requiring a complete reset.
wdt_enable( WDTO_4S );
Serial.print( "Initializing SD card..." );
// see if the card is present and can be initialized:
if( !SD.begin( SDSelect ) )
{
Serial.println( "SD error. Logging disabled." );
}
else
{
Serial.println( "SD card ready." );
}
}
/*******************************************************************************
*
* loop()
*
* Runs from top to bottom, the back to the top, as long as power is applied.
*
******************************************************************************/
void loop()
{
wdt_reset();
if( heartbeat > 500 )
{
heartbeat = 0;
//digitalWrite( 13, !( digitalRead( 13 ) ) );
}
if( intervalTimer >= 100 )
{
digitalWrite( 13, !( digitalRead( 13 ) ) );
intervalTimer = 0;
readPosition = analogRead( positionSensor );
//It's the only way string concatenation seems to work on Arduino.
logData = "";
logData += timeTracker;
logData += ": ";
logData += readPosition ;
logData += "," ;
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File SDdataFile = SD.open( "positionData.txt", FILE_WRITE );
// if the file is available, write to it:
if( SDdataFile )
{
SDdataFile.println( logData );
SDdataFile.close();
// print to the serial port too:
Serial.print( logData );
Serial.println();
}
// if the file isn't open, pop up an error:
else
{
Serial.print( "Could not write to SD card." );
Serial.println();
}
Serial.print( "Position: " );
Serial.print( readPosition );
Serial.println();
}
}
/*******************************************************************************
*
* ISR: millisecond timing
*
* This has a 1ms call frequency, so we can set this up to trigger anything
* at arbitrary times. Everthing triggered requires its own flag and then
* its own reset.
*
* The ISR name is Arduino-coded and can't be changed.
*
*******************************************************************************/
ISR( TIMER0_COMPA_vect )
{
heartbeat++;
timeTracker++;
intervalTimer++;
}