필터 지우기
필터 지우기

I want to change brightness of led in Arduino with Matlab

조회 수: 1 (최근 30일)
Cati Kati
Cati Kati 2016년 1월 28일
댓글: Walter Roberson 2016년 9월 20일
Hi all,
I want to send a data(numbers) from matlab to arduino, and arduino will read this data. After that, Arduino will set the brightness with this data. How can i do that?
Here is my arduino code:
int ledPin =13;
void setup() {
/* initialize serial */
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
int abb = Serial.read();
int acc = abb + 5;
Serial.write(acc);
}
}
Here is my MATLAB code:
delete(instrfindall);
clear s
arduinoCom = serial('COM6','BaudRate',9600); % insert your serial
sendData = 100;
fopen(arduinoCom);
fprintf(arduinoCom,'%i',sendData); %this will send 100 to the arduino
fscanf(arduinoCom) %this will read response
%or use BytesAvailableFcn property of serial
Thank you.

답변 (3개)

Walter Roberson
Walter Roberson 2016년 1월 28일

Cati Kati
Cati Kati 2016년 1월 28일
Thank you for answer, but i should do with serial communication.
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 1월 28일
Fading is a question for the Arduino side rather than the MATLAB side.
https://www.arduino.cc/en/Tutorial/Fade
If your question is not about how to fade then you need to clarify what your question is, as you appear to already have implemented basic communication between MATLAB and Arduino.

댓글을 달려면 로그인하십시오.


bachelor student
bachelor student 2016년 9월 20일
hello, your program have problem on both sides; on arduino side, you should not use Serial.write(), it can only send one byte and you want an integer to be sent which is 4 bytes, use Serial.println, it keeps sending till one sentence meets end, for example if it is a short data it wait until it sends 2 bytes and then go for new line, and on matlab side you should implement a loop, you cannot have a loop on arduino side but no loop on taking side which is matlab here. best of luck to you,
hope it works
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 9월 20일
You can use Serial.write with a minor effort:
void write_int(int val) {
Serial.write( (uint8_t *) &val, sizeof(int)/sizeof(uint8_t) );
}
Serial.println is not the same thing: it formats the numbers as text instead of sending them as binary.
On the MATLAB side,
fread(arduinoCom, 1, '*int32') %assuming Arduino int is 4 bytes
You might need to use swapbytes() on the values.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile 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!

Translated by