View unanswered posts | View active topics It is currently Fri Nov 22, 2019 11:00 am






Reply to topic  [ 3 posts ] 
Problem with this code. 
Author Message
Rookie

Joined: Tue Nov 10, 2015 11:53 pm
Posts: 2
Post Problem with this code.
For some reason my robot will only turn fast, when I have two parts of code for it to turn slow or fast depending on which direction it is facing. I'm probably missing something obvious, but I can't work out what D:

void turnRight() //RIGHT FUNCTION --------------------------------------------------------------------------------------------------------------------------------
{
while(SensorValue(compass) < 210 || SensorValue(compass) > 230)
{
if(SensorValue(IR) > 6)
{
nxtDisplayStringAt(3, 10, "<<6");
if(SensorValue(compass) <= 100 || SensorValue(compass) >= 340)
{
motor[motorB] = 55;
motor[motorC] = -45;
}
else if(SensorValue(compass) > 100 || SensorValue(compass) < 340)
{
motor[motorB] = 70;
motor[motorC] = -60;
}
}
else if(SensorValue(IR) == 6)
{
nxtDisplayStringAt(3, 10, ">=6");
motor[motorB] = 30;
motor[motorC] = 30;
}
}
forward();
}

Any help appreciated!


Tue Nov 17, 2015 11:10 pm
Profile
Rookie

Joined: Tue Nov 10, 2015 11:47 pm
Posts: 4
Post Re: Problem with this code.
Hey, I believe the problem may be stemming from your first Else If statement.

Try writing it like:
Code:
else if(SensorValue(compass) <= 100 && SensorValue(compass) >= 340)


It seems that if you have it as an "or" it will run for pretty much everything.

Hopefully that helps :D


Tue Nov 17, 2015 11:17 pm
Profile
Rookie

Joined: Tue Nov 17, 2015 11:15 pm
Posts: 1
Post Re: Problem with this code.
Hello there, :D

After experimenting with your code a little, I have found that the issue is rooted within the leftTurn() and rightTurn() functions. :oops: In both functions, in the first "else if" statement there should be an "&&" inplace of a "||" :mrgreen: :lol: :mrgreen: :lol: :mrgreen: :lol: :biggrin: :bigthumb:

Code:
void turnLeft()
{
    while(SensorValue(compass) < 210 || SensorValue(compass) > 230)
    {
         if(SensorValue(IR) < 4 && SensorValue(IR) > 0)
       {
            if(SensorValue(compass) <= 100 || SensorValue(compass) >= 340)
            {
               motor[motorB] = -45;
             motor[motorC] = 55;
            }
           else if(SensorValue(compass) > 100 && SensorValue(compass) < 340)
          {
             motor[motorB] = -60;
             motor[motorC] = 70;
          }
       }
      else if(SensorValue(IR) == 4)
       {
            motor[motorB] = 30;
           motor[motorC] = 30;
       }
    }
   forward();
}


Tue Nov 17, 2015 11:40 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 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:  
cron



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