2

i have a question on how to program a certain sequence for my robot.

lets say if i would to program to make it run from position a to b, i have a sensor attach to it that should it detect x, it would perform an action called y at the position where it detect x, where action y doesnt change its position.

i would like the robot to continue from where it left after performing action y to go towards b. however i do not know how to pause sequence from a to b and continue from where it left off after performing action y. i am controlling only the motor of the wheels so i can only set the speed of the wheels.

is there a pause function in general( not sleep) in c++ and to continue running its lines of code from where it paused?

user38950
  • 21
  • 1
  • I'm voting to close this question as off-topic because it's a generic programing question, unrelated to Arduino. –  Oct 14 '17 at 16:01
  • i know what you mean, but i can only control my wheels(moving from position from a to b) by setting its speed for a certain amount of time. so lets say for simplicity sake i program my wheels to move in a straight line for 10 seconds, and that it detect x and perform action y at 3 seconds of my wheels moving, how can i make it move the other 7 seconds instead of resetting it back to 10 seconds? – user38952 Oct 14 '17 at 14:31
  • im was the one who posted the question, i do not have enough reputation to write a comment. what you mention would work easily if the robot movement were simple, but my motion is to navigate through a maze actually, where i wrote the whole movement under one function. from what im getting from you, splitting up its movement and checking every second before resetting the timing seems to be pretty difficult and irritating to code it out. wondering if there is a simpler way to do this. – Joseph Ng Oct 14 '17 at 14:51

2 Answers2

1

You can use different ways:

  • Pause/sleep, don't use this, since it will block other activities

  • Poll, poll for time, and check every so-many ms what to do.

  • Timers/Interrupts, every microcontroller has one or more timers which you can use to generate an interrupt to perform an action. The interrupt can change the mode, and in your main program you can check for the mode what to do (go to another position, pick up something etc.

The last one takes a bit more programming, but is the preferred way to easily make changes later on.

There is no 'pause' function in C, but you can of course pause until an interrupt is triggered. In the datasheet of your microcontroller you can read how this can be used.

So in general your loops look like:

loop()
{
   switch (mode)
   {
   case modeMoveAToB: 
       ... move from A to B (just a small portion, like 1 cm)
       break;

   case modeActionY:
       ... Perform action Y
       mode = modeMoveAToB;
       break;
   }

   if (SensorTriggered)
   {
      mode = modeActionY;
      SensorTriggered = false;
   } 
}

In the trigger interrupt you set SensorTriggered to true.

Make sure that the modeActionY does not take long, so let it move e.g. 1 cm, so the sensor (SensorTriggered) is checked every 1 cm. If you need other actions, just add them in the case statement. And make the inside of each case statement a function, so you can easily reuse it later and/or keep the functionality clearly divided.

Michel Keijzers
  • 12,954
  • 7
  • 40
  • 56
0

stop running a certain function when sensor picks up something and return to where it stops?

simple:

1) write that certain function as an ISR - that's not used by the mcu;

2) when the sensor picks up something, set the flag of the respective ISR - execution will jump to the corresponding ISR.

3) sit back and enjoy.

dannyf
  • 2,770
  • 10
  • 13