Showing posts with label Lego. Show all posts
Showing posts with label Lego. Show all posts

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");
}
// ----------------------------

Wednesday, July 11, 2018

Don's Electronic Thing! part 7 -- Latest Program!

Well, here it is:
   Version 3 of the program!  The VLL code seems to work, needs testing.  The Microscout Program 4 code works.  I wanted to post it while I was thinking of it.

The main feature of the VLL code here, is that it will handle all 128 VLL codes!  So it should be able to work with the code pilot!  (As I don't have a Code Pilot, someone else will have to test that part out.)

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

 Last Update: 07/06/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:  MicroScout program 4/VLL protocol.

 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.

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

 pin 0 - BT transmit
 pin 1 - BT receive
 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
 pin 10 - BT connect led?

 ----------------------------
 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

 ----------------------------
 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.


 ----------------------------
 */

// 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, 0, 1, 10
};
/*
  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
 11 = BT Led

 */

// MSswitch variables
int MS1State = 0;
int MS2State = 0;
int MS3State = 0;
// MSxState current switch state.
int MS1Last = 0;
int MS2Last = 0;
int MS3Last = 0;
// MSxLast Last switch state when checked.
unsigned long LastTimeSwitchesChecked = 0;
unsigned long SwitchDebounce = 70;
// SwitchDebounce time in ms for microscout switches.
int Switchtoggle = 0;
// Switchtoggle 0=switch check off, 1=switch check on

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



// Infared variables



void setup() {
  //pinmodes
  pinMode(pin[1], OUTPUT); // MS1 led
  pinMode(pin[2], OUTPUT); // MS2 led
  pinMode(pin[3], OUTPUT); // MS3 led
  pinMode(pin[4], INPUT); // MS1 switch
  pinMode(pin[5], INPUT); // MS2 switch
  pinMode(pin[6], INPUT); // 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
  pinMode(pin[11], OUTPUT); // BT connect led
  //serial setup/output ready
  Serial.begin(9600);
  delay(5000); // give user time to open serial monitor.
  Serial.println("Ready");  //tell user its ready for commands
}

// ----------------------------
void loop() {
  message = "";

  // check time for MS switches
  if (Switchtoggle == 1) {
    if ((millis() - LastTimeSwitchesChecked) > SwitchDebounce) {
      MSSwitches();
    }
  }

  // listen for message
  // receive messages from the serial monitor/BT HC-05 module
  while (Serial.available() == 0) {
  }
  message = Serial.readString();
  Serial.println("you typed:");
  Serial.println(message);
  //Serial.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();

  //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;

  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(20);
        digitalWrite(pin[MSNum], LOW);
        delay(40);
    }
    else {
      //VLL0();
      // Led on, delay 40ms, led off, delay 20ms
      //Serial.print("Zero");
      digitalWrite(pin[MSNum], HIGH);
      delay(40);
      digitalWrite(pin[MSNum], LOW);
      delay(20);
    }
    //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);

  //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() {
  // check MicroScout Switches and report changes in status if any.

  // store millis()
  LastTimeSwitchesChecked = millis();
  message = "MS";

  // read MS switches
  MS1State = digitalRead(pin[4]);
  MS2State = digitalRead(pin[5]);
  MS3State = digitalRead(pin[6]);
  // check current state with last state
  if (MS1State != MS1Last) {
    message = message + "1";
    MS1Last = MS1State;
  }
  else message = message + "0";
  if (MS2State != MS2Last) {
    message = message + "1";
    MS2Last = MS2State;
  }
  else message = message + "0";
  if (MS3State != MS3Last) {
    message = message + "1";
    MS3Last = MS3State;
  }
  else message = message + "0";
  if (message > "MS") {
    //Send message
    Serial.println(message);
  }
  message = "";
}

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() {
  Serial.println("RCX");
}

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

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

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

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



Don's Electronic Thing part 6

I worked on the program.  It needed sections rewrote, because it started to feel like patches on top of patches. That messed some things up.  Sigh.  So I spent a few days fixing them.  Results: The Microscout protocol works again.  I fixed some mistakes I made in the VLL protocol.  That one still needs some work.  For a while I had some endless loops going...

I will post the program later - after I try some ideas.

Tuesday, June 5, 2018

Don's Electronic Thing part 5

Success!  (I think...)

I got the IR circuit bread-boarded.  I used the program from HERE

His circuit uses an 100 ohm resistor, I found my IR Led needed a 38 ohm resistor (I used a 47 ohm).

To test the IR circuits I downloaded his program to my arduino and experimented until it worked.

The  IR Led needs to be quite close to the TV, but it seems to work.  Maybe I should put a transistor in there to boost the signal...

Now I have to figure out how to diagram the circuit...

Wednesday, May 16, 2018

Don's Electronic Thing part 4

So far:
I have most of the MicroScout code written.  I want to a command to toggle the microscout switch reader subroutine on and off.  Then the main microscout code will be done.

Then the next is the IR circuit.  After the circuit is completed, time to build it.


Monday, May 7, 2018

Don's Electronic Thing part 3

I just received my HC-05 Bluetooth module!  Spent some time figuring out how to setup the circuit,

The fellow at this link has a nice simple setup that clearly shows how to hook up the BT module.

https://howtomechatronics.com/tutorials/arduino/arduino-and-hc-05-bluetooth-module-tutorial/

I did get it to pair with my android tablet (yea!).  Now I need to figure out the arduino coding to read it...


update:

   It does work!  I used a "chat" program in RFO Basic to send simple commands to the arduino, making a couple of Leds blink.  Next is to get the switch feed back to work.

Saturday, May 5, 2018

Don's Electronic Thing part 2 -- Software V1.0

Here is my sketch (program) for my Don's Electronic Thing V1.0

  I put in some blank sections for some of the other Lego computer bricks.  The program is a little repetitive.  I think I will re-write some of it later.  To use this, download it to your arduino, and open the serial monitor (button in upper right corner of the arduino ide).  When it says "Ready" on the serial monitor, type any commands in the space at the top.  Anyway, here is the current working version:

/*
Don's Electronic Thing Version 1
 -- Drive lego(cr) computer bricks from an Arduino Uno.
 -- Working on:
 MicroScout program 4.
 */

// init variables
String message="Test Message!";
// Pin assignments
int MS1Led=2;
int MS1Sw=3;
int MS2Led=4;
int MS2Sw=5;
int MS3Led=6;
int MS3Sw=7;
int IRTrn=8;
int IRRec=9;
int BTTrn=10;
int BTRec=11;
int BTConLed=12;

// MicroScout direction variables
int MS1=1;
int MS2=1;
int MS3=1;
int MSNum=0;
int MSAct=0;

// Infared variables


// Bluetooth variables



void setup() {
  //pinmodes
  pinMode(MS1Led,OUTPUT);
  pinMode(MS1Sw,INPUT);
  pinMode(MS2Led,OUTPUT);
  pinMode(MS2Sw,INPUT);
  pinMode(MS3Led,OUTPUT);
  pinMode(MS3Sw,INPUT);
  pinMode(IRTrn,OUTPUT);
  pinMode(IRRec,INPUT);
  pinMode(BTTrn,OUTPUT);
  pinMode(BTRec,INPUT);
  pinMode(BTConLed,OUTPUT);
  //serial setup/output ready
  Serial.begin(9600);
  delay(5000); // give user time to open serial monitor.
  Serial.println("Ready");  
}

void loop() {
  // listen for message
  // recieve messages from the serial monitor
  // in future will be from a BT HC-05 module

  while(Serial.available()==0) {
  }
  message=Serial.readString();
  Serial.println("you typed:");
  Serial.println(message);
  //Serial.println(message);
  // process message
  // if message starts with a two letter code then 'gosub'
  // to appropiate 'subroutine'
  if (message.startsWith("MS0")) {
    MS0();
  }
  if (message.startsWith("MS")) {
    MicroScout(); 
  }

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

  if (message.startsWith("RS")) {
    RCXSend();
  }

  if (message.startsWith("RR")) {
    RCXRec();
  }

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

  if (message.startsWith("YS")) {
    SpybotSend();
  }

  if (message.startsWith("YR")) {
    SpybotRec();
  }

  if (message.startsWith("CS")) {
    ScoutSend();
  }

  if (message.startsWith("CR")) {
    ScoutRec();
  }



  // end of voidloop
}


// code for MS program 4
void MicroScout() {
  /*
 now to pickout the ms number, and function number
   MS0x -- send same command to all MicroScouts.
   MS1x -- MicroScout #1
   MS2x -- MicroScout #2
   MS3x -- MicroScout #3
   0 -- stop motor
   1 -- motor forwards
   2 -- motor backwards
   */
  //Serial.println("MicroScout");

  //  Serial.println(message);

  int mlen=message.length();
  if (mlen==4 ) {

    MSNum=message.charAt(2);
    MSAct=message.charAt(3);
    MSNum=MSNum-48;
    MSAct=MSAct-48;

    //check direction here
    //if needed do reverse blink

    if (MSNum==1) {
      // check for motor direction
      if (MSAct==1 && MS1==2) {
        RevBlink();
        MS1=1;
      }
      if (MSAct==2 && MS1==1) {
        RevBlink();
        MS1=2;
      }

      if (MSAct==0) {
        digitalWrite(MS1Led, LOW);
      }
      if (MSAct==1) {
        digitalWrite(MS1Led, HIGH);
      }
      if (MSAct==2) {
        // microscout go backwards.
        digitalWrite(MS1Led, HIGH);
      }
    }

    if (MSNum==2) {
      // check for motor direction
      if (MSAct==1 && MS2==2) {
        RevBlink();
        MS2=1;
      }
      if (MSAct==2 && MS2==1) {
        RevBlink();
        MS2=2;
      }

      if (MSAct==0) {
        digitalWrite(MS2Led, LOW);
      }
      if (MSAct==1) {
        digitalWrite(MS2Led, HIGH);
      }
      if (MSAct==2) {
        // microscout go backwards.
        digitalWrite(MS2Led, HIGH);
      }
    }
    if (MSNum==3) {
      // check for motor direction
      if (MSAct==1 && MS3==2) {
        RevBlink();
        MS3=1;
      }
      if (MSAct==2 && MS3==1) {
        RevBlink();
        MS3=2;
      }

      if (MSAct==0) {
        digitalWrite(MS3Led, LOW);
      }
      if (MSAct==1) {
        digitalWrite(MS3Led, HIGH);
      }
      if (MSAct==2) {
        // microscout go backwards.
        digitalWrite(MS3Led, HIGH);
      }
    }

  }

  message="";
}

void MS0() {
  // The MicroScout "MS0x" command sends the same action to all 
  // MicroScouts.
  MSAct=message.charAt(3);
  MSAct=MSAct-48;
  message="MS1";
  message.concat(MSAct);
  MicroScout();
  message="MS2";
  message.concat(MSAct);
  MicroScout();
  message="MS3";
  message.concat(MSAct);
  MicroScout();

}

void RevBlink() {
  if (MSNum==1) {
    digitalWrite(MS1Led, HIGH);
    delay(100);
    digitalWrite(MS1Led, LOW);
    delay(100);
    digitalWrite(MS1Led, HIGH);
    delay(100);
    digitalWrite(MS1Led, LOW);
    delay(100);
    digitalWrite(MS1Led, HIGH);
  }
  if (MSNum==2) {
    digitalWrite(MS2Led, HIGH);
    delay(100);
    digitalWrite(MS2Led, LOW);
    delay(100);
    digitalWrite(MS2Led, HIGH);
    delay(100);
    digitalWrite(MS2Led, LOW);
    delay(100);
    digitalWrite(MS2Led, HIGH);
  }
  if (MSNum==3) {
    digitalWrite(MS3Led, HIGH);
    delay(100);
    digitalWrite(MS3Led, LOW);
    delay(100);
    digitalWrite(MS3Led, HIGH);
    delay(100);
    digitalWrite(MS3Led, LOW);
    delay(100);
    digitalWrite(MS3Led, HIGH);
  }
}

// code for MS VLL?
void VLL() {
  Serial.println("VLL");
}

// code for RCX
void RCXSend() {
  Serial.println("RCXSend");
}

void RCXRec() {
  Serial.println("RCXRec");
}

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

// code for Spybot
void SpybotSend() {
  Serial.println("SpybotSend");
}

void SpybotRec() {
  Serial.println("SpybotRec");
}

void ScoutSend() {
  Serial.println("ScoutSend");
}

void ScoutRec() {
  Serial.println("ScoutRec");
}