MATLAB Answers

Convert Arduino Code to Matlab

69 ビュー (過去 30 日間)
Kim Soltau
Kim Soltau 2019 年 5 月 16 日
コメント済み: Warren Dennis 2020 年 5 月 14 日
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?

回答 (1 件)

Mark Sherstan
Mark Sherstan 2019 年 5 月 17 日
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);
}
  1 件のコメント
Warren Dennis
Warren Dennis 2020 年 5 月 14 日
I would like to convert the this code from Arduino to Matlab
// Define Pins
#define BLUE 3
#define GREEN 5
#define RED 6
void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
}
// define variables
int redValue;
int greenValue;
int blueValue;
// main loop
void loop()
{
#define delayTime 10 // fading time between colors
redValue = 255; // choose a value between 1 and 255 to change the color.
greenValue = 0;
blueValue = 0;
// this is unnecessary as we've either turned on RED in SETUP
// or in the previous loop ... regardless, this turns RED off
// analogWrite(RED, 0);
// delay(1000);
for(int i = 0; i < 255; i += 1) // fades out red bring green full when i=255
{
redValue -= 1;
greenValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(RED, 255 - redValue);
// analogWrite(GREEN, 255 - greenValue);
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
delay(delayTime);
}
redValue = 0;
greenValue = 255;
blueValue = 0;
for(int i = 0; i < 255; i += 1) // fades out green bring blue full when i=255
{
greenValue -= 1;
blueValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(GREEN, 255 - greenValue);
// analogWrite(BLUE, 255 - blueValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
delay(delayTime);
}
redValue = 0;
greenValue = 0;
blueValue = 255;
for(int i = 0; i < 255; i += 1) // fades out blue bring red full when i=255
{
// The following code has been rearranged to match the other two similar sections
blueValue -= 1;
redValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(BLUE, 255 - blueValue);
// analogWrite(RED, 255 - redValue);
analogWrite(BLUE, blueValue);
analogWrite(RED, redValue);
delay(delayTime);
}
}

サインインしてコメントする。

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by