Converting Arduino Code to MATLAB Code

Hello all,

I'm having trouble trying to convert an Arduino code into Matlab Code and I'm not very experienced in the programming aspect :confused: I want to be able to input and arduino code into the Simulink Matlab function block so I can read in encoder data and use it for other applications.

Could someone help me convert the code below into a Matlab code?

const int CLOCK_PIN = 2;
const int DATA_PIN = 3;
const int BIT_COUNT = 15;  

void setup() {
  pinMode(DATA_PIN, INPUT);
  pinMode(CLOCK_PIN, OUTPUT);
  
  digitalWrite(CLOCK_PIN, HIGH);

  Serial.begin(115200);
}

void loop() {
  float reading = readPosition();
  Serial.println(reading,2);
  delay(25);
}

float readPosition() {
  unsigned long graysample = shiftIn(DATA_PIN, CLOCK_PIN, BIT_COUNT);
  delayMicroseconds(100);  

unsigned long binarysample = grayToBinary32(graysample);

  return ((binarysample * 360UL) / 32768.0); 

unsigned long shiftIn(const int data_pin, const int clock_pin, const int bit_count) {
  unsigned long data = 0;

  for (int i=0; i<bit_count; i++) {
    data <<= 1; // shift all read data left one bit.
    
     
  
    PORTD &= ~(1 << 2); 
    delayMicroseconds(1);
   
    PORTD |= (1 << 2); 
    delayMicroseconds(1);

    data |= digitalRead(data_pin); 

  return data;
}

unsigned int grayToBinary32(unsigned int num)
{
    num = num ^ (num >> 16);
    num = num ^ (num >> 8);
    num = num ^ (num >> 4);
    num = num ^ (num >> 2);
    num = num ^ (num >> 1);
    return num;
}

This code is a code that is in an Arduino forum about absolute encoders and I added an additional section to convert gray code output into binary output.

So far the return statements and brackets are my main issue, but I have a feeling there's plenty of more issues :confused:

Let me know and thank you for your time.

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

MATLAB does not support Arduino-like digital input and output on any computer without special hardware. See Data Acquisition Toolbox. What computer are you using to run MATLAB and how do you plan to equip it to read the encoder?

It is possible to have MATLAB communicate via the serial port with an Arduino, and use the Arduino to do digital or analog input and output. Google "matlab arduino" for more information.

Right now I'm using my hp laptop where I downloaded the Matlab-Arduino package for Simulink. I can get the Arduino pins on Simulink and I can read the encoders with the Arduino code I listed, but I need to get the code to submit through Simulink.

I'm using an SN75179BP Driver/ Receiver to combine the Data +- and Clock +- into a single output and input. Encoder outputs gray code with SSI communication and converted to binary and read by Arduino code. I can see the encoder position using the serial monitor on the arduino sketch so I know the code works but I don't know how to declare constants or write simply code into matlab :confused: