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

PLEASE HELP! Programming format thing.
http://robotc.net/forums/viewtopic.php?f=69&t=10890
Page 1 of 1

Author:  Skittelzittel [ Thu Apr 09, 2015 2:25 pm ]
Post subject:  PLEASE HELP! Programming format thing.

Hi, so i had to design and create a program to sort different marbles. There isn't anything wrong with the program i don't think but the format or the syntax is messed up somewhere. My issue is when it hits the first IF statement group, when it doesn't meet the requirements I want it to move on to the second IF IF ELSE statement group but if just loops back up to the base code setup. Any tips?

Here's the Code.




task main()
{
repeat(forever)
{
turnFlashlightOn(flashlight, 127);
setServo(servo2, 60);
setServo(servo3, -10);
waitInMilliseconds(2000);
setServo(servo3, 30);
waitInMilliseconds(190);
{
if (SensorValue[lightsensor] >500)
{
if (SensorValue[lightsensor] <700)
{setServo(servo, -90);
waitInMilliseconds(500);
setServo(servo, -10);}
else
{
setServo(servo, 90);
waitInMilliseconds(500);
setServo(servo, -10);
}
{
if (SensorValue[linefollower] >500)
{
if (SensorValue[linefollower] <900)
{
setServo(servo2, 120);
waitInMilliseconds(500);
setServo(servo2, 60);}
else
{
setServo(servo2, -90);
waitInMilliseconds(500);
setServo(servo2, 60);
}
}
}
}
}
}
}

Author:  Tabor473 [ Tue May 19, 2015 4:30 pm ]
Post subject:  Re: PLEASE HELP! Programming format thing.

So first I formatted the code so that its easy to understand what is happening.
Code:
task main()
{
   repeat(forever)
   {
      turnFlashlightOn(flashlight, 127);
      setServo(servo2, 60);
      setServo(servo3, -10);
      waitInMilliseconds(2000);
      setServo(servo3, 30);
      waitInMilliseconds(190);
      {
         if (SensorValue[lightsensor] >500)
         {
            if (SensorValue[lightsensor] <700)
            {setServo(servo, -90);
               waitInMilliseconds(500);
               setServo(servo, -10);}
            else
            {
               setServo(servo, 90);
               waitInMilliseconds(500);
               setServo(servo, -10);
            }
            {
               if (SensorValue[linefollower] >500)
               {
                  if (SensorValue[linefollower] <900)
                  {
                     setServo(servo2, 120);
                     waitInMilliseconds(500);
                     setServo(servo2, 60);}
                  else
                  {
                     setServo(servo2, -90);
                     waitInMilliseconds(500);
                     setServo(servo2, 60);
                  }
               }
            }
         }
      }
   }
}


The first if statement opens a bracket that never closes until the end of the program. This means all the code contained within the brackets will only run if the set of criteria is true in the if statement.


I am not certain exactly what you are trying to do as part of each conditional statement but it would appear that you want both if statements to close here rather than just one of them close.

Code:
if (SensorValue[lightsensor] >500)
         {
            if (SensorValue[lightsensor] <700)
            {setServo(servo, -90);
               waitInMilliseconds(500);
               setServo(servo, -10);}


The same would be true for
Code:
if (SensorValue[linefollower] >500)
               {
                  if (SensorValue[linefollower] <900)
                  {
                     setServo(servo2, 120);
                     waitInMilliseconds(500);
                     setServo(servo2, 60);}



Right now the compiler doesn't know when you want to do each action. If you write out in words how you expect the robot to react to each set of criteria I can try to show you an example of how to write that code but for now just make sure all your if statements only contain the code that depends on if it is true.

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