Mindstorm Robotics and Visual C++
by David Wendt

Listing One
void TorbotDlg::OnDownload()
{
  m_spirit.SelectPrgm( PROGRAM_5 );  // (1) program 5
  m_spirit.BeginOfTask( TASK_0 );    // (2) start task generation
  {
    m_spirit.SetVar( VAR_0, SRC_CONSTANT,0 );   // (3) set var0 to 0
    // (4) set sensor 1 to switch
    m_spirit.SetSensorType( SENSOR_1, SENSOR_TYPE_SWITCH );
    m_spirit.SetSensorMode( SENSOR_1, SENSOR_MODE_BOOLEAN, 0 );
    // set sensor 3 to switch
    m_spirit.SetSensorType( SENSOR_3, SENSOR_TYPE_SWITCH );
    m_spirit.SetSensorMode( SENSOR_3, SENSOR_MODE_BOOLEAN, 0 );
    m_spirit.Loop( SRC_CONSTANT,INFINITE_LOOP );  // (5) loop forever
    {
      // (6) is switch on 1 ?
      m_spirit.If( SRC_SENSOR_VALUE, SENSOR_1, OP_EQUAL,
                   SRC_CONSTANT, 1 );
      {
        // (7) are we still processing the variable ?
        m_spirit.If( SRC_VARIABLE, VAR_0, OP_EQUAL,
                     SRC_CONSTANT, 0 );
        {
          // (8) turn off motors
          m_spirit.Off( MOTOR_AC );
          // (9) set var0 to 1
          m_spirit.SetVar( VAR_0, SRC_CONSTANT, 1 );
          // (10) slight delay to let event fire
          m_spirit.Wait( SRC_CONSTANT, 20 );
        }
        m_spirit.EndIf();  // (11)
      }
      m_spirit.EndIf();

      // (12) is switch on 3 ?
      m_spirit.If( SRC_SENSOR_VALUE, SENSOR_3, OP_EQUAL,
                   SRC_CONSTANT, 1 );
      {
        // are we still processing the variable ?
        m_spirit.If( SRC_VARIABLE, VAR_0, OP_EQUAL,
                     SRC_CONSTANT, 0 );
        {
          // turn off motors
          m_spirit.Off( MOTOR_AC );
          // set var0 to 2
          m_spirit.SetVar( VAR_0, SRC_CONSTANT, 2 );
          // slight delay to let event fire
          m_spirit.Wait( SRC_CONSTANT, 20 );
        }
        m_spirit.EndIf();
      }
      m_spirit.EndIf();
    }
    m_spirit.EndLoop();   // (13)
  }
  m_spirit.EndOfTask();   // (14) starts the download
}


Listing Two
void TorbotDlg::OnStart()
{
  // (1) start motors in a known state:
  //      max power, forward direction
  m_spirit.Off( MOTOR_AC );
  m_spirit.SetPower( MOTOR_AC, SRC_CONSTANT, POWER_FULL );
  m_spirit.SetFwd( MOTOR_A );     // these directions make
  m_spirit.SetRwd( MOTOR_C );     // both treads run forward

  // (2) setup to receive var0 changes every 10ms
  m_spirit.SetEvent( SRC_VARIABLE, VAR_0, 10 );
  m_spirit.StartTask( TASK_0 );      // (3) turn on task 0
  m_spirit.On( MOTOR_AC );           // (4) startup the motors
}

Listing Three
void TorbotDlg::OnVariableChangeSpiritctrl1(short Number, short Value)
{
  m_spirit.ClearEvent( SRC_VARIABLE, VAR_0 );
  switch( Value )
  {
    case 1:
      m_spirit.AlterDir( MOTOR_AC );             // backup
      m_spirit.On( MOTOR_AC );
      m_spirit.PlaySystemSound( SOUND_CLICK );   // lil' beep
      ::Sleep( 300 );                            // .3 second delay
      m_spirit.AlterDir( MOTOR_A );              // turn right
      ::Sleep( 300 );                            // .3 second delay
      m_spirit.AlterDir( MOTOR_C );              // go straight
      m_spirit.SetVar( VAR_0, SRC_CONSTANT, 0 ); // reset var0
      break;
    case 2:
      m_spirit.AlterDir( MOTOR_AC );             // backup
      m_spirit.On( MOTOR_AC );
      m_spirit.PlaySystemSound( SOUND_CLICK );   // lil' beep
      ::Sleep( 300 );                            // .3 second delay
      m_spirit.AlterDir( MOTOR_C );              // turn left
      ::Sleep( 300 );                            // .3 second delay
      m_spirit.AlterDir( MOTOR_A );              // go straight
      m_spirit.SetVar( VAR_0, SRC_CONSTANT, 0 ); // reset var0
      break;
  }
  m_spirit.SetEvent( SRC_VARIABLE, VAR_0, 10 );  // re-enable event
}




1


