I want to change brightness of led in Arduino with Matlab
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (3)
Cati Kati
le 28 Jan 2016
1 commentaire
Walter Roberson
le 28 Jan 2016
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
le 20 Sep 2016
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 commentaire
Walter Roberson
le 20 Sep 2016
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.
Voir également
Catégories
En savoir plus sur MATLAB Support Package for Arduino Hardware dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!