I want to change brightness of led in Arduino with Matlab
3 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(3 个)
Cati Kati
2016-1-28
1 个评论
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
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
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 Center 和 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!