Making a robot stop and hold it's position [SOLVED]

I'm familiar with blink without delay but I don't understand how that would help.

The example shows you how to avoid using delay. Learn from it, and your life will be immensely richer.

Thank you for your reply and your time, I do appreciate it and I am trying to learn.

I shouldn't have even mentioned the use of delay.

From my understanding delay simply stops the program from running until a specified time has passed. And blink without delay using millis lets the program run and checks the inputs after a specified time has passed. I'm trying to avoid any specified time. I'm looking to stop the movement of the robot during the loop() for an infinite amount of time when a certain condition exists.

To clarify in my simple mind I can use delay to stop everything from executing for let's say 2000 ms. Or I can use blink without delay (millis) and recheck the values after 2000 ms. I'm confused how that would stop the robot from moving for any where between 2 seconds to 15 minutes.

I apologize if I'm difficult. I'm just trying to figure this out.

"I'm looking to stop the movement of the robot during the loop() for an infinite amount of time when a certain condition exists."

while (condition exists){ // such as digitalRead(pinX) == LOW
// hang out
}
// continue

Thank you for your input as well CrossRoads. In my code posted above am I on the right path with the while statement?

while (ir > Middle && (millis() - lastBlockTime < 200))

You want it to continue when either becomes false? i.e. ir <= Middle OR >=200 mS have elapsed?

No, I want it to stay still when both of those are true. Meaning when the IR is greater than Middle and the last block time (last time the camera saw red) is less than 200.

I also have it programed to turn and avoid objects if the camera hasn't seen red in over 200 and IR is greater than middle. That works perfectly.

Thank you again.

I apologize! Yes! I stepped away from the computer and realized what you meant. Exactly, stay still when both are true or continue when either are false. Thanks again.

Hi I am going build a pick and place robot which can detect the object and place in a appropriate place so can I get the code for it or if I get the corrected code of your project "Making a robot stop and hold it's position" It will be helpful for me

Sameer_Shaikh: Hi I am going build a pick and place robot which can detect the object and place in a appropriate place so can I get the code for it or if I get the corrected code of your project "Making a robot stop and hold it's position" It will be helpful for me

I don't really understand your reply.

CrossRoads: while (ir > Middle && (millis() - lastBlockTime < 200))

You want it to continue when either becomes false? i.e. ir <= Middle OR >=200 mS have elapsed?

Is that how I should write the second part of the while statement? Or is the while statement not my best option here? I played with a case and switch before. But that caused all kinds of goofy behavior. Thanks again.

I think while ( ) { } is your best option, you just need to figure out the logic you need. I would guess that (millis() - lastBlockTme) won't be true very long; 200mS is not a long amount of time when mechanical stuff is involved.

  while ( (ir > Middle) && ( (millis() - lastBlockTime) < 200) )
  {
    Rest();
    read_Ir();
    ScanForBlocks();
  }

(added a couple more ( ) to make the tested parts more clear) and nothing in your code appears to update last BlockTime, so it won't be in that while hold time for long no matter what ir does. Is that really what you want?

I'll add in the extra () that you did and try that. I could definitely go to 500mS and maybe even 1000 but 1000 may give it some false positives. I'll try increasing the mS until I get behavior I don't want. You are also right about the last block time, thank you for catching that error. I think I need to replace ScanForBlocks(); with Camera();. The Camera(); loop produces the lastBlockTime.

I'll give all of that a try and see what happens. Thank you again.

Now some interesting things are happening. I tried quite a few changes that didn’t yield any results. One thing I did try was increasing the 200mS to 500mS, 1000mS and all the way to 5000mS. That in itself didn’t change anything when the robot was looking at a red object (which is what the camera is programed to look for and produces the lastBlockTime). However, if I showed the robot a red object then quickly swapped it out for another object the robot would hold it’s rest(); position for approximately 5 seconds. I’ve got to think this out a little.

Whoa! It works! I adjusted the millis all the way up to 50000. I correctly assumed that would cause it to go into rest(); if the IR sensor detected something during that time even if the red object wasn't in front of the camera. meaning if a wall or furniture was detected within the Middle range of the IR it would go into Rest(); To fix that I increased the distance the IR sensor will cause the robot to turn and avoid an object if it doesn't see red. 50,000 millis wouldn't be enough time to hold the position so I upped it again to 500,000. I only poll the IR in the while statement now. Now the robot will hold it's position for a maximum of 8 minutes (which should be enough time). Most importantly, if the object moves during that time it will go back to scanning for the object or tracking it appropriately. It seems like the battery is getting low from all this use so I'm going to charge it and recheck all it's motions before I tag this as solved. Below is what the current loop looks like right now.

void loop()
{
  read_Ir();
  ScanForBlocks();
  camera();

  while ((ir > Middle) && (millis() - lastBlockTime < 500000))
  {
    Rest();
    read_Ir();
  }
  if ((panLoop.m_pos < RCS_CENTER_POS) && ir > Far && (millis() - lastBlockTime > 500))
  {
    Turn_Right(200);
  }
  else if ((panLoop.m_pos > RCS_CENTER_POS) && Far > Middle && (millis() - lastBlockTime > 500))
  {
    Turn_Left(200);
  }
}

A very big thank you to you guys for your input so far. Any other advice is always appreciated.

Scott

Thank you very much to the Arduino forum especially CrossRoads. The robot does for the most part exactly as I had wished for. It does stop and hold in the rest() position when appropriate. It also can still roam the house avoiding walls and furniture. I had to make a few minor adjustments in the turn left and turn right obstacle avoidance to avoid false positives (bring the mills - lastBlockTime > back down to 200). I need to do more fine tuning of the IR values and perhaps speed up the pan servo proportional gain to make it run flawlessly. But that wasn't what the topic was about. The robot does stop and hold it's position now until the object moves. Next up is fine tuning adjustments and adding the sound back to it!

Thank you again for your help.

Scott

Glad you got it working.

Thanks for your suggestions it will help me in my project and if I have any doubt I will post it here

Thanks once again

Hey Scott_Callahan can I get all details of your project.because I am going make similar to your robot so if I get the details it will be helpful for me

Sameer_Shaikh

Sameer_Shaikh: Hey Scott_Callahan can I get all details of your project.because I am going make similar to your robot so if I get the details it will be helpful for me

Sameer_Shaikh

While I'd like to be of great assistance you need to know that I know very little about code or robots. I am no expert at all and have no training. Anything I have figured out is from mostly trial and error. I'll spend over a month on trying new ideas and searching the internet before I go to a forum like this to ask for advice. This is a very new hobby to a nearly 40 year old blue collar worker who's college education didn't even earn an AA. I'm sure that shows in my grammar and punctuation. I have no idea what you mean by "all the details". My robot's functions and your idea seems to me to be very different. Not to mention I have no idea if your robot walks or is on wheels or tracks. I read a while ago to start simple and tackle one part of code at a time. I really try to do that and I'd suggest you do the same. Get a robot you think will physically do what you want and get it to move then go from there. After that the best advice I can offer is if you want a robot that can track color, google PixyCam and PixyPet. That is what changed my robot from a simple device into one that can seek certain objects out. I promise you, if I just gave you my code it would be absolutely worthless to you and you'd have more questions than answers. One more piece of info, I can't remember how I found it but I saw a robot on the internet a while ago that was an arm that seeked out certain color objects, picked them up and moved them to another location. The code for that was significantly different than mine and was no use to me. However, that seems more what you are looking for.

Where did u saw that video and how did you get the code of that robot if saw it on YouTube how did you get it's code

I'm making some adjustments on the code to make the robot a little more accurate. I'm trying to have the robot take 1 small step before it goes into the rest position with the hopes that a small movement will help the pan servo stay centered on the object. I first simply wrote Walk(5); into the while statement. But of course all the robot did was alternate between Walk(); and Rest(); So I wrote it like what is posted below. It seems to work, but the Walk(5); is such a small step I can't really tell if I wrote it right. Can anyone tell me if this is appropriate? Specifically how I added the first if()

Thanks again

void loop()
{
  read_Ir();
  ScanForBlocks();
  camera();

  if ((ir > Middle) && (millis() - lastBlockTime < 200))
  {
    Walk(5);
    {
      while ((ir > Middle) && (millis() - lastBlockTime < 600000))
      {
        Rest();
        read_Ir();
      }
    }
  }