Program: 6361
Project: 6361
Buggy source:
/** 
 * @param args
 */
public static void main(String[] args){
  new MyRtThread(10,1 * 100){
    public void run(){
      up=true;
      flag=false;
      value=10;
      speed=0x400;
      while (true) {
        if (state == STATE_ALL) {
          counter++;
          if ((counter % value) == 0) {
            if (flag) {
              flag=false;
            }
 else {
              flag=true;
            }
          }
          counter1++;
          if ((counter1 % speed) == 0) {
            if (speaker_up) {
              value++;
              if (value >= 50) {
                speaker_up=false;
                value--;
              }
            }
 else {
              value--;
              if (value <= 10) {
                speaker_up=true;
                value++;
              }
            }
          }
        }
 else {
          flag=false;
        }
        if (flag) {
          Speaker.write(true);
        }
 else         Speaker.write(false);
        waitForNextPeriod();
      }
    }
  }
;
  new RtThread(10,100 * 1000){
    public void run(){
      val=LED0;
      up=true;
      while (true) {
        if (state == STATE_ALL) {
          if (up) {
switch (val) {
case LED0:
              val=LED1;
            break;
case LED1:
          val=LED2;
        break;
case LED2:
      val=LED3;
    break;
case LED3:
{
    up=false;
    val=LED2;
    break;
  }
default :
val=LED0;
break;
}
}
 else {
switch (val) {
case LED0:
{
up=true;
val=LED1;
break;
}
case LED1:
val=LED0;
break;
case LED2:
val=LED1;
break;
default :
val=LED0;
break;
}
}
}
 else if (state != STATE_LINEFOLLOWER) {
val=0;
}
FutureUse.writePins(val);
waitForNextPeriod();
}
}
}
;
new RtThread(10,50 * 1000){
public void run(){
int forwardCount=0;
System.out.println("String_Node_Str");
MOTORS=new Motor[]{new Motor(0),new Motor(1)};
freeValue=0;
stop=true;
while (true) {
for (int i=0; i < 4; i++) if (Buttons.getButton(i)) {
state=i + 1;
Leds.setLed(1,(i & 1) != 0);
Leds.setLed(2,(i & 2) != 0);
break;
}
stop=!DigitalInputs.getDigitalInput(2);
if ((Buttons.getButtons() != 0) || stop) freeValue=Sensors.readSensor(IR_SENSOR);
if (stop) for (int i=0; i < 2; i++) MOTORS[i].setState(Motor.STATE_OFF);
switch (state) {
case STATE_OFF:
{
for (int i=0; i < 2; i++) MOTORS[i].setState(Motor.STATE_OFF);
break;
}
case STATE_LINEFOLLOWER:
{
if (!stop) {
int val=Sensors.readSensor(IR_SENSOR);
boolean black=val > 285;
MOTORS[MOTOR_LEFT].setDutyCyclePercentage(60);
MOTORS[MOTOR_RIGHT].setDutyCyclePercentage(60);
if (black) {
MOTORS[MOTOR_RIGHT].setState(Motor.STATE_FORWARD);
MOTORS[MOTOR_LEFT].setState(Motor.STATE_BRAKE);
FutureUse.writePins(LED0 | LED3);
}
 else {
MOTORS[MOTOR_LEFT].setState(Motor.STATE_FORWARD);
MOTORS[MOTOR_RIGHT].setState(Motor.STATE_BRAKE);
FutureUse.writePins(LED1 | LED2);
}
}
break;
}
case STATE_TOUCHSENSOR:
case STATE_IRSENSOR:
case STATE_ALL:
{
if (!stop) {
for (int i=0; i < 2; i++) MOTORS[i].setMotorPercentage(Motor.STATE_FORWARD,true,40);
int totalDifference=Sensors.readSensor(IR_SENSOR) - freeValue;
if (forwardCount >= 60) {
if (MOTORS[0].readNormalizedBackEMF()[1] <= 5) {
Leds.setLeds(0x9);
turnback(false);
}
}
if (((state == STATE_IRSENSOR) || (state == STATE_ALL)) && (Math.abs(totalDifference) >= IR_SENSOR_THRESHOLD)) {
turnback(true);
}
 else if ((state == STATE_TOUCHSENSOR || state == STATE_ALL) && (forwardCount >= 6) && (DigitalInputs.getDigitalInput(1))) {
turnback(false);
}
}
break;
}
}
{
boolean goingForward=false;
for (int i=0; i < 2; i++) {
if (MOTORS[i].getState() == Motor.STATE_FORWARD) {
goingForward=true;
break;
}
}
forwardCount=goingForward ? forwardCount + 1 : 0;
}
waitForNextPeriod();
}
}
void turnback(boolean fast){
int turnspeed, turnsleep;
boolean dirLeft=false;
if (fast) {
turnspeed=80;
turnsleep=300;
}
 else {
turnspeed=70;
turnsleep=400;
}
speed=!fast ? 0x400 : 0x50;
dirLeft=(Native.rd(Const.IO_US_CNT) & 1) != 0;
for (int i=0; i < 2; i++) MOTORS[i].setMotorPercentage(Motor.STATE_BACKWARD,false,100);
RtThread.sleepMs(100);
for (int i=0; i < 2; i++) MOTORS[i].setMotorPercentage(Motor.STATE_BACKWARD,false,turnspeed);
RtThread.sleepMs(turnsleep - 100);
MOTORS[dirLeft ? MOTOR_LEFT : MOTOR_RIGHT].setMotorPercentage(Motor.STATE_BACKWARD,false,70);
MOTORS[dirLeft ? MOTOR_RIGHT : MOTOR_LEFT].setState(Motor.STATE_OFF);
RtThread.sleepMs(600);
speed=0x400;
Leds.setLed(0,false);
Leds.setLed(3,false);
}
}
;
RtThread.startMission();
}

Fixed version (ground truth):
/** 
 * @param args
 */
public static void main(String[] args){
  new RtThread(10,1 * 100){
    public void run(){
      up=true;
      flag=false;
      value=10;
      speed=0x400;
      while (true) {
        if (state == STATE_ALL) {
          counter++;
          if ((counter % value) == 0) {
            if (flag) {
              flag=false;
            }
 else {
              flag=true;
            }
          }
          counter1++;
          if ((counter1 % speed) == 0) {
            if (speaker_up) {
              value++;
              if (value >= 50) {
                speaker_up=false;
                value--;
              }
            }
 else {
              value--;
              if (value <= 10) {
                speaker_up=true;
                value++;
              }
            }
          }
        }
 else {
          flag=false;
        }
        if (flag) {
          Speaker.write(true);
        }
 else         Speaker.write(false);
        waitForNextPeriod();
      }
    }
  }
;
  new RtThread(10,100 * 1000){
    public void run(){
      val=LED0;
      up=true;
      while (true) {
        if (state == STATE_ALL) {
          if (up) {
switch (val) {
case LED0:
              val=LED1;
            break;
case LED1:
          val=LED2;
        break;
case LED2:
      val=LED3;
    break;
case LED3:
{
    up=false;
    val=LED2;
    break;
  }
default :
val=LED0;
break;
}
}
 else {
switch (val) {
case LED0:
{
up=true;
val=LED1;
break;
}
case LED1:
val=LED0;
break;
case LED2:
val=LED1;
break;
default :
val=LED0;
break;
}
}
}
 else if (state != STATE_LINEFOLLOWER) {
val=0;
}
FutureUse.writePins(val);
waitForNextPeriod();
}
}
}
;
new RtThread(10,50 * 1000){
public void run(){
int forwardCount=0;
System.out.println("String_Node_Str");
MOTORS=new Motor[]{new Motor(0),new Motor(1)};
freeValue=0;
stop=true;
while (true) {
for (int i=0; i < 4; i++) if (Buttons.getButton(i)) {
state=i + 1;
Leds.setLed(1,(i & 1) != 0);
Leds.setLed(2,(i & 2) != 0);
break;
}
stop=!DigitalInputs.getDigitalInput(2);
if ((Buttons.getButtons() != 0) || stop) freeValue=Sensors.readSensor(IR_SENSOR);
if (stop) for (int i=0; i < 2; i++) MOTORS[i].setState(Motor.STATE_OFF);
switch (state) {
case STATE_OFF:
{
for (int i=0; i < 2; i++) MOTORS[i].setState(Motor.STATE_OFF);
break;
}
case STATE_LINEFOLLOWER:
{
if (!stop) {
int val=Sensors.readSensor(IR_SENSOR);
boolean black=val > 285;
MOTORS[MOTOR_LEFT].setDutyCyclePercentage(60);
MOTORS[MOTOR_RIGHT].setDutyCyclePercentage(60);
if (black) {
MOTORS[MOTOR_RIGHT].setState(Motor.STATE_FORWARD);
MOTORS[MOTOR_LEFT].setState(Motor.STATE_BRAKE);
FutureUse.writePins(LED0 | LED3);
}
 else {
MOTORS[MOTOR_LEFT].setState(Motor.STATE_FORWARD);
MOTORS[MOTOR_RIGHT].setState(Motor.STATE_BRAKE);
FutureUse.writePins(LED1 | LED2);
}
}
break;
}
case STATE_TOUCHSENSOR:
case STATE_IRSENSOR:
case STATE_ALL:
{
if (!stop) {
for (int i=0; i < 2; i++) MOTORS[i].setMotorPercentage(Motor.STATE_FORWARD,true,40);
int totalDifference=Sensors.readSensor(IR_SENSOR) - freeValue;
if (forwardCount >= 60) {
if (MOTORS[0].readNormalizedBackEMF()[1] <= 5) {
Leds.setLeds(0x9);
turnback(false);
}
}
if (((state == STATE_IRSENSOR) || (state == STATE_ALL)) && (Math.abs(totalDifference) >= IR_SENSOR_THRESHOLD)) {
turnback(true);
}
 else if ((state == STATE_TOUCHSENSOR || state == STATE_ALL) && (forwardCount >= 6) && (DigitalInputs.getDigitalInput(1))) {
turnback(false);
}
}
break;
}
}
{
boolean goingForward=false;
for (int i=0; i < 2; i++) {
if (MOTORS[i].getState() == Motor.STATE_FORWARD) {
goingForward=true;
break;
}
}
forwardCount=goingForward ? forwardCount + 1 : 0;
}
waitForNextPeriod();
}
}
void turnback(boolean fast){
int turnspeed, turnsleep;
boolean dirLeft=false;
if (fast) {
turnspeed=80;
turnsleep=300;
}
 else {
turnspeed=70;
turnsleep=400;
}
speed=!fast ? 0x400 : 0x50;
dirLeft=(Native.rd(Const.IO_US_CNT) & 1) != 0;
for (int i=0; i < 2; i++) MOTORS[i].setMotorPercentage(Motor.STATE_BACKWARD,false,100);
RtThread.sleepMs(100);
for (int i=0; i < 2; i++) MOTORS[i].setMotorPercentage(Motor.STATE_BACKWARD,false,turnspeed);
RtThread.sleepMs(turnsleep - 100);
MOTORS[dirLeft ? MOTOR_LEFT : MOTOR_RIGHT].setMotorPercentage(Motor.STATE_BACKWARD,false,70);
MOTORS[dirLeft ? MOTOR_RIGHT : MOTOR_LEFT].setState(Motor.STATE_OFF);
RtThread.sleepMs(600);
speed=0x400;
Leds.setLed(0,false);
Leds.setLed(3,false);
}
}
;
RtThread.startMission();
}