i need help creating a if then program

3 replies [Last post]
qewrty
Title: NooBot
Joined: 03/06/2012
Posts:
BotPoints: 5
User offline. Last seen 10 years 29 weeks ago.

i am trying to create a program where the light sensor starts another program. however when i try to do this i keep on getting errors. this is what i have so far. just trying to move the motor.

  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. wait_for_light(0);
  8. if (analog10(0) >30);
  9. enable_motors(0,3);
  10. mtp(0,1000);
  11. mtp(3,1000);
  12. }

ruler501
ruler501's picture
Title: NooBot
Joined: 01/29/2012
Posts:
BotPoints: 367
User offline. Last seen 7 years 20 weeks ago.

First if you have a #include with nothing after it the program shouldnt be able to compile
and if you just want to wait for the light you can remove the if(analog10(0) > 30); as how you wrote it it wont do anything and wait_for_light will already wait for the light to be turned on after calibration is complete. I may be wrong but I do not believe that you have to do enable_motors. Also at the end you need to sleep to allow the motors to finish moving you do this with bmd(motor). The final thing though is more of a personal choice and that is to use mrp instead of mtp. Here is the code I would use instead:

//define the light sensor so its a little more readable
#define LIGHT 0

using namespace std;

int main()
{
wait_for_light(LIGHT);
mrp(0,500, 1000);
mrp(3,500, 1000);
bmd(0);
bmd(3);
}

This will wait for the light then move forward 1000 ticks at 500 ticks a second(unless i got the last two arguments backwards)

Beta
Beta's picture
Title: The Magnificent
Joined: 02/24/2012
Posts:
BotPoints: 266
User offline. Last seen 8 years 32 weeks ago.

The reason the include wasn't showing up was because of html weirdness. I went ahead and encapsulated your code in <c> blocks to prevent that from happening.

I think the error is caused by the lack of definition of the  enable_motors(0, 3); function.

ruler501 is correct about the if not functioning as expected. Remember that an if is executed only once. It does not wait for a condition to be true to continue.

I would recommend using a while loop to wait for a condition to be true. You can find examples in KISS's built in documentation.
Or, if this is a competition robot, it is strongly recommended you use the wait_for_light(int port); function. This allows you to calibrate your robot for the possibly stronger or weaker lighting at the tournament.

Good Luck!

Braden McDorman

Developer of the KIPR Link, KISS IDE, KIPR's 2D Simulator, and CBCJVM.

Reach me at bmcdorman(cat)kipr(dog)org where (cat)=@ and (dog)=. if you need assistance of any kind.

qewrty
Title: NooBot
Joined: 03/06/2012
Posts:
BotPoints: 5
User offline. Last seen 10 years 29 weeks ago.

thanks every thing now works great.