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

10 views (last 30 days)
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

Ameer Hamza
Ameer Hamza on 12 May 2018
Edited: Ameer Hamza on 12 May 2018
Use fwrite() to the data as bytes over the serial port
fwrite(s, a(1,f));
  18 Comments
Ameer Hamza
Ameer Hamza on 12 May 2018
@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 on 12 May 2018
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)

Bushra Ali
Bushra Ali on 12 May 2018
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 on 12 May 2018
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

Categories

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

Community Treasure Hunt

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

Start Hunting!