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
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 ![]()
Let me know and thank you for your time.
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.