Society of Robots - Robot Forum

Software => Software => Topic started by: SciOlyStudent on February 13, 2008, 09:46:58 AM

Title: Need a code example
Post by: SciOlyStudent on February 13, 2008, 09:46:58 AM
Hello again, all of you who are slapping your heads in my ignorance,

I am finally able to program my robot and have now hit yet another wall.  I need a bit of code to make the robot go forward for an amount of time and then stop.  It should be simple, but the solution has yet to smack me over the head as it so often does... could anyone offer an example of the code I need? (I'm using the variables from the 50$ robot, servo_left, servo_right, delay_cycles)
Title: Re: Need a code example
Post by: SciOlyStudent on February 13, 2008, 02:40:24 PM
   while(1)
      {
      delay_cycles(50400);
      int i=i+1;
      }
   while(i<5)
      {
      servo_left(25);
      servo_right(44);
      }
   while(i>5)
      {
      servo_left(0);
      servo_right(0);
      }

this is what I have so far, but it only drives straight regardless of how long I  leave it on.  What am I missing?
Title: Re: Need a code example
Post by: cooldog on February 13, 2008, 07:26:26 PM
you will need encoders to tell it when to stop
Title: Re: Need a code example
Post by: SciOlyStudent on February 14, 2008, 07:28:05 AM
you will need encoders to tell it when to stop

So there is no way to make the robot go forward and then stop after a certain amount of time?  I'm not talking accuracy right now, I'm just trying to figure out how to make it go forward and then stop.
Title: Re: Need a code example
Post by: superchiku on February 14, 2008, 10:41:37 AM
1st of all i suggest build in ur logic well ur code doesnt make sense

if u want the robot to do wat u want u have to make a good program and the codings should be very flexible try making other logics and those may solve ur problem
and by the way have u done programming bfore ?
Title: Re: Need a code example
Post by: Steck72255 on February 14, 2008, 11:45:17 AM
The first while loop that you created is an infinite loop, so the program never exits this loop.  The other two while loops are never touched, and since you defined i with in the first loop they wouldn't work anyway.  In order for your code block to make sense you would be better off doing something like this:

int i = 0;
while (1)
{     
      if (i < 5)
      {           
            servo_left(25);
            servo_right(44);
       }
       else
       {
            servo_left(0);
            servo_right(0);
       }

      delay_cycles(50400);
      i =i+1;
}

I am still new to robotics programming so this may not solve your problem entirely, but hopefully it does  ;D

Hope this was helpful.
Title: Re: Need a code example
Post by: SciOlyStudent on February 14, 2008, 02:34:50 PM
Ok, I'll give it a try, thanks a lot Steck!
Title: Re: Need a code example
Post by: Admin on February 17, 2008, 10:06:32 AM
While Steck72255 was right, his code won't entirely work either. The reason is because your microcontroller will go through that code crazy fast. Probably ~50 times per second.

That means the if (i < 5) part will run for an insignificant amount of time.

You want something more like this:
Code: [Select]
long int i = 0;
while (1)
{     
      if (i < 5000)
      {           
            servo_left(25);
            servo_right(44);
            delay_cycles(200);
       }
       else
       {
            servo_left(44);
            servo_right(25);
            delay_cycles(200);
       }

      delay_cycles(50400);
      i =i+1;
}