Sample Mission NQC Code

From Science Olympiad Student Center Event Wiki

Articles in this series

Main Article
Descriptions of how to accomplish specific historic tasks and bonuses
A guide to programming with Lego Mindstorms and NXT
DarkSabre's 2005 ETL, illustrated with pictures and comments
The code for DarkSabre's 2005 MPC
DarkSabre's 2004 ETL, illustrated with pictures and comments
Information about acquiring and using Nichrome Wire
Diagrams, information, and uses for relays
Examples of Closed System Hydraulics
Examples of Photoresistor Use

The code

int RCX=2;      // Change this according to which RCX is being programmed
//sensor triggering variables
int ball1=999; //480;  //bernoulli ball present   **triggers when higher**
int precip1=999; //408; //precipitate present     **triggers when higher**
int magFO1=440; //electromag released FO are blocked   **triggers when lower**
int heattouch=500; //bimetallic has pressed the sensor **triggers when lower**
int chemLED1=0; //chemical battery LEDs are on   **triggers when lower**
int sound1=200; //piezo detection     **triggers when lower**
int lacetouch=500; //lace tying trigger  **triggers when lower**
//motor timing variables
int motorswitcher=300;
int springsmotor=300;
int chembatterymotor=0;
int ballthrowermotor=1000;
int pneumaticmotor=100;
int precipmotor=0;
int laceknotmotor=2600;
int laceknotmotor2=600;
//special ops variables
int bernoulli=0;
int transformer=0;
int aa=0;
int transformer2=0; //Rapid checker Averager **triggers when lower**
int thejudgefriendlywaitcommand=300;
// true/false transfer markers
int i=0;
int j=0;
int k=0;
int r=0;
task main()
{
     SetPower(OUT_A && OUT_B && OUT_C, OUT_FULL);
     SetDirection(OUT_A && OUT_B && OUT_C, OUT_FWD); //cords need to go up!
     SetSensorType(SENSOR_1, SENSOR_TYPE_NONE); //tells the RCX that the sensors are generic
     SetSensorType(SENSOR_2, SENSOR_TYPE_NONE);
     SetSensorType(SENSOR_3, SENSOR_TYPE_NONE);
     SetSensorMode(SENSOR_1, SENSOR_MODE_RAW); //reads sensors in raw data form
     SetSensorMode(SENSOR_2, SENSOR_MODE_RAW);
     SetSensorMode(SENSOR_3, SENSOR_MODE_RAW);
     start Identify;
     if (RCX==1)start RCX1;
     if (RCX==2)start RCX2;
     if (RCX==3)start RCX3;
     if (RCX==4)start RCX4;
}
task RCX1()
{
     while(true)
     {
           if(SENSOR_1<=magFO1 && i==0)     //electromagnet drops ball and FO is blocked
           {
              repeat(50)
              {
                aa=(SENSOR_2+aa);      //sums the ambient readings and stores them as "aa"
                Wait(1);
              }
              precip1=((aa/50)+5);    //finds the average of the ambient conditions
              ClearTimer(1);      //timer for resetting the precip dumper
              On(OUT_A);          //precip dumper
              until(SENSOR_2>=precip1)
              precipmotor=(Timer(1)*10); //sets the reset time in the correct units
              Off(OUT_A);
              i=1;
           }
           if(SENSOR_2>=precip1 && j==0)    //precipitate is present
           {
              repeat(50)
              {
                aa=(SENSOR_3+aa);      //sums ambient readings and stores them as "aa"
                Wait(1);
              }
              ball1=((aa/50)+20);    //finds the average of the ambient conditions
              PlayTone(800,100);
              Wait(thejudgefriendlywaitcommand);
              OnFor(OUT_B,100);             //pneumatic switch
              j=1;
              Float(OUT_B);         //Float puts motors into "neutral"
           }
           if(SENSOR_3>=ball1 && k==0)        //bernoulli ball is present
           {
              while(bernoulli==0)
              {
                ClearTimer(0);
                until(Timer(0)==2||SENSOR_3<=ball1)   //the ball must stay for 2 seconds
                if(SENSOR_3>=ball1) bernoulli=1;
              }
              PlayTone(800,100);
              Wait(thejudgefriendlywaitcommand);
              OnFor(OUT_C, springsmotor);               //springs motor (release and store)
              k=1;
              Float(OUT_C);
           }
           if(i==1 && j==1 && k==1 && r==0)        //resetting code
           {
              Toggle(OUT_A);  //switches motor direction
              OnFor(OUT_A,precipmotor);        //precip
              r=1;
           }
     }
}
task RCX2()
{
     while(true)
     {
           if(SENSOR_1<=sound1 && i==0)  //piezoelectric microphone
           {
              repeat(50)
              {
                aa=(SENSOR_2+aa);         //averager of ambient conditions
                Wait(1);
              }
              transformer2=(aa/50);
              PlayTone(800,100);
              Wait(thejudgefriendlywaitcommand);
              OnFor(OUT_A, motorswitcher);     //completes transformer circuit
              Float(OUT_A);
              i=1;
           }
           ClearTimer(1);    //for transformer hits counting
           while(i==1 && j==0)  //transformer LEDs
           {
              if(SENSOR_2<=transformer2) transformer=transformer+1;
              if(transformer>=5 && Timer(1)<=3)   //triggers if it gets enough hits in the given time
              {
                 aa=0;
                 repeat(50)
                 {
                   aa=(SENSOR_3+aa);     //averager
                   Wait(1);
                 }
                 chemLED1=((aa/50)-1);
                 ClearTimer(0);      //timer for chemical battery resetting
                 On(OUT_B);  //completes chemical battery
                 until(SENSOR_3<=chemLED1)//lowers until immersed in vinegar
                 Wait(100);
                 until(SENSOR_3<=chemLED1)   //makes sure it is in vinegar
                 Off(OUT_B);
                 chembatterymotor=(Timer(0)*10);  //sets reset time
                 Float(OUT_B);
                 j=1;
              }
              if(transformer<=5 && Timer(1)>=3)  //if it doesn't get enough hits in the given time
              {
              ClearTimer(1);      //start over
              transformer=0;
              }
              Wait(1);
           }
           if(SENSOR_3<=chemLED1 && k==0)      //chemical battery LED
           {
              OnFor(OUT_C, ballthrowermotor);  //launches ball
              Float(OUT_C);
              k=1;
           }
           if(i==1 && j==1 && k==1 && r==0)    //resetting code
           {
              Toggle(OUT_B);
              OnFor(OUT_B, chembatterymotor);   //resets chemical battery
              r=1;
           }
     }
}
task RCX3()
{
     while(true)
     {
           if(SENSOR_1<=heattouch && i==0)   //increasing/decreasing heat
           {
              PlayTone(800,100);
              OnFor(OUT_A, motorswitcher);    //turns NiCr off
              Wait(100);
              until(SENSOR_1>=heattouch);      //until it is released
              PlayTone(800,100);
              OnFor(OUT_C, motorswitcher);   //completes buzzer circuit
              On OUT_B;                      //rattle
              Wait(30);
              repeat (40)
              {
                 Toggle OUT_B;        //shakes rattle for 12.3 seconds total
                 Wait(30);
              }
              Off(OUT_B);
              i=1;
              Float(OUT_A && OUT_B && OUT_C);
           }
     }
}
task RCX4()
{
     while(true)
     {
           if(SENSOR_1<=lacetouch && i==0)   //lace tying trigger
           {
              Toggle(OUT_A);
              Toggle(OUT_B);
              OnFor(OUT_A, laceknotmotor);  //intial pulling
              OnFor(OUT_B, pneumaticmotor); //removes the pin
              OnFor(OUT_A, laceknotmotor2); //finishes the knot
              i=1;
              Float(OUT_A && OUT_B);
           }
     }
}
task Identify()
{
     Wait(100);
     repeat(RCX)
     {
           PlayTone(800,20);
           Wait(35);
     }
}