View unanswered posts | View active topics It is currently Sat Aug 30, 2014 12:27 am






Reply to topic  [ 7 posts ] 
Why doesnt this code work? 
Author Message
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post 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.


Tue Oct 19, 2010 6:54 pm
Profile
Expert

Joined: Mon Mar 15, 2010 4:24 pm
Posts: 124
Post 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;
  }


Tue Oct 19, 2010 7:41 pm
Profile
Site Admin
Site Admin

Joined: Tue May 15, 2007 9:02 am
Posts: 405
Post 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
}

_________________
Jesse Flot
CMU Robotics Academy
ROBOTC Support


Tue Oct 19, 2010 9:48 pm
Profile
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post Re: Why doesnt this code work?
Oh yeah, I have just assigned the sensors, and it works!! :)

Thanks loads.


Wed Oct 20, 2010 4:56 pm
Profile
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post Re: Why doesnt this code work?
Oh, and it compiles either way the brackets are near the while statement. Thanks anyway though :)


Wed Oct 20, 2010 4:59 pm
Profile
Expert

Joined: Mon Mar 15, 2010 4:24 pm
Posts: 124
Post 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


Wed Oct 20, 2010 5:55 pm
Profile
Rookie

Joined: Fri Oct 15, 2010 2:20 pm
Posts: 17
Location: Rochdale, UK
Post 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.


Sat Oct 23, 2010 7:00 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.