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.
-
You need to learn how serial communication works. hackingmajenkoblog.wordpress.com/2016/02/01/…Majenko– Majenko2017年02月17日 22:21:10 +00:00Commented Feb 17, 2017 at 22:21
2 Answers 2
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.
-
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.lkdhruw– lkdhruw2017年02月18日 19:03:15 +00:00Commented Feb 18, 2017 at 19:03
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.
-
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.Naseeb Gill– Naseeb Gill2017年02月18日 17:25:23 +00:00Commented Feb 18, 2017 at 17:25