Arduino with MATLAB: Stopping

Hello all,

This is the first project that I am working on with MATLAB or Arduino. I have experience with C++ and .spin language and I have figured out most of what I need for this particular task. It is very simple: Turn a turntable 45 degrees, take a picture. Do this 3 times and then close out of the program. I will figure out how to trigger and receive from the program at a later date.

I was just wondering if there was any way to stop running the program once I have taken the pictures I need? I have looked at a lot of different places and they suggest just running an empty loop to “stop” the Arduino from doing anything. However, that will not work because I am interfacing that with my MATLAB GUI to where when the pictures are done, I need to return control to MATLAB.

I assume that this is done through Arduino and not MATLAB, but I may be wrong. Please let me know if you need any additional information and thanks in advance for your help.

void setup() {
TurnTable.attach(9); // Servo is connected to digital pin 9
}
void loop(){
TurnTable.write(ServoPos);
for (; ServoPos < 135; ServoPos = ServoPos + 45){
TurnTable.write(ServoPos); //Rotate to ServoPos
delay(1500); //Pause for Picture
//Insert Take Picture Code Here
delay(1500);
}
if(ServoPos == 135){
//stop();
}
}

As it stands MATLAB is not controlling the Arduino. It will run the program as soon as the Arduino is powered or reset. I suggest that you make the turning and picture taking portion of the code into a function and have loop() simply wait for MATLAB to signal the process to run, perhaps by means of a serial interface, call the function and then return to waiting.

Thank You for the quick response HeliBob,

So your suggestion is to move the code from the instructions for the servo from Arduino controlled to MATLAB? Correct me if I am wrong, but that is my interpretation. I start running the code in Arduino after the user clicks a button. Could I place an empty loop to be started after the 135° rotation in Arduino and then somehow have it to where when the user clicked another button on the GUI it would stop the Arduino from running and reading and transfer to a new GUI page?

Thanks for the help,
Bryce

You don't need an empty loop after the final move because you already have the loop() function.

start of loop()
  if serial input available from MATLAB
    read serial input from MATLAB
    if input is "start"
      call action function
    end if
  end if
end of loop() 

action function
  commands to move and take photos
end of function

I am not familiar with MATLAB but a button to send "start" or any other appropriate message to the Arduino must surely be possible. If necessary the Arduino could signal back to MATLAB that the photos had been taken. What you do in MATLAB after clicking the start button is up to you.

That is true, I guess I do have an empty loop from my original void loop. does your first loop have to be a void, or can it be a while or do while? I tried to switch it but came up with errors. Maybe it is expected in the Arduino to have that loop and that is what is automatically executed. I know in .spin language, I could call specific parts of the file and it did not need to be a never-ending loop.

Another question is that with the serial connection, will the serial port open up a window automatically? Because I do not need to show the user any information via the terminal and I do not want it to pop up. If serial communication can be used to end communication between MATLAB and Arduino then I will use the method you have described.

I already have the Arduino code being called by MATLAB and that works fine. It just waits for several seconds after the servo is done rotating because it is still in the loop. After a while of nothing happening, MATLAB thinks that communication fails between the Arduino and itself and pops an error message. I will also post this on MATLAB forums because it may be able to be solved that way too.

Any additional information on how to do it via Arduino would be great because my MATLAB is getting pretty dense and I would rather complicate Arduino than MATLAB. Thanks

Maybe it is expected in the Arduino to have that loop and that is what is automatically executed.

My start of loop() refers to the Arduino loop() function that forms part of any program written and compiled using the Arduino IDE unless you do something about it. The mechanism is there so you may as well use it.

will the serial port open up a window automatically?

If you are referring to the Arduino then no. The Serial terminal window must be opened manually if and when it is needed to input data and show output which is not needed in your case.

After a while of nothing happening, MATLAB thinks that communication fails between the Arduino and itself and pops an error message.

It sounds like you need to change its behaviour. Send the start message from MATLAB and wait until either the Arduino sends back a message indicating that it has done its stuff or until a defined time passes and act accordingly. One thing to be aware of is that initiating serial communication with the Arduino from an external source will cause most, but not all Arduinos to reset and start its code again. This could, of course, be used to your advantage as long as you realise what is going on.