Author Topic: Need a code example  (Read 2422 times)

0 Members and 1 Guest are viewing this topic.

Offline SciOlyStudentTopic starter

  • Full Member
  • ***
  • Posts: 67
  • Helpful? 0
Need a code example
« 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)

Offline SciOlyStudentTopic starter

  • Full Member
  • ***
  • Posts: 67
  • Helpful? 0
Re: Need a code example
« Reply #1 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?

Offline cooldog

  • Supreme Robot
  • *****
  • Posts: 751
  • Helpful? 4
  • be nice to nerds, one day they will be your boss
Re: Need a code example
« Reply #2 on: February 13, 2008, 07:26:26 PM »
you will need encoders to tell it when to stop
robot will rule the world and i will be building them
-admin

favorite web sites
http://www.societyofrobots.com/
http://www.instructables.com/

Offline SciOlyStudentTopic starter

  • Full Member
  • ***
  • Posts: 67
  • Helpful? 0
Re: Need a code example
« Reply #3 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.

Offline superchiku

  • Supreme Robot
  • *****
  • Posts: 953
  • Helpful? 5
  • cooll
Re: Need a code example
« Reply #4 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 ?
JAYDEEP ...

IT AND ROBOTICS ENGINEER

"IN THE END IT DOESNT EVEN MATTER"

Offline Steck72255

  • Beginner
  • *
  • Posts: 2
  • Helpful? 0
Re: Need a code example
« Reply #5 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.

Offline SciOlyStudentTopic starter

  • Full Member
  • ***
  • Posts: 67
  • Helpful? 0
Re: Need a code example
« Reply #6 on: February 14, 2008, 02:34:50 PM »
Ok, I'll give it a try, thanks a lot Steck!

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,666
  • Helpful? 169
    • Society of Robots
Re: Need a code example
« Reply #7 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;
}

 


Get Your Ad Here

data_list