View unanswered posts | View active topics It is currently Wed Feb 21, 2018 7:13 pm






Reply to topic  [ 2 posts ] 
Code not working help plz 
Author Message
Rookie

Joined: Tue Apr 05, 2016 3:35 pm
Posts: 1
Post Code not working help plz
Making an elevator that has three levels. We are using a variable (floorCounter) to let the code know what set of commands to look at. When going from floor 3 to floor 2, the variable changes to 1 instead of 2 as it should. The rest of the code works except this one part.
task main()
{while(1==1)
{
int floorCount;
floorCount=1;
while(floorCount==1)
{
turnLEDOn(light1);
turnLEDOff(light2);
turnLEDOff(light3);
if(SensorValue(floor2)==1)
{
startMotor(lift,-63);
wait(2.5);
stopMotor(lift);
floorCount=floorCount+1;
}
if(SensorValue(floor3)==1)
{
startMotor(lift,-63);
wait(5);
stopMotor(lift);
floorCount=floorCount+2;
}
}
while(floorCount==2)
{
turnLEDOff(light1);
turnLEDOff(light3);
turnLEDOn(light2);
if(SensorValue(floor1)==1)
{
startMotor(lift,63);
wait(2.5);
stopMotor(lift);
floorCount=floorCount-1;

}
if(SensorValue(floor3)==1)
{
startMotor(lift,-63);
wait(2.5);
stopMotor(lift);
floorCount=floorCount+1;
}
}
while(floorCount==3)
{
turnLEDOff(light1);
turnLEDOff(light2);
turnLEDOn(light3);
if(SensorValue(floor2)==1)
{
startMotor(lift,63);
wait(2.5);
stopMotor(lift);
floorCount=floorCount-1;
}
if(SensorValue(floor1)==1)
{
startMotor(lift,63);
wait(5);
stopMotor(lift);
floorCount=floorCount-2;
}
}
}
}


Tue Apr 05, 2016 3:39 pm
Profile
Moderator
Moderator

Joined: Thu Jan 03, 2013 5:10 pm
Posts: 207
Location: The plateau north of the Ohio River Valley, also known as Cave Country.
Post Re: Code not working help plz
The issue is at the start of your parent while loop. Each time the elevator moves up, the program flows seamlessly from one inner loop to the next. Whenever the elevator descends, however, the main loop is rerun, resetting floorCount to 1. This is only an issue if you're moving down to a floor other than floor 1, which only occurs when you move from F3 to F2. The solution is easy: just move the problem code outside of the loop.

Code:
task main()
{
  int floorCount = 1; // ADDED
  while(true)
  {
    // int floorCount; // REMOVED
    // floorCount=1; //// REMOVED

    while(floorCount==1)
    {
      turnLEDOn(light1);
      turnLEDOff(light2);
      turnLEDOff(light3);
      if(SensorValue(floor2)==1)
      {
        startMotor(lift,-63);
        wait(2.5);
        stopMotor(lift);
        floorCount=floorCount+1;
      }
      if(SensorValue(floor3)==1)
      {
        startMotor(lift,-63);
        wait(5);
        stopMotor(lift);
        floorCount=floorCount+2;
      }
    }

    while(floorCount==2)
    {
      turnLEDOff(light1);
      turnLEDOff(light3);
      turnLEDOn(light2);
      if(SensorValue(floor1)==1)
      {
        startMotor(lift,63);
        wait(2.5);
        stopMotor(lift);
        floorCount=floorCount-1;
      }
      if(SensorValue(floor3)==1)
      {
        startMotor(lift,-63);
        wait(2.5);
        stopMotor(lift);
        floorCount=floorCount+1;
      }
    }

    while(floorCount==3)
    {
      turnLEDOff(light1);
      turnLEDOff(light2);
      turnLEDOn(light3);
      if(SensorValue(floor2)==1)
      {
        startMotor(lift,63);
        wait(2.5);
        stopMotor(lift);
        floorCount=floorCount-1;
      }
      if(SensorValue(floor1)==1)
      {
        startMotor(lift,63);
        wait(5);
        stopMotor(lift);
        floorCount=floorCount-2;
      }
    }
  }
}

_________________
I'm not a robot! I'm british! ~ quote from an asparagus
I am not a robot! I am a unicorn! ~ quote from a robot


Tue Apr 05, 2016 4:42 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 posts ] 

Who is online

Users browsing this forum: No registered users and 1 guest


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.