trying to send string to arduino from matlab via serial port.

2 views (last 30 days)
Hello,
I'm working on a little project and i have serched for an answer to my problem with no success: I need to send a string of characters to my arduino uno via usb serial port.
To check if i have succeeded i programed the arduino to light up the build in led on the board every time the arduino recive the letter 'z' and turn off for any other character.
the matlab code suppose to send a string of character ('azazazaz') to the arduino. but when i executed the program all i got is a very fast blinking of the lad and nothing more.
i checked the arduino code separatly and it seems to work fine, i don't know what else to do.
the matlab code:
priorPorts = instrfind delete(priorPorts)
s = serial('com3', 'BaudRate', 9600); fopen(s); fprintf(s, 'azazazaz'); fclose(s);
the arduino code:
int ledPin = 13;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop()
{
char val[8];
Serial.begin(9600);
while (Serial.available() == 0);
Serial.readBytes(val, 8);
for (int i = 0; i < 8; i++)
{
if (val[i] == 'z' )
{
digitalWrite(ledPin, LOW);
Serial.println("Led is Off");
delay(500);
}
else
{
digitalWrite(ledPin, HIGH);
Serial.println("Led is On");
delay(500);
}
}
Serial.end();
}
thank you for your help!

Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!