Azzera filtri
Azzera filtri

I want to change brightness of led in Arduino with Matlab

1 visualizzazione (ultimi 30 giorni)
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.

Risposte (3)

Walter Roberson
Walter Roberson il 28 Gen 2016

Cati Kati
Cati Kati il 28 Gen 2016
Thank you for answer, but i should do with serial communication.
  1 Commento
Walter Roberson
Walter Roberson il 28 Gen 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.

Accedi per commentare.


bachelor student
bachelor student il 20 Set 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 Commento
Walter Roberson
Walter Roberson il 20 Set 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.

Accedi per commentare.

Categorie

Scopri di più su MATLAB Support Package for Arduino Hardware in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by