View unanswered posts | View active topics It is currently Sun Dec 16, 2018 4:39 am






Reply to topic  [ 2 posts ] 
PLEASE HELP! Programming format thing. 
Author Message
Rookie

Joined: Mon Feb 23, 2015 8:56 am
Posts: 2
Post 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);
}
}
}
}
}
}
}


Thu Apr 09, 2015 2:25 pm
Profile
Moderator
Moderator

Joined: Tue May 19, 2015 3:07 pm
Posts: 91
Post 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.


Tue May 19, 2015 4:30 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 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.