can someone guide me what i m doing wrong in sending array to arduino I have to use this array further to make logic..
Show older comments
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
More Answers (2)
Bushra Ali
on 12 May 2018
0 votes
1 Comment
Ameer Hamza
on 12 May 2018
Please use the comment section for discussion. I have moved your answer to comment. Please delete this answer.
Bushra Ali
on 12 May 2018
0 votes
Categories
Find more on Device Driver Blocks 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!