Hello to all,
I have started a new project requiring I send data from Matlab into an Arduino DUE board via the USB port, in order to convert the array into a signal. I have taken care to format the data in 12 bits. If I save the data on an SD card, the Arduino reads it very well and manages to send the data out. However, when I send the exact data via the US port, the signal is not generated anymore. Here is my Arduino code:
#include <SPI.h>
#include <SD.h>
int v[800];
int led = 13;
void setup() {
pinMode(led, OUTPUT);
// put your setup code here, to run once:
analogWriteResolution(12);
Serial.begin(9600);
for (int i = 0; i < 800; i++) {
v[i] = 0;
}
int i = 0;
if (!SD.begin(4)) {
return;
}
}
void loop() {
int i = 0;
if (Serial.available() > 0)
{
if(Serial.peek() == 'c') {
Serial.read();
}
while (Serial.available() > 0) {
digitalWrite(13, HIGH);
Serial.read();
v[i] = Serial.parseInt();
i++;
}
i = 0;
}
else {
digitalWrite(13, LOW);
for (int k = 0; k < 400; k++) {
analogWrite(DAC0, v[k]);
analogWrite(DAC1, v[k+400]);
delayMicroseconds(1);
Serial.println(v[k]);
}
}
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
}
// put your main code here, to run repeatedly:
/* int l = sizeof(sl) / sizeof(*sl);
for (int i = 0; i < l; i++) {
analogWrite(DAC0, sl[i]);
analogWrite(DAC1, sr[i]);
//analogWrite(DAC1, v[9-i]);
//Serial.println(s[i]);
delayMicroseconds(1);
}*/
Here is the Matlab code responsible for the data transfer:
s = serial('COM5', 'BaudRate', 9600);
fopen(s);
fprintf(s, '%c', 'c'); % data is coming
% send 1 element at a time as string
for i = 1:length(x)
fprintf(s, '%s', (x(i)));
end
fclose(s)
Can anybody help me?
Best regards,
Jean