MATLAB Answers

Shacks
1

Simulink and arduino connection using instrumentation control toolbox

Asked by Shacks
on 23 May 2013
Latest activity Commented on by Josephine Gotz on 15 Jun 2016
Hello, I am trying to send data from simulink to matlab using instrumentation toolbox. I have a 'constant block' connected to a 'to instrument block '. I want to turn a couple of lights on and off. (In reality i want to do something more complex but i need arduino to receive numbers from simulink(0-180)) As of now the arduino receives a signal but the arduino is not able to understand the received signal(I know this cause the receive light turns on when i run the simulation). The arduino code i am using is shown below
/************************* Private Variables(serial reading)*****************/ unsigned long loopTime, currentTime; const int serStrLen = 30; char serInStr[ serStrLen ]; // array that will hold the serial input string /*************************************************************/ const int ledPin = 13; const int ledPin2 = 12;
int ch; // Values to send on channels (duration of pulse minus start, in microseconds)
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(ledPin2, OUTPUT); // Set servo pin as an output pin
Serial.begin(9600); // connect to the serial port
Serial.flush();
}
void loop() {
if( readSerialString() )
{
Serial.println(serInStr);
}
ch = abs(atoi( serInStr ));
if (ch==5 )
{digitalWrite(ledPin,HIGH);
digitalWrite(ledPin2, LOW);}
else
{digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin, LOW);}
}
/************* Serial Reading ************************************/
uint8_t readSerialString() { if(!Serial.available()) { return 0; } delay(10); // wait a little for serial data
memset( serInStr, 0, sizeof(serInStr) ); // set it all to zero
int i = 0;
while(Serial.available() && i<serStrLen ) {
serInStr[i] = Serial.read(); // FIXME: doesn't check buffer overrun
i++;
}
return i; // return number of chars read
}
The know the above code works because when I send numbers via serial monitor, it works how it is supposed to.But when i try to send number 5 via simulink, the arduino does no respond the same way.
I am guessing i have to set a particular datatype in the 'to instrument' box or the ascii format string section in the 'to instrument' box settings.
Pls Help, any help would be appreciated.

  2 Comments

Update:: I noticed, the simulink model kind of works, I am unable to send 2 or three digit number, The arduino responds as if i am sending 2 or 3 separate single digit number. So now the question is , how do i send 2 or 3 digit numbers to arduino ?
Hey, did you manage to solve this? I think maybe I have a similar problem. Thanks

Sign in to comment.

3 Answers

Answer by Jeremy
on 16 Oct 2013
 Accepted Answer

hi Shacks, I have a quick question for you: how did you configure your 'to instrument'? i.e. under 'hardware configuration'I am wondering if that is my problem. I am not having any luck getting the simplest toy model, a constant-connected-to-a'to instrument' model to build. I get a generic 'errors encountered while building' error.

  0 Comments

Sign in to comment.


Answer by Shacks
on 30 May 2013

I feel like i am talking to myself, anyways future reader(s) i solved it, just type in '%i\n' in ascii string format setting.

  0 Comments

Sign in to comment.


Answer by Shacks
on 16 Oct 2013

Under output format, i selected ASCII Under format string '%i\n' Time out : 10 Buffer Size :8192 Interface: Serial Port: Select the right one Baudrate:9600
obviously the hardware setting should match to that of arduino. Under instrument initialization I made no changes.
Hope this helps.

  0 Comments

Sign in to comment.