can someone guide me what i m doing wrong in sending array to arduino I have to use this array further to make logic..

2 次查看(过去 30 天)
MATLAB code is here
> a=4:8
if ~isempty(instrfind)
fclose(instrfind)
delete(instrfind)
end
s=serial('COM3','BaudRate',9600);
fopen(s);
for f=1:5
fprintf(arduino,a(1,f));
pause(1)
end
arduino code is
int data[5]={0,0,0,0,0};
const int RelayPin1=13;
const int RelayPin2=3;
const int RelayPin3=4;
const int RelayPin4=5;
const int RelayPressure=6;
void setup() { Serial.begin(9600); pinMode(2,OUTPUT); pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT);
pinMode(6,OUTPUT); }
void loop() { if(Serial.available()>0) { for (int i=0; i<8; i++) { data[i]=Serial.read(); // read data }
if(data[0]<2)
{digitalWrite (RelayPin1,LOW); }
if(data[0]>2)
{digitalWrite (RelayPin1,HIGH);
delay (5000);
}
if(data[1]<2)
{digitalWrite (RelayPin2,LOW); }
if(data[1]>2)
{digitalWrite (RelayPin2,HIGH);
delay(5000);}
if(data[2]<2)
{digitalWrite (RelayPin3,LOW); }
if(data[2]>2)
{digitalWrite (RelayPin3,HIGH);
delay(5000);}
if(data[3]<2)
{digitalWrite (RelayPin4,LOW); }
if(data[3]>2)
{digitalWrite (RelayPin4,HIGH);
delay(5000);}
if(data[4]>2 && data[4]<8)
{ digitalWrite (RelayPressure,LOW);}
if(data[4]>8)
{ digitalWrite (RelayPressure,HIGH);
delay(5000);}
}
if (Serial.available()<0)
{
digitalWrite (RelayPin1,LOW);
digitalWrite (RelayPin2,LOW);
digitalWrite (RelayPin3,LOW);
digitalWrite (RelayPin4,LOW);
digitalWrite (RelayPressure,LOW);
digitalWrite(LED_BUILTIN,LOW);
}
}

采纳的回答

Ameer Hamza
Ameer Hamza 2018-5-12
编辑:Ameer Hamza 2018-5-12
Use fwrite() to the data as bytes over the serial port
fwrite(s, a(1,f));
  18 个评论
Ameer Hamza
Ameer Hamza 2018-5-12
@Bushra's comment moved here:
can u brief me about how arduino stores the array? I m confused in it .. it seems like the data i m sending isnt storing the way i want to
Ameer Hamza
Ameer Hamza 2018-5-12
The Arduino store array quite similar to MATLAB except that you need to specifically declare the type of array ( int in your case). I suggest you start from a basic program in which you send and read data from Arduino IDE. The Arduino will just look at incoming byte and send it back. For example, declare your loop like this
void loop() {
if(Serial.available()) {
Serial.print(Serial.read());
}
}
if this program works, this can give you some idea for the problem.

请先登录,再进行评论。

更多回答(2 个)

Bushra Ali
Bushra Ali 2018-5-12
yes i sent the vector a to arduino and i wrote println command too. when we write the code arduino() in matlab it gives info regarding arduino board connected saying COM3.

Bushra Ali
Bushra Ali 2018-5-12
can u brief me about how arduino stores the array? I m confused in it .. it seems like the data i m sending isnt storing the way i want to

类别

Help CenterFile 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!

Translated by