matlab serial communication with arduino is very slow

4 Ansichten (letzte 30 Tage)
Mahsa Rm
Mahsa Rm am 5 Jan. 2022
Bearbeitet: Mahsa Rm am 5 Jan. 2022
I want to send commands with matlab, to arduino uno which is connected to a 4channel relay. but after about 8 seconds of communicating, it takes so long to send commands. so arduino recieves my command late and relay will change state after about 20 seconds delay!
here is my matlab code:
sr = serial('COM4','BaudRate',9600);
fopen(sr); % open Arduino USB port
while 1
command = input('say the command:');
if (command == 0)
break;
end
fprintf(sr,char(command),'async');
end
fclose(sr);
a = instrfind();
fclose(a);
and here is my aruino code(void loop code). and help will be appreciated.
if(Serial.available()>0) // if there is data to read
{
matdata = Serial.read(); // read the data
if (matdata==1)
{digitalWrite(relaypin1,HIGH);
}
else if(matdata==2)
{digitalWrite(relaypin1,LOW);
}
if(matdata==3)
{digitalWrite(relaypin2,HIGH);
}
else if(matdata==4)
{digitalWrite(relaypin2,LOW);
}
if(matdata==5)
{digitalWrite(relaypin3,HIGH);
}
else if(matdata==6)
{digitalWrite(relaypin3,LOW);
}
if(matdata==7)
{digitalWrite(relaypin4,HIGH);
}
else if(matdata==8)
{digitalWrite(relaypin4,LOW);
}
}
else
{Serial.println('None');}

Antworten (0)

Kategorien

Mehr zu Instrument Control Toolbox finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by