Sending double data from Matlab to the Teensy 3.6 via the microUSB

opps, forgot to mention that in the last post. My computer uses little endian and the teensy 3.6 does as well. So that should all be fine and well there.

I'm concerned about sending ASCII because I do not know the number of characters I will be sending over to the teensy. Will I need to code out something to ensure it is, say 5 characters long or is it read over as a complete package like a string (i.e. '02.55' and '10.75')?

Let me have a play around with ASCII. I've modified my code to ping back the value of the sine wave back to matlab and I have it being graphed there.

updated Arduino Code (likely with errors)

#include <SPI.h>  //SPI library for Arduino
#include <math.h>
#include <string.h>

//Teensy 3.6 Constants
uint8_t             chipSelectPin     =               15; 
bool                GO                =            false;

char                startMarker       =              '<';
char                endMarker         =              '>';

const int           ArbMem            =            50e3;
uint16_t            Istim             =               0;

uint16_t LD;
uint32_t  N;
char      SendCode[2];
double  ArbHeap[ArbMem] = {};
double freq;
double  amp;

void setup() {
  SPI.begin();
  delay(10);
  Serial.begin(115200);
  pinMode (chipSelectPin, OUTPUT);

  SPI.beginTransaction(SPISettings(17e6, MSBFIRST, SPI_MODE0));
  digitalWrite(chipSelectPin, HIGH);
  delay(1000);
}

void loop() {
  if (GO) {
    UpdateStim();
  }   
  if (Serial.available() > 0) {
    UNOSerialEvent();
  }
}

void UNOSerialEvent() {
  byte            temp[8];
  ElecPairs[1] = 4;
  ElecPairs[0] = 5;

  byte rb = Serial.read();
  if (rb == startMarker) {
    Serial.flush();

    for (int c = 0; c < 8; c++) {
      temp[c] = Serial.read();
    }
    freq = byteToDouble(temp);

    // get amplitude value second
    for (int c = 0; c < 8; c++) {
      temp[c] = Serial.read();
    }
    amp = byteToDouble(temp);
    
    makeSine(freq, amp);
    GO = true;
  }

  rb = Serial.read();
}


void UpdateStim(void) {

  double Amp = ArbHeap[Istim];

  Serial.write(Amp);
  
  Istim++;
  if (Istim >= N)  {
    Istim = 0;
  }
}

void makeSine(double Freq, double Amp) {
  double   Stim;
  uint8_t  Trim       =                         0x80;
  double   tld        =                        14.25;
  uint32_t FreqUpdate =          floor(1 / (tld * 1e-6));
  uint16_t FreqB      =                           50;
  N                   = (uint32_t)(FreqUpdate / FreqB);
  LD                  =  floor(tld * ((FreqB / Freq) - 1));

  for ( int c = 0; c < (int)N; c++) {
    Stim = Amp*sin((2.0*PI*((double)c))/(double)N);

    ArbHeap[c] = Stim;
  }
}

double byteToDouble(byte doubleNbyte[8]) {

  union {
    double   Out;
    byte   In[8];
  } U ;

  for (int c = 0; c < 8; c++) {
    U.In[c] = doubleNbyte[c];
  }
  return U.Out;
}

Matlab

SerialID = serial('COM5','BaudRate',115200);
fopen(SerialID);
fwrite(SerialID,'<','char');
fwrite(SerialID,10.00,'double');
fwrite(SerialID,1.00,'double');
fwrite(SerialID,'>','char');

figure(1)
c = 0;
while(1)
    c=c+1;
    data(c) = fread(SerialID,1,'double');
    
    plot(c,data(c),'.b')
    if c == 1
        hold on
    end
    drawnow;
end