// pulse_sensor_sound.nqc -- Plays a sound after 4 presses of touch sensor

task main()
{
  SetSensor(SENSOR_1,SENSOR_PULSE);
  while(true)
  {
    if(SENSOR_1==4)
    {
      PlaySound(SOUND_DOWN);
      ClearSensor(SENSOR_1);
    }
  }
}
----------------------------------------------------------------------
int closeness = 250;     // bigger means closer to obstacls
int lastlevel;           // To store the previous level

task send_signal()
{
  while(true) 
    {SendMessage(0); Wait(10);}
}

task check_signal()
{
  while(true)
  {
    lastlevel = SENSOR_2;
    if(SENSOR_2 > lastlevel + closeness)
      {OnRev(OUT_C); Wait(100); OnFwd(OUT_A+OUT_C);}
  }
}

task main()
{
  SetSensorType(SENSOR_2, SENSOR_TYPE_LIGHT); 
  SetSensorMode(SENSOR_2, SENSOR_MODE_RAW);
  OnFwd(OUT_A+OUT_C);
  start send_signal;
  start check_signal;
}
----------------------------------------------------------------------
// time_display.nqc -- displays 1+Timer(0), sometimes only a 1!

int x=0;
task main()
{
   ClearTimer(0);
   SetUserDisplay(x,0);
   while(true)
   {
      x = 1 + Timer(0);
   }
}
----------------------------------------------------------------------
// time_display_ok.nqc -- displays 1+Timer(0) correctly

int displayValue=0;
task main()
{
   int x;
   ClearTimer(0);
   SetUserDisplay(displayValue,0);
   while(true)
   {
      x = 1 + Timer(0);
      displayValue = x;
   }
}
----------------------------------------------------------------------
// 10_wrong.nqc -- Interfering tasks
task main()
{
  SetSensor(SENSOR_1,SENSOR_TOUCH);
  start check_sensors;
  start move_square;
}

task move_square()
{
  while (true)
  {
    OnFwd(OUT_A+OUT_C); Wait(100);  // go forward a while
    OnRev(OUT_C); Wait(85);         // turn for a while
  }
}

task check_sensors()
{
  while (true)
  {
    if (SENSOR_1 == 1)
    { 
      OnRev(OUT_A+OUT_C);           // if sensor pressed
      Wait(50);                     // go backwards a while
      OnFwd(OUT_A);                 // turn a while
      Wait(85);                     // go forward
      OnFwd(OUT_C);
    }
  }
}
----------------------------------------------------------------------
// 10_stopping.nqc -- Eliminating task interference by stopping tasks
task main()
{
  SetSensor(SENSOR_1,SENSOR_TOUCH);
  start check_sensors;
  start move_square;
}

task move_square()
{
  while (true)
  {
    OnFwd(OUT_A+OUT_C); Wait(100);
    OnRev(OUT_C); Wait(85);
  }
}

task check_sensors()
{
  while (true)
  {
    if (SENSOR_1 == 1)
    { 
      stop move_square;
      OnRev(OUT_A+OUT_C); Wait(50);
      OnFwd(OUT_A); Wait(85);
      start move_square;
    }
  }
}
----------------------------------------------------------------------
// 10_semaphore1.nqc -- Stopping task interference with a semaphore
int sem;

task main()
{
  sem = 0;
  SetSensor(SENSOR_1,SENSOR_TOUCH);
  start check_sensors;
  start move_square;
}

task move_square()
{
  while (true)
  {
    until (sem == 0); sem = 1;
    OnFwd(OUT_A+OUT_C); 
    sem = 0;
    Wait(100);
    until (sem == 0); sem = 1;
    OnRev(OUT_C); 
    sem = 0;
    Wait(85);
  }
}

task check_sensors()
{
  while (true)
  {
    if (SENSOR_1 == 1)
    {
      until (sem == 0); sem = 1;
      OnRev(OUT_A+OUT_C); Wait(50);
      OnFwd(OUT_A); Wait(85);
      sem = 0;
    }
  }
}
----------------------------------------------------------------------
// 10_acquire_usr -- Using acquire for access control
task main()
{
  SetSensor(SENSOR_1,SENSOR_TOUCH);
  start check_sensors;
  start move_square;
}

task move_square()
{
  SetPriority(2);
  while (true)
  {
    acquire(ACQUIRE_USER_1)
    {
    OnFwd(OUT_A+OUT_C); Wait(100);
    OnRev(OUT_C); Wait(85);
    }
  }
}

task check_sensors()
{
  SetPriority(1);
  while (true)
  {
    if (SENSOR_1 == 1)
    {
      acquire(ACQUIRE_USER_1)
      {
        OnRev(OUT_A+OUT_C); Wait(50);
        OnFwd(OUT_A); Wait(85);
      }
    }
  }
  
}
----------------------------------------------------------------------
// event_touch_sensor -- pressing touch sensor triggers a beep

task main()
{
   SetSensor(SENSOR_1, SENSOR_TOUCH);
   SetEvent(0, SENSOR_1, EVENT_TYPE_PRESSED);
   while(true)
   {
      monitor(EVENT_MASK(0))
      {
        while(true){};
      }
      catch
      {
         PlaySound(SOUND_CLICK);
      }
   }
}