Society of Robots - Robot Forum

Software => Software => Topic started by: galannthegreat on June 07, 2009, 06:49:50 PM

Title: How do I setup an interrupt for this?
Post by: galannthegreat on June 07, 2009, 06:49:50 PM
My friend and I are trying to figure out how to get an interrupt working with this code:

Code: [Select]
for(x=0;x<200;x++)
{
output_high(PIN_D1);
delay_us(500);
output_low(PIN_D1);
delay_us(2500);          //left most position
}

for(x=0;x<200;x++)
{
output_high(PIN_D1);
delay_us(2500);
output_low(PIN_D1);
delay_us(500);          //right most position
}

The question I have is how can I get an interrupt to work in this code? and how can I connect the sensor/switch to work as the interrupt?

Is there a better method to accomplish this?
Title: Re: How do I setup an interrupt for this?
Post by: sonictj on June 08, 2009, 02:12:03 AM
Can you elaborate on what type of interrupt you want and what the code exactly is doing?
Title: Re: How do I setup an interrupt for this?
Post by: galannthegreat on June 08, 2009, 10:31:08 AM
Sorry for not clarifying this initially.

Okay, so my code is essentially making a servo scan back and forth to its most outer limits, and I want to set up an interrupt for an IR Rangefinder or a PING type sensor. I want the interrupt to be able to stop the servo movement and hold position until nothing is seen by the sensor.
Title: Re: How do I setup an interrupt for this?
Post by: sonictj on June 08, 2009, 11:16:45 AM
I did the same thing on my robot using a stepper motor, but an interrupt isn't the answer.  Interrupts should only be used for very quick tasks.  You want the program to sit and wait for a value.  The way to do this is to declare a bool global variable.  Make a servo function that moves one degree at a time if nothing is in view.

psuedo code
Code: [Select]

void servo()
{
if (sensor sees object) return;
else if (sensor sees nothing)
{
//change servo direction if servo encounters either extreme
 if ((servo_position == max_position) || servo_position == min_position) servo_dir =~ servo_dir;

  move servo 1 degree in the servo_dir direction
}