0

I want to send numeric value from matlab to arduino but code is not working.

Matlab code is as:

doi = 3 ;
arduino=serial('COM5','BaudRate',9600); % create serial communication object 
fopen(arduino); % initiate arduino communication
fprintf(arduino, '%s', char(doi)); % send answer variable content to arduino
fclose(arduino); 

Arduino code is as:

 int solenoidPin = 13; //This is the output pin on the Arduino
int doi;
void setup()
{
 Serial.begin(9600);
 //pinMode(13, OUTPUT);
 pinMode(solenoidPin, OUTPUT); //Sets that pin as an output
}
void loop()
{
 if(Serial.available()>0)
 {
 doi = Serial.read();
 //Serial.println(doi);
 //Serial.println("\n");
 //doi = doi - 48;
 if (doi == 1)
 {
 //Serial.println(1);
 digitalWrite(solenoidPin, HIGH); //Switch Solenoid ON
 delay(1000); //Wait .15 Second
 digitalWrite(solenoidPin, LOW);
 }
 else if (doi == 2)
 {
 //Serial.println(2);
 digitalWrite(solenoidPin, HIGH); //Switch Solenoid OFF
 delay(2000); //Wait .165 Second
 digitalWrite(solenoidPin, LOW);
 }
 else 
 {
 //Serial.println(3);
 digitalWrite(solenoidPin, HIGH); //Switch Solenoid OFF
 delay(3000); //Wait .180 Second
 digitalWrite(solenoidPin, LOW);
 }
 }
}

I used str2num(doi) also instead of fprintf(arduino, '%s', char(doi)) but no output.

Please give suggestion to correct this.

Thanks.

asked Feb 17, 2017 at 21:33
1

2 Answers 2

0

fopen(arduino); takes some time to initialize the serial connection so you should use pause(2); after fopen(arduino); and in fprintf() send doi as interger i.e. fprintf(arduino, '%i', doi);. Suppose the doi is 1 then code would be as

doi = 1;
arduino=serial('COM5','BaudRate',9600); % create serial communication object 
fopen(arduino); % initiate arduino communication
pause(2);
fprintf(arduino, '%i', doi); % send answer variable content to arduino
fclose(arduino);

Another modification required in arduino program as

doi = doi - 48;

Uncomment this line inside if(Serial.available()>0) block.

answered Feb 18, 2017 at 18:51
1
  • I presumed that you are retrieving the value of doi from some other function then using pause(2) will make your program significantly slow. to avoid this problem you can split your program into three part first part will contain upto fopen() which will be executed when the main program initialized, fprintf() will be executed during runtime and at the end fclose() will be executed to terminate the serial communication. Commented Feb 18, 2017 at 19:03
0

You send a string of ASCII characters representing a number in human readable format.

You then read one of those characters that represents a single digit and then try and use it as if it were a number.

Instead you need to read the whole string of numbers (which means having some way of knowing where the end of the numbers are in the stream of characters that arrive) and then interpret it back into a number again.

answered Feb 17, 2017 at 22:30
1
  • Hello Thanks for your reply but as I am beginner so I can not understand the solution what you suggest completely. Can you please suggest me modification in my above code so that I can solve this problem. Thanks. Commented Feb 18, 2017 at 17:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.