Having issues with code

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
fortyeightplus
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Post by fortyeightplus »

Code: Select all

task main (){

#define NEAR 15 // cm
   SetSensorLowspeed(IN_1);
   while(true){
      OnFwd(OUT_AC,75);
      while(SensorUS(IN_1)>NEAR);
      Off(OUT_AC);
      OnRev(OUT_AC,100);
}

#define THRESHOLD 29
     SetSensorLight(IN_2);
     OnFwd(OUT_AC,75);
     while(true){
        if (Sensor(IN_2) > THRESHOLD){
          OnRev(OUT_C, 75);
          Wait(100);
          until(Sensor(IN_2) <= THRESHOLD);
          OnFwd(OUT_AC, 75);
}

#define THRESHOLD 29
   SetSensorLight(IN_3);
     OnFwd(OUT_AC,75);
     while(true){
        if (Sensor(IN_2) > THRESHOLD){
          OnRev(OUT_C, 75);
          Wait(100);
          until(Sensor(IN_3) <= THRESHOLD);
          OnFwd(OUT_AC, 75);
       }
     }
   }
}
What if I do this? I get no error messages, although I don't have the NXT brick or the robot near me, so I can't try it out. But do you think it looks good? 'Casue what I want the robot to do is to stay within a one square meter radius, or 14ft2 approximatley and (if possible) when it sees another robot (with help of the ultrasonic sensor) it's supposed to "rush" forwards to the robot, have I done the program correctly? If not, what's wrong? Thanks in advance :)
totokan
Posts: 45
Joined: 28 Sep 2013, 04:22

Re: Having issues with code

Post by totokan »

A few coding pointers that perhaps might help you.
#DEFINE should go at the top of your program, outside of any functions or tasks or anything. It is a preprocessor directive, and it tells the compiler (the program that makes your code into something the NXT can run) to replace every following instance of the first argument (the first thing following the #DEFINE) with the second argument. If you later call #DEFINE on the same argument, it will possibly do strange things, so avoid that. So, at the top of your program, if we move what you have around, it will look like this:

Code: Select all

#define NEAR 15 // cm
#define THRESHOLD 29
#define THRESHOLD 29

task main (){
We can remove the second definition of threshold or, if we want two separate thresholds, re-name them to THRESHOLD_L and THRESHOLD_R, for example.

Secondly, your code does this several times:

Code: Select all

while(true){
  //some kind of code
}
This creates an infinite loop and when you execute the program you will never get past it. You either want to add a condition that will cause the loop to end (E.G. if a sensor has a specific value) in place of the TRUE or add an if(something) break; to the inside of the loop.
fortyeightplus
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Post by fortyeightplus »

sorry, double post
Last edited by fortyeightplus on 13 Nov 2013, 11:46, edited 1 time in total.
fortyeightplus
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Post by fortyeightplus »

totokan wrote:A few coding pointers that perhaps might help you.
#DEFINE should go at the top of your program, outside of any functions or tasks or anything. It is a preprocessor directive, and it tells the compiler (the program that makes your code into something the NXT can run) to replace every following instance of the first argument (the first thing following the #DEFINE) with the second argument. If you later call #DEFINE on the same argument, it will possibly do strange things, so avoid that. So, at the top of your program, if we move what you have around, it will look like this:

Code: Select all

#define NEAR 15 // cm
#define THRESHOLD 29
#define THRESHOLD 29

task main (){
We can remove the second definition of threshold or, if we want two separate thresholds, re-name them to THRESHOLD_L and THRESHOLD_R, for example.

Secondly, your code does this several times:

Code: Select all

while(true){
  //some kind of code
}
This creates an infinite loop and when you execute the program you will never get past it. You either want to add a condition that will cause the loop to end (E.G. if a sensor has a specific value) in place of the TRUE or add an if(something) break; to the inside of the loop.
Could you give me an example in code so I could try to use it some how?

While(true){ creates a loop but right now I can't think of another solution, what do you mean by if(something)?
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Having issues with code

Post by HaWe »

if you want help about certain API function you can type them into your BCC editor and press [F1].
Immediately you will get help about this topic.

So if you type
if
and press F1 you will get this:
The if statement
The if statement evaluates a condition.

If the condition is true, it executes one statement (the consequence). The value of a condition is considered to be false only when it evaluates to zero. If it evaluates to any non-zero value, it is true. The syntax for an if statement is shown below.

if (condition) consequence
The condition of an if-statement must be enclosed in parentheses, as shown in the code sample below. The compound statement in the last example allows two statements to execute as a consequence of the condition being true.
the same procedure about the command
while:
The while statement
The while statement is used to construct a conditional loop.

The condition is evaluated, and if true the body of the loop is executed, then the condition is tested again. This process continues until the condition becomes false (or a break statement is executed). The syntax for a while loop appears in the code fragment below.

while (condition) body
Because the body of a while statement must be a single statement, it is very common to use a compound statement as the body. The sample below illustrates this usage pattern.
As it is about a schoolwork we must not write code for you. But we may give you one hint or the other, and you should write the new code accordingly and test it by yourself and figure out how it works.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 14 guests