Matlab to Arduino Communication with Array
이전 댓글 표시
Hello Everybody, I haven't seen to many answers to this question, but I can't get anything to work between my Arduino and matlab code when I want to send an array. Sending a single value is fine, but sending many values is very difficult. Below are my codes. I can't seem to figure anything out, and my personal opinion is that the For loop in arduino code is the incorrect way to read the array. Any and all help is very much appreciated! (PS: Some of the stuff in the arduino code is for setting a DAC to do electric signals. The link for this part can be found here: https://learn.adafruit.com/mcp4725-12-bit-dac-tutorial?view=all)
Matlab Code:
clear all
clc
output = [1,2,1];
data = char(output);
s = serial('COM4','BaudRate',9600);
fopen(s);
fprintf(s,'%d',data);
fclose(s);
Arduino Code:
#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
int ledPin=13;
int i;
char matlabData;
char arduinovalue[3];
void setup() {
pinMode(ledPin,OUTPUT);
Serial.begin(9600);
dac.begin(0x62);
}
void loop() {
int uge = sizeof( arduinovalue ) / sizeof( int );
if (Serial.available() > 0) {
for (i = 0; i < uge; i++){
arduinovalue[i] = Serial.read();
}
}
else{
for (i = 0; i < uge; i++){
arduinovalue[i] = 0;
}
}
for (i = 0; i < uge; i++){
if(arduinovalue[i]==1){
digitalWrite(ledPin,HIGH); // turn light on
dac.setVoltage(0, false);
}
else if(arduinovalue[i]==2){
digitalWrite(ledPin,LOW); // turn light off
dac.setVoltage(4095, false);
}
}
}
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!