Thursday, February 28, 2019

StarCadets is on BoardGameGeek!



I just discovered that my StarCadets game is up on the BoardGameGeek!  This is so awesome!  The BoardGameGeek!  Coolness!

Here is the link to the page:

StarCadets Page on BoardGameGeek.com

It was uploaded on 5-21-2012!  How did I miss it for that long?




Tuesday, December 25, 2018

Don's Electronic Thing 12 -- Latest Program! VLL!

Well, here it is:  The latest version of the program.  In this update, the VLL part works.  Yes!  IT WORKS!  (Though, it does seem to drop/error out on the 8th or 10th command sent.  Have to figure that out sometime.)  The program will send all 128 vll codes.  So it should work for the codepilot, and the microscout!   Note: If you want to use the direct mode commands (motor forward, motor reverse), you will have to keep sending those commands.  Command speed has not been tested.  (ie: Just how fast you can send commands to it, before it crashes.)
---------------------------------------------------------------

/*
  Lego Connector Version 3C
  by Donald Hosford (Donzerme@Yahoo.com)

  Last Update: 12/23/2018

  Purpose:
  -- Use an Arduino Uno to enable old Lego Computer Bricks to be
  controlled by any bluetooth enabled smart phone/tablet/PC etc.

  -- Working on:  ???

  ----------------------------
  Physical Pin assignments:

  pin 0 - hardware USB port transmit
  pin 1 - hardware USB port recieve
  pin 2 - IR receiver unit
  pin 3 - IR transmit led
  pin 4 - MicroScout 1 led
  pin 5 - MicroScout 2 led
  pin 6 - MicroScout 3 led
  pin 7 - MicroScout 1 touch switch
  pin 8 - MicroScout 2 touch switch
  pin 9 - MicroScout 3 touch switch
  pin10 - BT transmit
  pin11 - BT recieve

  ----------------------------
  Commands:
  All commands are simple text characters.  Every command will start
  with a two letter device code (upper case for now).  Below is a
  list of Lego P-brick devices I intend to support.

  ----------------------------
  Command format: (Device)(unit id)(action)

  Devices: (So far -- this may change.)
  BM - Bionicle Manas
  MS - MicroScout (Program 4)
  PF - Power Functions
  RC - RCX
  SB - Spybot
  SC - Scout
  VL - MicroScout VLL

  Power Functions - maybe not, as I don't have any of these sets.
  ----------------------------
  Switches:
   - I put three momentary switches on my arduino.
   - I plan to use them to indicate the microscout motor rotations.
   
  Commands:
    MSS1 - Turns ON the switch watcher 'subroutine'.
    MSS0 - Turns OFF the switch watcher 'subroutine'.  

  When the switch watcher is ON, reports every time
  a switch changes.

  The report starts with 'MS', followed by a four digit
  number.  (Example: MS1000)  Only the last three digits
  on the right show the switch status.
  
  A "0" (zero) indicate that switch isn't pressed.
  A "1" (one) indicates that switch is pressed.
  ----------------------------
  Microscout:

  Device: MS

  Unit ID:
  0 - All units
  1 - First unit
  2 - Second unit
  3 - Third unit

  Action:
  0 - Stop motor
  1 - Forward rotation
  2 - Backwards rotation

  Example: MS11 - MicroScout unit 1, Motor forwards.
  MS02 - All MicroScouts, motor backwards.

  ----------------------------
  VLL:

  Device: VL

  Unit ID:
  0 - All units
  1 - First unit
  2 - Second unit
  3 - Third unit

  Action:
  code 0 to 127
  I - Initialize microscout for vll?


  Examples:
  VL1100 - Send VLL code 100 to Microscout port 1,
  VL1I   - Initialize microscout port 1 for vll?
  Note: call VLLInit() first?  Then send VLL commands.

  ----------------------------
*/
// Code Start:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

// init variables
String message = "";

// initialize MicroScout variables
int MSNum = 0;
int Action = 0;
int d = 0;
String c = "";


//Array hold MicroScout data
int MSD[] = {
  0, 1, 1, 1
};

//Pin array to hold the assignments
int pin[] = {
  0, 4, 5, 6, 7, 8, 9, 3, 2, 10, 11
};
/*
  Pin[] array positions
  0 = space holder, not a pin number
  1 = MS1 Led
  2 = MS2 Led
  3 = MS3 Led
  4 = MS1 sw
  5 = MS2 sw
  6 = MS3 sw
  7 = IR transmitter led
  8 = IR receiver unit
  9 = BT transmitter
  10 = BT receiver
*/


// MS Switch Last Total
int LTot = 1000;

// Switch Debounce
// SwitchDebounce time in ms for microscout switches.
int SwitchDebounce = 100;

// Last Time Switches Checked
int LSC = millis();

// Current Time Checked
int CC = LSC;

// MS Switch Status
int MS1St = 0;
int MS2St = 0;
int MS3St = 0;

// MS Switch pins
int SW1 = 7;
int SW2 = 8;
int SW3 = 9;

// Switchtoggle 0=switch check off, 1=switch check on
int Switchtoggle = 0;

//VLL variables
int N = 0;
int N2 = 0;
int command = 0;

// Infared variables
//   None yet...

void setup() {
  //Serial.begin(9600);
  //while (!Serial) {
  //  ; // wait for serial port to connect.
  //}

  // setup software serial port
  mySerial.begin(9600);

  LSC = CC;


  //pinmodes
  pinMode(pin[1], OUTPUT); // MS1 led
  pinMode(pin[2], OUTPUT); // MS2 led
  pinMode(pin[3], OUTPUT); // MS3 led
  pinMode(SW1, INPUT_PULLUP); // MS1 switch
  pinMode(SW2, INPUT_PULLUP); // MS2 switch
  pinMode(SW3, INPUT_PULLUP); // MS3 switch
  pinMode(pin[7], OUTPUT); // IR send
  pinMode(pin[8], INPUT); // IR receive
  // Software serial port doesn't need pinMode...
  //pinMode(pin[9], OUTPUT); // BT send
  //pinMode(pin[10], INPUT); // BT receive


  //serial setup/output ready
  //tell user its ready for commands
  //Serial.println("Ready");
  mySerial.println("Ready");
}

// ----------------------------
void loop() {

  //check switches
  if (Switchtoggle == 1) {
    MSSwitches();
  }
  message = "";

  // listen for messages
  // check for messages from the serial monitor.
  // then check for messages from the BT HC-05 module.
  //if (Serial.available() > 0) {
  //  message = Serial.readString();
  //  Serial.println("you typed:");
  //  Serial.println(message);
  //  if (message > "") {
  //    protocol();
  //  }
  //}

  message = "";

  if (mySerial.available() > 0) {
    message = mySerial.readString();
    mySerial.println("you typed:");
    mySerial.println(message);
    Serial.println(message);
    if (message > "") {
      protocol();
    }
  }

  message = "";

  // this code is remarked out, because it doesn't exist yet...
  // If IRToggle is on, then check status of IR reciever.
  //if (IRToggle == 1) {
  // IRWatcher();
  //}
  // end of voidloop
}

// ----------------------------
void protocol() {
  // if message starts with a two letter code then 'gosub'
  // to appropriate 'subroutine'

  if (message.startsWith("MS")) {
    MicroScout();
  }

  if (message.startsWith("VL")) {
    VLLmain();
  }

  if (message.startsWith("RC")) {
    RCX();
  }

  if (message.startsWith("BM")) {
    Manas();
  }

  if (message.startsWith("SB")) {
    Spybot();
  }

  if (message.startsWith("SC")) {
    Scout();
  }

  if (message.startsWith("PF")) {
    PowerFunctions();
  }

}
// ----------------------------
void VLLmain() {
  // Main subroutine for the VLL protocal.
  // pull MS number, and Action
  MSNum = message.charAt(2);
  MSNum = MSNum - 48;

  // get action  -- action "e"  integer
  if (message.substring(3) == "I") {
    // send vll init pulse.
    VLLinit();
    message = "";
  }
  else {

    d = message.length();
    c = message.substring(3, d);
    Action = c.toInt();
    if (Action > 127) {
      Action = 127;
    }
    d = Action;

    // code for all MS.
    if (MSNum == 0) {

      MSNum = 1;
      VLLsend();
      Action = d;
      MSNum = 2;
      VLLsend();
      Action = d;
      MSNum = 3;
      VLLsend();
      // clear message
      message = "";
      Action = -1;
      MSNum = 0;
    }
    else {
      // code for 1 MS
      VLLsend();
      // clear message
      MSNum = 0;
      Action = -1;
      message = "";
    }
  }
}
// ----------------------------
void VLLsend() {
  //retreive action, send it
  // This VLL code has been adapted from a python3 program by:
  // https://github.com/JorgePe/mindstorms-vll/blob/master/vll-atat.py

  //Serial.println("Start");

  // Init variables
  int CS = 0;
  int n = 0;
  int v = 0;


  //Action = message.toInt();

  // vll send number
  //void VLLsend()
  //VLLstart
  // led off, delay 20ms
  //  digitalWrite(pin[MSNum], LOW);
  // delay(20);

  // checksum calculation
  // checksum formulas:
  //         C or Perl: 7-((n+(n>2)+(n>>4))&7)
  //             Basic: 7-(n+int(n/4)+int(n/16)) mod 8
  //   Arduino Scratch: 7-(n+int(n/4)+int(n/16)) % 8

  // Arduino uno scratch MOD command: z = x % y

  n = Action;

  // checksum calculation -- seems to be correct
  CS = 7 - (n + int(n / 4) + int(n / 16)) % 8;

  //Serial.print(CS);
  //Serial.print("/");
  //Serial.print(n);
  //Serial.print("/");

  // Shift the bits to the left, seven spaces.
  // Makes room for the number.
  CS = CS << 7;
  //Serial.print(CS);
  //Serial.print("/");

  // Add number.
  CS = CS + n;

  //Serial.println(CS);
  //Serial.println("---");

  // Read bits, and send them.
  //send start pulse first.
  VLLstart();
  //now send the checksum and data bits.
  for ( int i = 9; i >= 0; i--) {
    v = bitRead(CS, i);

    if (v == 1) {
      VLLone();
    }
    else {
      VLLzero();
    }
  }

  //Serial.println(" ");
  //Serial.println("---");

  //finally send the stop pulse.
  VLLstop();
  // all done, clear message.
  message = "";
  Action = -1;
  Serial.println("Done");
  //done.
}

//---------------------------------
//VLL functions

void VLLone() {
  //VLL1();
  //led on, delay 20ms, led off, delay 40ms
  //Serial.print("1");
  digitalWrite(pin[MSNum], HIGH);
  delay(20);
  digitalWrite(pin[MSNum], LOW);
  delay(40);
}

void VLLzero() {
  //VLL0();
  // Led on, delay 40ms, led off, delay 20ms
  //Serial.print("0");
  digitalWrite(pin[MSNum], HIGH);
  delay(40);
  digitalWrite(pin[MSNum], LOW);
  delay(20);
}

void VLLinit() {
  // VLLinit ()
  // led on, delay 400ms, led off
  digitalWrite(pin[MSNum], HIGH);
  delay(400);
  digitalWrite(pin[MSNum], LOW);
}

void VLLstart() {
  // VLLstart()
  // led off, delay 20ms
  digitalWrite(pin[MSNum], LOW);
  delay (20);
}

void VLLstop() {
  //VLLstop();
  // led on, delay 20ms, led off, delay 60ms, led on, delay 120ms
  digitalWrite(pin[MSNum], HIGH);
  delay(20);
  digitalWrite(pin[MSNum], LOW);
  delay(60);
  digitalWrite(pin[MSNum], HIGH);
  delay(120);
  digitalWrite(pin[MSNum], LOW);


}

void VLLPause() {
  //vllpause()
  //led on, delay 150ms
  digitalWrite(pin[MSNum], HIGH);
  delay(150);
}

// ----------------------------
void MicroScout() {
  // The main entry for MicroScout program 4 protocol.
  String c = "";
  int d = 0;

  // pull MS number, and Action
  MSNum = message.charAt(2);
  MSNum = MSNum - 48;

  // get action  -- action "e"  integer
  c = message.substring(3);
  Action = c.toInt();
  d = Action;

  // code for all MS.
  if (MSNum == 0) {

    MSNum = 1;
    MSSend();
    Action = d;
    MSNum = 2;
    MSSend();
    Action = d;
    MSNum = 3;
    MSSend();
    // clear message
    message = "";
    Action = -1;
    MSNum = 0;
  }
  else {
    // code for 1 MS
    MSSend();
    // clear message
    MSNum = 0;
    Action = -1;
    message = "";
  }
}

void MSSend() {
  // code for MS program 4

  // MS switches toggle
  if (MSNum == 35) {
    if (Action == 0) {
      Switchtoggle = 0;
      mySerial.println("Switches OFF");
    }
    else {
      Switchtoggle = 1;
      mySerial.println("Switches ON");
    }
  }

  //check direction here
  //if needed do reverse blink
  if (Action == 1 && MSD[MSNum] == 2) {
    MSRevBlink();
    MSD[MSNum] = 1;
  }

  if (Action == 2 && MSD[MSNum] == 1) {
    MSRevBlink();
    MSD[MSNum] = 2;
  }

  // Action: stop motor
  if (Action == 0) {
    digitalWrite(pin[MSNum], LOW);
  }

  // Action: motor forwards
  if (Action == 1) {
    digitalWrite(pin[MSNum], HIGH);
  }

  // Action: motor backwards
  if (Action == 2) {
    digitalWrite(pin[MSNum], HIGH);
  }

  // all done, clear message.
  message = "";
  Action = -1;

}

void MSRevBlink() {
  digitalWrite(pin[MSNum], HIGH);
  delay(100);
  digitalWrite(pin[MSNum], LOW);
  delay(100);
  digitalWrite(pin[MSNum], HIGH);
  delay(100);
  digitalWrite(pin[MSNum], LOW);
  delay(100);
  digitalWrite(pin[MSNum], HIGH);
}

void MSSwitches() {

  CC = millis();

  // check MicroScout Switches and report changes in status if any.
  if ((CC - LSC) > SwitchDebounce) {

    int CTot = 1000;

    // read pins
    MS1St = digitalRead(SW1);
    MS2St = digitalRead(SW2);
    MS3St = digitalRead(SW3);

    // total statuses
    if (MS1St == 0) {
      CTot = CTot + 100;
    }
    if (MS2St == 0) {
      CTot = CTot + 10;
    }
    if (MS3St == 0) {
      CTot = CTot + 1;
    }

    // check results
    if (CTot == LTot) {
      // if equal, do nothing.
    }
    else {
      //print CTot
      mySerial.print("MS");
      mySerial.println(CTot);
      LTot = CTot;
      LSC = CC;


    }
  }
}

void MVIdAct() {
  // Init variables
  String a = "";
  char b = 0;
  String c = "";
  int d = 0;
  int e = 0;

  // get protocol -- protocol "a"  String
  a = message.substring(0, 2);
  // get MS port address -- MS address "d"  integer
  b = message.charAt(2);
  d = b - 48;
  // get action  -- action "e"  integer
  c = message.substring(3);
  e = c.toInt();
}


// ----------------------------
// code for RCX
void RCX() {
  mySerial.println("RCX");
}

// ----------------------------
// code for Bionicle Manas
void Manas() {
  mySerial.println("Bionicle Manas");
}

// ----------------------------
// code for Spybot
void Spybot() {
  mySerial.println("Spybot");
}

// ----------------------------
// code for Scout brick
void Scout() {
  mySerial.println("Scout");
}

// ----------------------------
// code for Power Functions
void PowerFunctions() {
  mySerial.println("Power Function");
}
// ----------------------------

Saturday, December 1, 2018

Don's Electronic Thing 11 -- Latest program.

Here is the latest version of the program.  In this version, the HC-05 BT connection was moved to the software serial port on pins 10/11.  This makes updating the software very easy!  Just plug the arduino into the computer's usb port, and go!  I am currently working on a control interface on an old tablet.  Any comments, suggestions, etc. are welcome.

-------------------------------------------------------------------------------

 /*
  Lego Connector Version 3
 by Donald Hosford (Donzerme@Yahoo.com)

 Last Update: 12/01/2018

 Purpose:
 -- Use an Arduino Uno to enable old Lego Computer Bricks to be
 controlled by any bluetooth enabled smart phone/tablet/PC etc.

 -- Working on:  VLL protocol

 ----------------------------
 Physical Pin assignments:

 pin 0 - hardware USB port transmit
 pin 1 - hardware USB port recieve
 pin 2 - IR receiver unit
 pin 3 - IR transmit led
 pin 4 - MicroScout 1 led
 pin 5 - MicroScout 2 led
 pin 6 - MicroScout 3 led
 pin 7 - MicroScout 1 touch switch
 pin 8 - MicroScout 2 touch switch
 pin 9 - MicroScout 3 touch switch
 pin10 - BT transmit
 pin11 - BT recieve

 ----------------------------
 Commands:
 All commands are simple text characters.  Every command will start
 with a two letter device code (upper case for now).  Below is a
 list of Lego P-brick devices I intend to support.

 ----------------------------
 Command format: (Device)(unit id)(action)

 Devices: (So far -- this may change.)
 BM - Bionicle Manas
 MS - MicroScout (Program 4)
 PF - Power Functions
 RC - RCX
 SB - Spybot
 SC - Scout
 VL - MicroScout VLL

 Power Functions - maybe not, as I don't have any of these sets.

 ----------------------------
 Microscout:

 Device: MS

 Unit ID:
 0 - All units
 1 - First unit
 2 - Second unit
 3 - Third unit

 Action:
 0 - Stop motor
 1 - Forward rotation
 2 - Backwards rotation

 Example: MS11 - MicroScout unit 1, Motor forwards.
 MS02 - All MicroScouts, motor backwards.

 ----------------------------
 VLL:

 Device: VL

 Unit ID:
 0 - All units
 1 - First unit
 2 - Second unit
 3 - Third unit

 Action:
 code 0 to 127
 I - Initialize microscout for vll?


 Examples:
 VL1100 - Send VLL code 100 to Microscout port 1,
 VL1I   - Initialize microscout port 1 for vll?
 Note: call VLLInit() first?  Then send VLL commands.

 ----------------------------
 */
// Code Start:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11); // RX, TX

// init variables
String message = "";

// initialize MicroScout variables
int MSNum = 0;
int Action = 0;

//Array hold MicroScout data
int MSD[] = {
  0, 1, 1, 1
};

//Pin array to hold the assignments
int pin[] = {
  0, 4, 5, 6, 7, 8, 9, 3, 2, 10, 11
};
/*
  Pin[] array positions
 0 = space holder, not a pin number
 1 = MS1 Led
 2 = MS2 Led
 3 = MS3 Led
 4 = MS1 sw
 5 = MS2 sw
 6 = MS3 sw
 7 = IR transmitter led
 8 = IR receiver unit
 9 = BT transmitter
 10 = BT receiver
 */


// MS Switch Last Total
int LTot = 1000;

// Switch Debounce
// SwitchDebounce time in ms for microscout switches.
int SwitchDebounce = 100;

// Last Time Switches Checked
int LSC = millis();

// Current Time Checked
int CC = LSC;

// MS Switch Status
int MS1St = 0;
int MS2St = 0;
int MS3St = 0;

// MS Switch pins
int SW1=7;
int SW2=8;
int SW3=9;

// Switchtoggle 0=switch check off, 1=switch check on
int Switchtoggle = 1;
// Switchtoggle 0=switch check off, 1=switch check on
// Switchtoggle 0=switch check off, 1=switch check on

//VLL variables
int N = 0;
int N2 = 0;
int command = 0;

// Infared variables
//   None yet...

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }
  
  // setup software serial port
  mySerial.begin(9600);

  while (!mySerial) {
    ; // wait for mySerial port to connect.
  }
  mySerial.println("Ready");


  LSC = CC;


  //pinmodes
  pinMode(pin[1], OUTPUT); // MS1 led
  pinMode(pin[2], OUTPUT); // MS2 led
  pinMode(pin[3], OUTPUT); // MS3 led
  pinMode(SW1, INPUT_PULLUP); // MS1 switch
  pinMode(SW2, INPUT_PULLUP); // MS2 switch
  pinMode(SW3, INPUT_PULLUP); // MS3 switch
  pinMode(pin[7], OUTPUT); // IR send
  pinMode(pin[8], INPUT); // IR receive
  pinMode(pin[9], OUTPUT); // BT send
  pinMode(pin[10], INPUT); // BT receive


  //serial setup/output ready
  Serial.begin(9600);
  delay(5000); // give user time to open serial monitor.
  mySerial.println("Ready");  //tell user its ready for commands


// ----------------------------
void loop() {

  message = "";

  // listen for message
  // receive messages from the serial monitor/BT HC-05 module
  while (mySerial.available() == 0) {
    // loop until something is recieved from serial.

    // If Switchtoggle is on, then check status of switches.
    // report any changes from last check.
    if (Switchtoggle == 1) {
      MSSwitches();
    }

    // this code is remarked out, because it doesn't exist yet...
    // If IRToggle is on, then check status of IR reciever.
    //if (IRToggle == 1) {
    // IRWatcher();
    //} 
  }
  message = mySerial.readString();
  mySerial.print ("you typed: ");
  mySerial.println(message);
  // process message
  if (message > "") {
    protocol();
  }
  // end of voidloop
}

// ----------------------------
void protocol() {
  // if message starts with a two letter code then 'gosub'
  // to appropriate 'subroutine'

  if (message.startsWith("MS")) {
    MicroScout();
  }

  if (message.startsWith("VL")) {
    VLL();
  }

  if (message.startsWith("RC")) {
    RCX();
  }

  if (message.startsWith("BM")) {
    Manas();
  }

  if (message.startsWith("SB")) {
    Spybot();
  }

  if (message.startsWith("SC")) {
    Scout();
  }

  if (message.startsWith("PF")) {
    PowerFunctions();
  }

}

// ----------------------------
void VLL() {
  // The main entry for MicroScout/Code Pilot VLL protocol.

  // Init variables
  String c = "";
  int CS = 0;
  int n = 0;

  // get message length
  int mlen = message.length();

  // retrieve MS number, and convert to number
  MSNum = message.charAt(2);
  MSNum = MSNum - 48;

  // retrieve action
  c = message.substring(3);
  Action = c.toInt();

  mySerial.print (Action);

  //Serial.println(MSNum);
  //Serial.println(Action);

  // check for init action.
  if (Action == 73) {
    // Initalize VLL.
    VLLInit();
    // Done, clear message.
    message = "";
    Action = -1;
  }
  else {
    VLLSend();
  }
}

void VLLSend() {
  //retreive action, send it
  // This VLL code has been adapted from a python3 program by:
  // https://github.com/JorgePe/mindstorms-vll/blob/master/vll-atat.py

  //Serial.println("Start");

  // Init variables
  int CS = 0;
  int n = 0;
  int v = 0;
  // Delay long: 40ms
  int DLo = 38;
  // Delay short: 20ms
  int DSh = 19;

  Action = message.toInt();

  // vll send number
  //void VLLsend()
  //VLLstart
  // led off, delay 20ms
  digitalWrite(pin[MSNum], LOW);
  delay(20);

  // checksum calculation
  // checksum formulas:
  //         C or Perl: 7-((n+(n>2)+(n>>4))&7)
  //             Basic: 7-(n+int(n/4)+int(n/16)) mod 8
  //   Arduino Scratch: 7-(n+int(n/4)+int(n/16)) % 8

  // Arduino uno scratch MOD command: z = x % y

  n = Action;

  //Serial.println("---");
  //Serial.print(n);
  //Serial.print("/");

  // checksum calculation -- seems to be correct
  CS = 7 - (n + int(n / 4) + int(n / 16)) % 8;
  //Serial.print(CS);
  //Serial.print("/");

  // Shift the bits to the left, seven spaces.
  // Makes room for the number.
  CS = CS << 7;
  //Serial.print(CS);
  //Serial.print("/");

  // Add number.
  CS = CS + n;
  //Serial.println(CS);
  //Serial.println("---");

  // Read bits, and send them.
  for ( int i = 9; i >= 0; i--) {
    v = bitRead(CS, i);

    //Serial.print(i);
    //Serial.print("/");
    //Serial.print(v);
    //Serial.print("/");

    //Serial.print (i);

    if (v == 1) {
      //VLL1();
      //led on, delay 20ms, led off, delay 40ms
      //Serial.print("One");
      digitalWrite(pin[MSNum], HIGH);
      delay(DSh);
      digitalWrite(pin[MSNum], LOW);
      delay(DLo);
    }
    else {
      //VLL0();
      // Led on, delay 40ms, led off, delay 20ms
      //Serial.print("Zero");
      digitalWrite(pin[MSNum], HIGH);
      delay(DLo);
      digitalWrite(pin[MSNum], LOW);
      delay(DSh);
    }
    //Serial.print("/");
  }
  //VLLstop();
  // led on, delay 20ms, led off, delay 60ms, led on, delay 120ms
  digitalWrite(pin[MSNum], HIGH);
  delay(20);
  digitalWrite(pin[MSNum], LOW);
  delay(60);
  digitalWrite(pin[MSNum], HIGH);
  delay(120);
  digitalWrite(pin[MSNum], LOW);

  //vllpause();
  //led on, delay 150
  digitalWrite(pin[MSNum], HIGH);
  delay(150);
  digitalWrite(pin[MSNum], LOW);
  // all done, clear message.
  message = "";
  Action = -1;
  //Serial.println("Done");
}

void VLLInit() {
  // VLLinit() {
  // led on, delay 400ms, (led off?)
  digitalWrite(pin[MSNum], HIGH);
  delay(400);
  digitalWrite(pin[MSNum], LOW);

  // delay 2000ms?
  //delay(2000);
}

// ----------------------------
void MicroScout() {
  // The main entry for MicroScout program 4 protocol.
  String c = "";
  int d = 0;

  // pull MS number, and Action
  MSNum = message.charAt(2);
  MSNum = MSNum - 48;

  // get action  -- action "e"  integer
  c = message.substring(3);
  Action = c.toInt();
  d = Action;

  // code for all MS.
  if (MSNum == 0) {

    MSNum = 1;
    MSSend();
    Action = d;
    MSNum = 2;
    MSSend();
    Action = d;
    MSNum = 3;
    MSSend();
    // clear message
    message = "";
    Action = -1;
    MSNum = 0;
  }
  else {
    // code for 1 MS
    MSSend();
    // clear message
    MSNum = 0;
    Action = -1;
    message = "";
  }
}

void MSSend() {
  // code for MS program 4

  // MS switches toggle
  if (MSNum == 35) {
    if (Action == 0) {
      Switchtoggle = 0;
      Serial.println("MS Switches OFF");
    }
    else {
      Switchtoggle = 1;
      Serial.println("MS Switches ON");
    }
  }

  //check direction here
  //if needed do reverse blink
  if (Action == 1 && MSD[MSNum] == 2) {
    MSRevBlink();
    MSD[MSNum] = 1;
  }

  if (Action == 2 && MSD[MSNum] == 1) {
    MSRevBlink();
    MSD[MSNum] = 2;
  }

  // Action: stop motor
  if (Action == 0) {
    digitalWrite(pin[MSNum], LOW);
  }

  // Action: motor forwards
  if (Action == 1) {
    digitalWrite(pin[MSNum], HIGH);
  }

  // Action: motor backwards
  if (Action == 2) {
    digitalWrite(pin[MSNum], HIGH);
  }

  // all done, clear message.
  message = "";
  Action = -1;

}

void MSRevBlink() {
  digitalWrite(pin[MSNum], HIGH);
  delay(100);
  digitalWrite(pin[MSNum], LOW);
  delay(100);
  digitalWrite(pin[MSNum], HIGH);
  delay(100);
  digitalWrite(pin[MSNum], LOW);
  delay(100);
  digitalWrite(pin[MSNum], HIGH);
}

void MSSwitches() {

  CC = millis();

  // check MicroScout Switches and report changes in status if any.
  if ((CC - LSC) > SwitchDebounce) {

    int CTot = 1000;

    // read pins
    MS1St=digitalRead(SW1);
    MS2St=digitalRead(SW2);
    MS3St=digitalRead(SW3);

    // total statuses
    if (MS1St == 0) {
      CTot=CTot+100;
    }
    if (MS2St == 0) {
      CTot=CTot+10;
    }
    if (MS3St == 0) {
      CTot=CTot+1;
    }

    // check results
    if (CTot == LTot) {
      // if equal, do nothing.
    }
    else {
      //print CTot
      mySerial.print("MS");
      mySerial.println(CTot);
      LTot=CTot;
      LSC = CC;


    }
  }
}

void MVIdAct(){
  // Init variables
  String a = "";
  char b = 0;
  String c = "";
  int d = 0; 
  int e = 0;

  // get protocol -- protocol "a"  String
  a = message.substring(0,2);
  // get MS port address -- MS address "d"  integer
  b = message.charAt(2);
  d=b-48;
  // get action  -- action "e"  integer
  c = message.substring(3);
  e = c.toInt();
}


// ----------------------------
// code for RCX
void RCX() {
  mySerial.println("RCX");
}

// ----------------------------
// code for Bionicle Manas
void Manas() {
  mySerial.println("Bionicle Manas");
}

// ----------------------------
// code for Spybot
void Spybot() {
  mySerial.println("Spybot");
}

// ----------------------------
// code for Scout brick
void Scout() {
  mySerial.println("Scout");
}

// ----------------------------
// code for Power Functions
void PowerFunctions() {
  mySerial.println("Power9 Function");
}