MATLAB Answers

Convert Arduino Code to Matlab

66 views (last 30 days)
Kim Soltau
Kim Soltau on 16 May 2019
Answered: Mark Sherstan on 17 May 2019
Hello,
is it possible to convert the following code from the Arduino IDE to Matlab?
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0) ;
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high<<8) | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}
Or is it possible to have this Code on the Arduino and to give the results to matlab?

  0 Comments

Sign in to comment.

Answers (1)

Mark Sherstan
Mark Sherstan on 17 May 2019
Converting from Arduino (C++) to MATLAB has some challenges as describere here but it can be done. There are some converters online that will get you started but you will need an understanding of both langauges to ensure the code is "apples to apples".
The question is what are you trying to achieve? If you are acquiring raw values and processing them on the Arduino you could pass those raw values directly to MATLAB and process them there. The example below shows you how:
MATLAB
% Connect to serial port and set properties
s = serial('/dev/cu.usbmodem14101', 'BaudRate', 9600);
s.InputBufferSize = 20;
s.Timeout = 4;
fopen(s);
% Pause to begin a flow of data
pause(3);
fprintf("Connection established\n")
% Start a counter and timer
count = 0;
tic
startTimer = toc;
% Get data for 15 seconds
while (toc < startTimer+15)
% Perform the header checks and get cast bytes to ints
if (fread(s, 1) == 159)
if (fread(s, 1) == 110)
x = fread(s, 2);
analogOut = typecast(uint8(x), 'uint16');
end
end
% Display data to the user
fprintf("%d\n", analogOut)
% Increment counter
count = count + 1;
end
% Display sample rate to user
endTimer = toc;
fprintf("Sample rate was: %0.2f Hz\n", count/(endTimer - startTimer))
% Remove serial port connection
fclose(s);
delete(s)
clear s
Arduino
int analogValue;
void setup() {
// Setup serial port
Serial.begin(9600);
}
void loop() {
// Read the analog pin
analogValue = analogRead(A0);
// Write bytes via serial
writeBytes(&analogValue);
}
void writeBytes(int* data1){
// Cast to a byte pointer
byte* byteData1 = (byte*)(data1);
// Byte array with header for transmission
byte buf[4] = {0x9F, 0x6E, byteData1[0], byteData1[1]};
// Write the byte
Serial.write(buf, 4);
}

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags