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

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);
}
}

 Accepted Answer

Use fwrite() to the data as bytes over the serial port
fwrite(s, a(1,f));

18 Comments

What is not working? Is it giving some error or the Arduino output is not working as required?
arduino output wasnt working but i tried again now its working.. thank u so much :)
another issue i observed is all the values are storing in index 1. all the other variables are detected as empty. can u help me with this?
It depends on what you want to do in your Arduino code. Please add detail to what are you trying to do in Arduino and attach Arduino sketch file to your comment using paper clip button.
I am trying to control 5 relays . i extract the data by imaging processing in matlab then i imported this data in arduino now relay isnt working as per i want to u can check the program and guide me where i m wrong
whatever the values i send to arduino it gives 2 pin high and all other as low sometimes 2 3 4 as high but even when the condition satisfies it gives 5 6 low
Since I don't have your hardware, it is hard to debug, but there are few comments which might help.
1) In your MATLAB code, you are giving a pause of 1 second between writes, why do you need that? Try to remove it.
2) In your Arduino code you have
if(Serial.available()>0)
but in reality you are expecting 5 values, so try to change it like this
if(Serial.available()>=5)
3) Try to add Serial.print() debugging statements in your Arduino code. If I was trying to debug, I will print back all the values after reading to confirm that Arduino read them correctly e.g.
// your code
for(int i=1;i<6;i++)
{
matlabData[i]=Serial.read();
}
// I would add this, to echo back the data
for(int i=1;i<6;i++)
{
Serial.println(matlabData[i]);
}
also, you mentioned pins 5 and 6 remain low, so I will add few debugging lines to see if Arduino enter the if block. For example
if(g4>2)
{
Serial.println("I can reach here");
digitalWrite(R4,HIGH);delay(5000);
}
Now to read the values send back by Arduino, you will need few extra steps, initialize serial port with a large input buffer,
s=serial('COM3','BaudRate',9600, 'InputBufferSize', 1024*1024);
% 1 megabyte of buffer
and read the value returned by Arduino like this
char(fread(s, s.BytesAvailable)')
this will show what Arduino is sending back at serial port.
thanx alot I am gona try it .. hopefully it will work for me :)
It means that your Arduino is not sending anything back. Are you sure that Arduino is connected at COM3? Also, did you check this after sending the vector a to Arduino?
@Bushra's comment moved here:
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.
This is quite strange because the second for loop should send the data back. Try sending data through the serial monitor of Arduino IDE and see if there is any echo.
@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
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.

Sign in to comment.

More Answers (2)

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.

1 Comment

Please use the comment section for discussion. I have moved your answer to comment. Please delete this answer.

Sign in to comment.

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

Community Treasure Hunt

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

Start Hunting!