Writing text and image data to SD card

Hi, I am having some problems writing both image and text data to SD card. The following code uses various sensors to log text data, and write it to SD card as a .csv file. It also uses a PIR sensor controlled camera to take pictures and write them to the same SD card. When I run the code and leave the PIR sensor undisturbed it runs fine, writing the text data as expected. However, as soon as i disturb the PIR sensor to take a picture, I get an error #### Exception System.IO.IOException - CLR_E_INVALID_DRIVER (1) ####
the first picture writes to the card ok, but no further data (text or image) is placed on the card. How can i solve this issue? Am I confusing the SD card by expecting it to write two types of data simultaneously? thanks and sorry for the long message!

namespace sdcardtexttemplog
{
    public partial class Program
    {
        private GT.Timer _getReading = new GT.Timer(40000);
        private double _t1;
        private double _h;
        private double _lx;
        private double _p;

        int pictureIndex = 1;
        FileStream myFile;
        StreamWriter stream;
        TextWriter writer;
        GT.StorageDevice storage;
        GT.Timer timer;
        void TheLoop()
        {
            while (true)
            {

            }
        }
        void ProgramStarted()
        {
            Debug.Print("The Eagle Has Landed");
            sdCard.MountSDCard();
            motion_Sensor.Motion_Sensed += new Motion_Sensor.Motion_SensorEventHandler(motion_Sensor_Motion_Sensed);
            camera.PictureCaptured += new Camera.PictureCapturedEventHandler(camera_PictureCaptured);
            if (sdCard.IsCardMounted)
            {
                storage = sdCard.GetStorageDevice();
            }
            else
            {
                Debug.Print("SD CARD NOT MOUNTED PROPER");
            }
            if (sdCard.IsCardMounted && storage != null)
            {
                myFile = storage.Open("data" + ".csv", FileMode.Create, FileAccess.Write);
                writer = new StreamWriter(myFile);
                writer.Write("Temp");
                temperatureHumidity.MeasurementComplete += new GT.Modules.Seeed.TemperatureHumidity.MeasurementCompleteEventHandler(temperatureHumidity_MeasurementComplete);
                temperatureHumidity.StartContinuousMeasurements();
                barometer.MeasurementComplete += new GT.Modules.Seeed.Barometer.MeasurementCompleteEventHandler(barometer_MeasurementComplete);
                barometer.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 40);
                barometer.StartContinuousMeasurements();
                _getReading.Tick += new GT.Timer.TickEventHandler(_getReading_Tick);
                _getReading.Start();
            }
            else
            {
                temperatureHumidity.MeasurementComplete += new GT.Modules.Seeed.TemperatureHumidity.MeasurementCompleteEventHandler(temperatureHumidity_MeasurementComplete);
                temperatureHumidity.StartContinuousMeasurements();
                barometer.MeasurementComplete += new GT.Modules.Seeed.Barometer.MeasurementCompleteEventHandler(barometer_MeasurementComplete);
                barometer.ContinuousMeasurementInterval = new TimeSpan(0, 0, 0, 40);
                barometer.StartContinuousMeasurements();
                _getReading.Tick += new GT.Timer.TickEventHandler(_getReading_Tick);
                _getReading.Start();
            }
        }
        void temperatureHumidity_MeasurementComplete(GT.Modules.Seeed.TemperatureHumidity sender, double temperature, double relativeHumidity)
        {
            _t1 = temperature;
            _h = relativeHumidity;
        }
        void barometer_MeasurementComplete(GT.Modules.Seeed.Barometer sender, GT.Modules.Seeed.Barometer.SensorData sensorData)
        {
            _p = sensorData.Pressure;
        }
        void _getReading_Tick(GT.Timer timer)
        {
            _lx = lightSensor.ReadLightSensorPercentage();
            Debug.Print("Temperature : " + _t1 + " degrees ");
            Debug.Print("Humidity : " + _h);
            Debug.Print("Light : " + _lx);
            Debug.Print("Pressure : " + _p);
using (FileStream myFile = File.Open("\\SD\\_data.csv", FileMode.Append))
            {
                byte[] logMsg = UTF8Encoding.UTF8.GetBytes("temperature " + _t1 + " degrees" + "," + _h + "," + _lx + "," + _p + "," + "\n");

                myFile.Write(logMsg, 0, logMsg.Length);
                myFile.Flush();
        }  
        }
        void motion_Sensor_Motion_Sensed(Motion_Sensor sender, Motion_Sensor.Motion_SensorState state)
        {
            camera.TakePicture();
        }               
        void camera_PictureCaptured(Camera sender, GT.Picture picture)
        {
            int pictureIndex = 1;
            try
            {
                String filename = "picture_" + pictureIndex + ".jpg";                
                sdCard.GetStorageDevice().WriteFile(filename, picture.PictureData);
                pictureIndex++;
                sdCard.UnmountSDCard();
            }
            catch
                {
                    Debug.Print("broken");
                }
            
            }
        }
    }

You are unmounting sd card after the picture is taken.

thanks! I have no error now, but still only get one picture saved to the Sd card despite taking many, do I need to unmount the SD card in a different part of the code?

I think you have to make sure that card is ready before capturing another picture.

@ Architect -

You know more about this than I do…

If you unmount the SDCard wouldn’t you need to mount it again before use…

@ willgeorge - Yes, you need to mount it again.

I’d approach it slightly differently. You should register for the SD card events and move the code you have in ProgramStarted that sets up the storage to the correct handlers, and then I’d always do the validation that the storage is mounted and ready when you want to write the picture to the card.

Thanks very much guys, problem solved!

@ lauriebelch -

GREAT! But what solved your problem?