Breaking out of a loop

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I am working on a mini robot project where i am trying to drive servos using the PS2 controller. I have defined the different servo positions as an array as shown below:

const uint16_t servo_clockwise[17] = {150, 178, 206, 235, 263, 291, 319, 347, 375, 404, 431, 460, 488, 516, 544, 572, 600};

These servo positions allow me to rotate the servo from 0 degrees up till 180 degrees. I check if the right joystick up position is pressed and then start rotating the servo. In the for loop i have an if condition to check if the right joystick has been released to its neutral position and if it has, then i break out of the loop as shown in the code below:

uint8_t m;
if(ps2.ry == 0x00) {	//if right Joystick pressed up
   for(m = 0; m < 17; m++) {
	OCR1A = servo_clockwise[m];
	delay1s();
	if(ps2.ry != 0x00) {
           break;
	}
   }
   OCR1A = servo_clockwise[m];
}

But the break statement doesn't take me out of the loop, and the servo continues rotating till it completes 180 degrees. Not able to figure out why the break doesn't exit the for loop. Need help with this.

Thanks,
Sumair

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd start by correcting your indentation so you get a better picture of the nesting:

if(ps2.ry == 0x00) {   //if right Joystick pressed up
   for(m = 0; m < 17; m++) {
     OCR1A = servo_clockwise[m];
     delay1s();
     if(ps2.ry != 0x00) {
           break;
     }
   }
   OCR1A = servo_clockwise[m];
} 

If the if() condition were met then there's no doubt that break; would get you out of the for() loop but think about what you are doing here.

First you do:

if(ps2.ry == 0x00) {

so you only get into the for() loop in the first place if that variable is 0x00.

Then you do:

   if(ps2.ry != 0x00) {
           break; 

but how can ps2.ry update between these two lines. It was 0x00 so how can it have changed?

The only way I can think is if you have an interrupt ISR() that might "fire" while this code is running and change that value. If you are relying on this happening is the variable "volatile"?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Clawson,

thanks for your reply...now i get it. Once the right joystick is released to its neutral position it does not enter the for loop...hence, it cannot even check for the if condition inside the for loop.

I want to stop the servo rotation once the joystick is released back but haven't figured out how to do this using interrupts. Is there any other way i can stop the servo rotation mid-way without sing interrupts?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Is there any other way i can stop the servo rotation mid-way without sing interrupts?

Interrupts - perhaps a timer - could be the easiest solution but failing that why not add the code that polls the hardware to set ps2.ry within the loop?

If you then find you are doing the same thing in two places consider putting the code into a function that is called from both places.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Clawson,

I added the hardware polling code in a function and called it inside the for loop to update the ps2.ry parameter and it worked like a charm. Please see the code below:

uint8_t m;
if(ps2.ry == 0x00) {	//if right Joystick pressed up
  for(m = 0; m < 17; m++) {
     OCR1A = servo_clockwise[m];
     delay1s();			
     ps2_poll(ps2.pressure[5],PS2_MOTOR_OFF);
     if(ps2.ry != 0x00) {
        break;
     }
  }
  OCR1A = servo_clockwise[m];
}

Thanks for your advice.

Sumair