ROBOTC.net forums
http://www.robotc.net/forums/

Why doesnt this code work?
http://www.robotc.net/forums/viewtopic.php?f=53&t=2670
Page 1 of 1

Author:  boxr007 [ Tue Oct 19, 2010 6:54 pm ]
Post subject:  Why doesnt this code work?

Ok, so im just about starting to write some test programs, just to get more confident with the NXT system, and this one program, doesnt compile....why??

Code:
task main ()
{
  int distance_in_cm = 10;
 
  {
  while(true)
  motor(motorB) = -35;
  }
if(SensorValue(sensorSONAR)>distance_in_cm)
{
  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=360;
  motor(motorB)=-30;
}
else
{
   motor(motorB) = -35;
}
}


It has an error next to the if statement.
thanks guys.

Author:  Gary Samad [ Tue Oct 19, 2010 7:41 pm ]
Post subject:  Re: Why doesnt this code work?

boxr007 wrote:
Ok, so im just about starting to write some test programs, just to get more confident with the NXT system, and this one program, doesnt compile....why??

Code:
task main ()
{
  int distance_in_cm = 10;
 
  {
  while(true)
  motor(motorB) = -35;
  }
if(SensorValue(sensorSONAR)>distance_in_cm)
{
  nMotorEncoder[motorB] = 0;
  nMotorEncoderTarget[motorB]=360;
  motor(motorB)=-30;
}
else
{
   motor(motorB) = -35;
}
}


It has an error next to the if statement.
thanks guys.


The problem is with the braces near the while() statement. It should look like this:

Code:
  while(true)
  {
    motor(motorB) = -35;
  }

Author:  jbflot [ Tue Oct 19, 2010 9:48 pm ]
Post subject:  Re: Why doesnt this code work?

The reason it doesn't compile is the condition in the if statement "SensorValue(sensorSONAR)>distance_in_cm".

"sensorSonar" is a sensor type in ROBOTC, that allows you to specify which port a sonar sensor is connected. It's not an instance of a sonar sensor that you can use in a conditional comparison. Sensors in ROBOTC must first be configured using the Motors and Sensors Setup menu.

Check out this sample code that uses the sonar sensor:

Code:
#pragma config(Sensor, S4,     sonic,         sensorSONAR)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
  int distance_in_cm = 20;   // Create variable 'distance_in_cm' and initialize it to 20(cm).

   while(SensorValue[sonic] > distance_in_cm)   // While the Sonar Sensor readings are less than the specified, 'distance_in_cm':
   {
      motor[motorB] = 75;                     // Motor B is run at a 75 power level
      motor[motorC] = 75;                     // Motor C is run at a 75 power level
   }
   motor[motorB] = 0;    // Motor B is stopped at a 0 power level
   motor[motorC] = 0;    // Motor C is stopped at a 0 power level
}

Author:  boxr007 [ Wed Oct 20, 2010 4:56 pm ]
Post subject:  Re: Why doesnt this code work?

Oh yeah, I have just assigned the sensors, and it works!! :)

Thanks loads.

Author:  boxr007 [ Wed Oct 20, 2010 4:59 pm ]
Post subject:  Re: Why doesnt this code work?

Oh, and it compiles either way the brackets are near the while statement. Thanks anyway though :)

Author:  Gary Samad [ Wed Oct 20, 2010 5:55 pm ]
Post subject:  Re: Why doesnt this code work?

boxr007 wrote:
Oh, and it compiles either way the brackets are near the while statement. Thanks anyway though :)


Code:
  {
  while(true)
  motor(motorB) = -35;
  }

Well, ok, I guess I see that it could compile, but that is some bizarre syntax. Also, it looks to me like it would simply run an infinite loop setting the motor speed to -35 forever. It would never get past this to the "if" statement...

cu,
Gary

Author:  boxr007 [ Sat Oct 23, 2010 7:00 am ]
Post subject:  Re: Why doesnt this code work?

I know...unfortunately, my programming skills are somewhat, shall we say, basic :oops: lol. I am learning tho! lol.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/