0

First off. This is not in any way a class assignment. This is my own personal work and research. I just want to get that out of the way.

I am learning how to use Matlab with various Arduino projects. I am a seasoned Matlab user but I am fairly new to the entire Arduino space.

I am trying to send some numerical data from Matlab (via a GUI) to my Arduino Uno and have the Arduino write it to my micro SC card. This is a temporary step to my larger project. However, there is no need to go into those specifics as they are outside of the scope of my issues.

I am fairly confident that the Matlab code works and the Arduino code is slightly modified from another project I did where I wrote and read random numbers from my micro SD card.

However, as I run the Matlab code, the Arduino blinks as if it is receiving the data but after I check the micro SD card it remains blank.

I am confident that my Arduino is wired correctly to my micro SD card adapter since this remains the same from my prior project.

Therefore, I am sure I am missing something trivial to get it to work.

I have researched several websites on the subject and their method and mine seem to align very well.

I am fairly certain the problem is in the conditional statement:

if (Serial.available() > 0) {

As you will see.

The Matlab code is below:

arduinoCom = serial('COM3', 'BaudRate', 115200);  
sendData = 5;
fopen(arduinoCom);
fprintf(arduinoCom,'%i',sendData); %this will send 5 to the arduino
fclose(arduinoCom); 
delete(arduinoCom);

The Arduino code is as follows:

#include <SD.h> // load SD library

int chipSelect = 4;  // Chip select pin for the MicroSD Card Adapter
int incomingByte = 0; // for incoming serial data.
File SDF; // Serial data received is saved here.


void setup() {
     Serial.begin(115200); // start serial connection to print out debug messages and data
     pinMode(chipSelect, OUTPUT); // chip select pin must be set to OUTPUT mode
     if (!SD.begin(chipSelect)) { // Initialize SD card
        Serial.println("Could not initialize SD card."); // if return value is false, something went wrong.
     }
     while (!Serial) {
     }
}

void loop() {
        // Open file, Write data, Close file only when you receive data
        if (Serial.available() > 0) {
           incomingByte = Serial.read();
           SDF = SD.open("SerialDataFile.txt", FILE_WRITE); // open "SerialDataFile.txt" to write data
           SDF.println(incomingByte, DEC); // write ASCII-encoded decimal number to file
           SDF.close(); // close file
           }
}  

The expected result would be a file "SerialDataFile.txt" stored on my micro SD card with the value 5.

Thank you for your help!

  • Comments are not for extended discussion; this conversation has been moved to chat. – Nick Gammon Jan 24 at 6:09
  • Look, this is not a forum. Please refrain from answering the question, or making suggestions, in comments. Comments under the question are for clarifying any confusing parts of the question, and requesting further information. – Nick Gammon Jan 24 at 6:10
  • I am not seeing a way to check this answer as complete. - there is some sort of time delay. Check back tomorrow. – Nick Gammon Jan 24 at 6:12
0

The board resets on new USB connection with fopen. Wait 2 secs between fopen and fprintf in Mathlab code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.