"blink without delay" in servo motors

So I’m trying to program a sumo robot that turns right, left, pauses then repeats if the sensor doesn’t pick anything up, but when it does it should go forward. For simplicity I just made it a button for now with the intent of using a more complex sensor later. My problem is that it won’t execute the forward code when the button is pressed until it runs it’s full rotation of searching. How can I change the code to make the robot go forward as soon as the button is pressed?
Thank you!

#include <Servo.h> 
const int button = 7;    

Servo servoR;
Servo servoL;
int val = 0;

void setup() {
  
  servoR.attach(9);  // attaches the servo on pin 9 to the servo object
  servoL.attach(10); //attaches the servo on pin 10 to the servo object
  pinMode(button, INPUT);     
}

void loop(){

val = digitalRead(button);

  if (val == LOW) {     
     
      turnRight();
      turnLeft();
      stopBot();
  } 
  else {
   forward(); 
  }
}
void forward()
{
servoR.writeMicroseconds(2000);
servoL.writeMicroseconds(2000);
}

void turnRight()
{
servoR.writeMicroseconds(2000);
servoL.writeMicroseconds(1500);
delay (2000); //turns right for 2 secs
}

void turnLeft()
{
servoR.writeMicroseconds(1500);
servoL.writeMicroseconds(2000);
delay(2000); //turns left for 2 secs
}

void stopBot()
{
servoR.writeMicroseconds(1500);
servoL.writeMicroseconds(1500);
delay(2000); //stops the robot for 2 seconds
}

Hello, you could use some timer: http://playground.arduino.cc/code/timer

You need to eliminate the delay() commands in your code and do the timing using the millis() command and change the program structure to avoid it staying in the turnRight() or turnLeft() functions.

Have a look at the BlinkWithoutDelay example to see how to time actions without blocking execution of other code such as, in your case, reading a sensor and acting on it.

While using delay() is not ideal (and should be eliminated) I don't think that's the cause of the described symptoms.

The reason it runs through all its motions before responding to a button press is because that's what your program tells it to do. The program checks the button and, if it's not pressed, it runs through the three motions.

You probably need to check for the button press between each motion.

But then you will need to deal with the direction it should move - because if it moves forward after a turn left it will go in the direction it is then pointing. Maybe that's what you want to happen.

If you want it to stop turning and move forward when it is part way through a turn then you will probably need to set up an interrupt routine that is triggered by the button.

...R

Robin2: [... snip ...] If you want it to stop turning and move forward when it is part way through a turn then you will probably need to set up an interrupt routine that is triggered by the button.

Or, replace those 2 second delays with a loop that runs for 2 seconds. Inside that loop, check the state of the sensor (switch) and if it is triggered immediately send the stop, set a flag that a target has been found, and break out of the loop. In the main loop have it check for the flag state between scanning left and right, and if the flag is set tell it to move forward, or what ever your "attack" mode is. I guess during the attack phase, you should have something similar that resets the flag to re-initiate the search.

I can't imagine that the opponent robot would be moving fast enough to require the use of interrupts... (If so, you probably have other problems... ;))

In my opinion the interrupt process would be easier to follow if the program is to be modified in the future.

Interrupts aren't only for things that need to happen quickly.

...R

Robin2: Interrupts aren't only for things that need to happen quickly.

In my opinion interrupts should only be used when necessary, because of the extra design complexity they introduce. If you needed to respond to an input event very frequently or with very low latency then they might be necessary - otherwise, I just don't see the need.