fork(2) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.io.*;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. String s;
  11. while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
  12. }
  13. }
Runtime error #stdin #stdout #stderr 0.11s 320704KB
stdin
1
2Test Code
//////////////////////////////////////////////////////////////////////////////////////////
//File:    lineFollow3.ic	
//Robust   04/18/2012
//Purpose: Brute force to have rover follow a line (~2.5 inches wide)
//////////////////////////////////////////////////////////////////////////////////////////

int pctMOTOR_POWER   = 90;  //power level for motors
int wasFollowingLine = 0;   //used when off course (not on line)

//direction rover was last going (0=none,1=forward,2=left,3=right)
int lastMove         = 0;   

//States the rover can be in, as interpreted from the sensor values
#define onLine       1  //state indicative of lowRead from Sensors
#define spaceInsight 2  //state indicative of midRead from Sensors
#define lineInsight  3  //state indicative of highRead from Sensors
#define inSpace      4  //state indicative of highestRead from Sensors

//Range of values from the sensors that indicate where the line is. These
//need to be calibrated based on the line quality.  
//These trigger state changes
//of the rover according to the following ranges.
//onLine = 0-80; spaceInsight = 81-120; 
//lineInsight = 121-190; inSpace = 191+
int lowRead     =     80;
int midRead     =     120;
int highRead    =     190;
int highestRead =     255;

//sensors shows low voltage when no infrared light is reflected
//which varies according to the reflection distance and surface. 
//The value read from AX-11 board will range from 10 to 255. 
//Left sensor connected to AI-31 and Right connected to AI-17
#define READ_LEFT analog(31)
#define READ_MIDDLE analog(25)
#define READ_RIGHT analog(17)

//Amount of time (seconds) for motor movement when commanded to do so
#define FORWARD_TIME   0.1
#define BACKWARD_TIME  0.3
#define HARD_TURN_TIME 0.075
#define VEER_TURN_TIME 0.025

void main()
{    
    //State of each sensor (onLine, spaceInsight, lineInsight, inSpace)
    int sensor[3]   = {0,0,0};
    int sensorRead[3] = {0,0,0}; //analog values read from each sensor
    int i = 0; //temporary used for loops
    float j = 0.0; //used when line is not in sight
    
    //Pause for the operator to press start button
    printf("Press Start!\n");
    start_press();
    printf("Follow Line ...\n");
    
    //Loop until the operator presses the stop button
    while(!stop_button())
      {
        //continuously read from the light sensors.
        sensorRead[0] = READ_LEFT;
        sensorRead[1] = READ_RIGHT;
        sensorRead[2] = READ_MIDDLE;
        
        printf("MDL = %d\n", sensorRead[2]);
        
        //Check the values from the sensors and determine their state
        for (i=0; i<=2; i++)
          { 
            if (sensorRead[i] <= lowRead)
              {
                sensor[i] = onLine;
            }
            else if(sensorRead[i] <= midRead)
              {
                sensor[i] = spaceInsight;
            }
            else if(sensorRead[i] <= highRead)
              {
                sensor[i] = lineInsight;
            }
            else
              {
                sensor[i] = inSpace;
            }
        }// end of for loop
        
        //Logic to determine what moves the rover should make next.
        if((sensor[2] == onLine) || (sensorRead[2]<100))
          {
            MoveForward(FORWARD_TIME);
            lastMove = 1;
            wasFollowingLine = 1;
        }
        else if(((sensor[0]==onLine) && (sensor[1]==onLine)) || ((sensor[0]==lineInsight) && (sensor[1]==lineInsight)) || ((sensor[0]==spaceInsight) && (sensor[1]==spaceInsight)))
          {
//Both sensors detect line or line is small in width and they are on 
//it or getting near a line --> Move Forward
            MoveForward(FORWARD_TIME); //command rover to move
            lastMove = 1;              //set direction of rover
            wasFollowingLine = 1;
        }
        else if(((sensor[0]==spaceInsight) && (sensor[1]==onLine)) || ((sensor[0]==lineInsight) && (sensor[1]==spaceInsight)))
          {
            //veering off course so move a little towards sensor [1]
            MoveRight(VEER_TURN_TIME);
            lastMove = 3;
            wasFollowingLine = 1;
        }
        else if(((sensor[0]==onLine) && (sensor[1]==spaceInsight)) || ((sensor[0]==spaceInsight) && (sensor[1]==lineInsight))) 
          {
            //veering off course so move a little towards sensor[0]
            MoveLeft(VEER_TURN_TIME);            
            lastMove = 2;
            wasFollowingLine = 1;
        }
        else if(((sensor[0]==onLine)&&((sensor[1]==lineInsight)||(sensor[1]==inSpace))) || (((sensor[0]==spaceInsight)||(sensor[0]==lineInsight))&&(sensor[1]==inSpace)) || ((sensor[0]==onLine)&&(sensor[1]==lineInsight)))
          {
            //Turn Left
            MoveLeft(HARD_TURN_TIME);
            lastMove = 2;
            wasFollowingLine = 1;
        }
        else if((((sensor[0]==lineInsight)||(sensor[0]==inSpace))&&(sensor[1]==onLine)) || ((sensor[0]==inSpace)&&((sensor[1]==spaceInsight)||(sensor[1]==lineInsight))) || ((sensor[0]==lineInsight)&&(sensor[1]==onLine)))
          {
            //Turn Right
            MoveRight(HARD_TURN_TIME);
            lastMove = 3;
            wasFollowingLine = 1;
        }
        else 
          {
            //Neither sensor detecting anything 
            if(wasFollowingLine)
              {
                switch (lastMove)
                  {
                    case 0:
                      {   
//This should not occur, this means the last move was nothing
//yet wasFollowingLine is TRUE, so this is a failure in something
                        printf("Danger, Danger!\n");
                        break;
                    }
                    case 1:
                      {
//Rover was following line, lost it, and last move was forward
                        //--> Turn around and backtrack the way you came
                        MoveBackward(BACKWARD_TIME);
                        MoveLeft(HARD_TURN_TIME*5.0);
                        MoveForward(FORWARD_TIME);
                        wasFollowingLine = 0;
                        lastMove = 1;
                        break;
                    }
                    case 2:
                      {
//Rover was following line, lost it, and last move was left turn
                        //--> Backup and turn to the right
                        MoveBackward(BACKWARD_TIME);
                        MoveRight(HARD_TURN_TIME);
                        wasFollowingLine = 0;
                        lastMove = 3;
                        break;
                    }  
                    case 3:
                      {
//Rover was following line, lost it, and last move was right turn
                        //--> Backup and turn to the left
                        MoveBackward(BACKWARD_TIME);
                        MoveLeft(HARD_TURN_TIME);
                        wasFollowingLine = 0;
                        lastMove = 2;
                        break;
                    }  
                    default:
                      {
//Rover was following line, lost it, and last move was undefined
                        //--> Backup and try again
                        MoveBackward(BACKWARD_TIME);
                        wasFollowingLine = 0;
                        lastMove = 0;
                        break;
                    }  
                }//end of switch
            }//end of if(wasFollowingLine)
            else
              {
//were not following line so we need to locate a line somewhere in space
                if(lastMove==0)
                  {
//not been following line and we haven't moved we must be just starting
                    printf("here we goooooo\n");
                    MoveForward(FORWARD_TIME);
                    lastMove = 1;
                }
                else
                  {
                    //look for line taking a hunting dog approach
                    printf("hunting ...\n");
                    MoveForward(FORWARD_TIME+j);
                    MoveLeft(HARD_TURN_TIME);
//incremented to increase the diameter of the circle
                    j=j+0.02;  
                }
            }            
        }//end of else (neither detector detecting anything
    }//end of while(!stop_button)
    
    //Turn all motors off
    ao();
    printf("Done Following :-)\n");
}//end of void main()

void MoveLeft(float x)
{
    motor(0,-pctMOTOR_POWER);
    motor(1,pctMOTOR_POWER);
    sleep(x);
}

void MoveRight(float x)
{
    motor(0,pctMOTOR_POWER);
    motor(1,-pctMOTOR_POWER);
    sleep(x);    
}

void MoveForward(float x)
{
    motor(0,pctMOTOR_POWER);
    motor(1,pctMOTOR_POWER);
    sleep(x);
}

void MoveBackward(float x)
{
    motor(0,-pctMOTOR_POWER);
    motor(1,-pctMOTOR_POWER);
    sleep(x);       
}


stdout
1
2Test Code
//////////////////////////////////////////////////////////////////////////////////////////
//File:    lineFollow3.ic	
//Robust   04/18/2012
//Purpose: Brute force to have rover follow a line (~2.5 inches wide)
//////////////////////////////////////////////////////////////////////////////////////////

int pctMOTOR_POWER   = 90;  //power level for motors
int wasFollowingLine = 0;   //used when off course (not on line)

//direction rover was last going (0=none,1=forward,2=left,3=right)
int lastMove         = 0;   

//States the rover can be in, as interpreted from the sensor values
#define onLine       1  //state indicative of lowRead from Sensors
#define spaceInsight 2  //state indicative of midRead from Sensors
#define lineInsight  3  //state indicative of highRead from Sensors
#define inSpace      4  //state indicative of highestRead from Sensors

//Range of values from the sensors that indicate where the line is. These
//need to be calibrated based on the line quality.  
//These trigger state changes
//of the rover according to the following ranges.
//onLine = 0-80; spaceInsight = 81-120; 
//lineInsight = 121-190; inSpace = 191+
int lowRead     =     80;
int midRead     =     120;
int highRead    =     190;
int highestRead =     255;

//sensors shows low voltage when no infrared light is reflected
//which varies according to the reflection distance and surface. 
//The value read from AX-11 board will range from 10 to 255. 
//Left sensor connected to AI-31 and Right connected to AI-17
#define READ_LEFT analog(31)
#define READ_MIDDLE analog(25)
#define READ_RIGHT analog(17)

//Amount of time (seconds) for motor movement when commanded to do so
#define FORWARD_TIME   0.1
#define BACKWARD_TIME  0.3
#define HARD_TURN_TIME 0.075
#define VEER_TURN_TIME 0.025

void main()
{    
    //State of each sensor (onLine, spaceInsight, lineInsight, inSpace)
    int sensor[3]   = {0,0,0};
    int sensorRead[3] = {0,0,0}; //analog values read from each sensor
    int i = 0; //temporary used for loops
    float j = 0.0; //used when line is not in sight
    
    //Pause for the operator to press start button
    printf("Press Start!\n");
    start_press();
    printf("Follow Line ...\n");
    
    //Loop until the operator presses the stop button
    while(!stop_button())
      {
        //continuously read from the light sensors.
        sensorRead[0] = READ_LEFT;
        sensorRead[1] = READ_RIGHT;
        sensorRead[2] = READ_MIDDLE;
        
        printf("MDL = %d\n", sensorRead[2]);
        
        //Check the values from the sensors and determine their state
        for (i=0; i<=2; i++)
          { 
            if (sensorRead[i] <= lowRead)
              {
                sensor[i] = onLine;
            }
            else if(sensorRead[i] <= midRead)
              {
                sensor[i] = spaceInsight;
            }
            else if(sensorRead[i] <= highRead)
              {
                sensor[i] = lineInsight;
            }
            else
              {
                sensor[i] = inSpace;
            }
        }// end of for loop
        
        //Logic to determine what moves the rover should make next.
        if((sensor[2] == onLine) || (sensorRead[2]<100))
          {
            MoveForward(FORWARD_TIME);
            lastMove = 1;
            wasFollowingLine = 1;
        }
        else if(((sensor[0]==onLine) && (sensor[1]==onLine)) || ((sensor[0]==lineInsight) && (sensor[1]==lineInsight)) || ((sensor[0]==spaceInsight) && (sensor[1]==spaceInsight)))
          {
//Both sensors detect line or line is small in width and they are on 
//it or getting near a line --> Move Forward
            MoveForward(FORWARD_TIME); //command rover to move
            lastMove = 1;              //set direction of rover
            wasFollowingLine = 1;
        }
        else if(((sensor[0]==spaceInsight) && (sensor[1]==onLine)) || ((sensor[0]==lineInsight) && (sensor[1]==spaceInsight)))
          {
            //veering off course so move a little towards sensor [1]
            MoveRight(VEER_TURN_TIME);
            lastMove = 3;
            wasFollowingLine = 1;
        }
        else if(((sensor[0]==onLine) && (sensor[1]==spaceInsight)) || ((sensor[0]==spaceInsight) && (sensor[1]==lineInsight))) 
          {
            //veering off course so move a little towards sensor[0]
            MoveLeft(VEER_TURN_TIME);            
            lastMove = 2;
            wasFollowingLine = 1;
        }
        else if(((sensor[0]==onLine)&&((sensor[1]==lineInsight)||(sensor[1]==inSpace))) || (((sensor[0]==spaceInsight)||(sensor[0]==lineInsight))&&(sensor[1]==inSpace)) || ((sensor[0]==onLine)&&(sensor[1]==lineInsight)))
          {
            //Turn Left
            MoveLeft(HARD_TURN_TIME);
            lastMove = 2;
            wasFollowingLine = 1;
        }
        else if((((sensor[0]==lineInsight)||(sensor[0]==inSpace))&&(sensor[1]==onLine)) || ((sensor[0]==inSpace)&&((sensor[1]==spaceInsight)||(sensor[1]==lineInsight))) || ((sensor[0]==lineInsight)&&(sensor[1]==onLine)))
          {
            //Turn Right
            MoveRight(HARD_TURN_TIME);
            lastMove = 3;
            wasFollowingLine = 1;
        }
        else 
          {
            //Neither sensor detecting anything 
            if(wasFollowingLine)
              {
                switch (lastMove)
                  {
                    case 0:
                      {   
//This should not occur, this means the last move was nothing
//yet wasFollowingLine is TRUE, so this is a failure in something
                        printf("Danger, Danger!\n");
                        break;
                    }
                    case 1:
                      {
//Rover was following line, lost it, and last move was forward
                        //--> Turn around and backtrack the way you came
                        MoveBackward(BACKWARD_TIME);
                        MoveLeft(HARD_TURN_TIME*5.0);
                        MoveForward(FORWARD_TIME);
                        wasFollowingLine = 0;
                        lastMove = 1;
                        break;
                    }
                    case 2:
                      {
//Rover was following line, lost it, and last move was left turn
                        //--> Backup and turn to the right
                        MoveBackward(BACKWARD_TIME);
                        MoveRight(HARD_TURN_TIME);
                        wasFollowingLine = 0;
                        lastMove = 3;
                        break;
                    }  
                    case 3:
                      {
//Rover was following line, lost it, and last move was right turn
                        //--> Backup and turn to the left
                        MoveBackward(BACKWARD_TIME);
                        MoveLeft(HARD_TURN_TIME);
                        wasFollowingLine = 0;
                        lastMove = 2;
                        break;
                    }  
                    default:
                      {
//Rover was following line, lost it, and last move was undefined
                        //--> Backup and try again
                        MoveBackward(BACKWARD_TIME);
                        wasFollowingLine = 0;
                        lastMove = 0;
                        break;
                    }  
                }//end of switch
            }//end of if(wasFollowingLine)
            else
              {
//were not following line so we need to locate a line somewhere in space
                if(lastMove==0)
                  {
//not been following line and we haven't moved we must be just starting
                    printf("here we goooooo\n");
                    MoveForward(FORWARD_TIME);
                    lastMove = 1;
                }
                else
                  {
                    //look for line taking a hunting dog approach
                    printf("hunting ...\n");
                    MoveForward(FORWARD_TIME+j);
                    MoveLeft(HARD_TURN_TIME);
//incremented to increase the diameter of the circle
                    j=j+0.02;  
                }
            }            
        }//end of else (neither detector detecting anything
    }//end of while(!stop_button)
    
    //Turn all motors off
    ao();
    printf("Done Following :-)\n");
}//end of void main()

void MoveLeft(float x)
{
    motor(0,-pctMOTOR_POWER);
    motor(1,pctMOTOR_POWER);
    sleep(x);
}

void MoveRight(float x)
{
    motor(0,pctMOTOR_POWER);
    motor(1,-pctMOTOR_POWER);
    sleep(x);    
}

void MoveForward(float x)
{
    motor(0,pctMOTOR_POWER);
    motor(1,pctMOTOR_POWER);
    sleep(x);
}

void MoveBackward(float x)
{
    motor(0,-pctMOTOR_POWER);
    motor(1,-pctMOTOR_POWER);
    sleep(x);       
}


stderr
Exception in thread "main" java.lang.NullPointerException
	at Ideone.main(Main.java:12)