Registered Users          New User?
Picture of James Koomson
Interrupt
by James Koomson - Tuesday, December 1, 2009, 09:12 AM
  Hi, I need help adding a interrupt to my program. The robot is suppose to go in the shape of a 5 in the length's of 2, 3, 2,3,2 feet which I did in my code. I have a switch at the front of robot that suppose to stop it's pattern and reverse to 2 feet. Can anybody help me with this...where is the code

#include "mxapi.h"

int forward[2] = {2000,1000};
int spin_right[2] = {2000,2000};
int reverse[2] = {1000,2000};
int spin_left[2] = {1000,1000};
int stop[2] = {1500,1500};

void move(int count, int pulsewidth[])
{
int i;
for(i=0; i<=count; i++)
{

RB3 = 1;
delay_us(pulsewidth[0]);
RB3 = 0;
RB0 = 1;
delay_us(pulsewidth[1]);
RB0 = 0;
delay_ms(20);
}
}


void main(void)
{
int i; //Declare a variable called i
TRISB3 = 0;
TRISB0 = 0;
RB3 = 0;
RB0 = 0;
TRISB7 = 1; //Set up Port B7 as an input
move(140, reverse);
while(1==1)


{

if (RC2 == 0)

if(RB7==1) //If Port B7 is high

{

move(100,reverse); //Stop moving

}

else if(RB7==0) //If Port B7 is low

{move(1,forward); //Move forward 1

}
{

for(i=0; i<176; i++) //Forward 24 inches
{
RB3 = 1;
delay_us(1286);
RB3 = 0;
RB0 = 1;
delay_us(1655);
RB0 = 0;
delay_ms(20);
}
for(i=0; i<35; i++) //Left turn 90degrees
{
RB3 = 1;
delay_us(1000);
RB3 = 0;
RB0 = 1;
delay_us(1000);
RB0 = 0;
delay_ms(20);
}
for(i=0; i<216; i++) //Forward 36 inches
{
RB3 = 1;
delay_us(1223);
RB3 = 0;
RB0 = 1;
delay_us(1638);
RB0 = 0;
delay_ms(20);
}
for(i=0; i<35; i++) //Left turn 90 degrees
{
RB3 = 1;
delay_us(1000);
RB3 = 0;
RB0 = 1;
delay_us(1100);
RB0 = 0;
delay_ms(20);
}
for(i=0; i<145; i++) //Forward 24 inches
{
RB3 = 1;
delay_us(1293);
RB3 = 0;
RB0 = 1;
delay_us(1625);
RB0 = 0;
delay_ms(20);
}
for(i=0; i<34; i++) //Right turn 90 degrees
{
RB3 = 1;
delay_us(2000);
RB3 = 0;
RB0 = 1;
delay_us(2000);
RB0 = 0;
delay_ms(20);
}
for(i=0; i<210; i++) //Forward 36 inches
{
RB3 = 1;
delay_us(1260);
RB3 = 0;
RB0 = 1;
delay_us(1643);
RB0 = 0;
delay_ms(20);
}
for(i=0; i<35; i++) //Right turn 90 degrees
{
RB3 = 1;
delay_us(2000);
RB3 = 0;
RB0 = 1;
delay_us(2000);
RB0 = 0;
delay_ms(20);
}
for(i=0; i<171; i++) //Forward 24 inches
{
RB3 = 1;
delay_us(1301);
RB3 = 0;
RB0 = 1;
delay_us(1648);
RB0 = 0;
delay_ms(20);

}
}
end();
}
}
Me
Re: Interrupt
by Ivan Rudnicki - Wednesday, December 2, 2009, 07:28 AM
  A couple of observations about your code.

1) You set up the move function, but then you don't use it consistently. Instead of using a for loop to make each movement, you could have much simpler code. For example, instead of the for loop marked with the comment "Forward 24 inches," you could add an array for those pulse durations (1286 and 1655), together with the rest of the arrays:

int slow_forward[2]={1286, 1655};

Then, you can pass this array to the move function, together with the number of times that you want the movement to repeat(176):

move(176, slow_forward);

2) I'm not sure that I understand exactly what you want your robot to do, but I think that you should make all these moves before you start the while loop that contains the if-else statements for your sensors. That way, the robot will complete the pattern on the floor and then start monitoring the sensors.