Code

In this page I will show the different codes I did to manage the electronics and the interface created for this project, everything was coded in Arduino C code and Processing.

Arduino Codes

I had to program 2 different boards for this project, the fret boards and the master board. As I explained in Networking Week, I decided to connect them with I2C through a ribbon cable, because it is easier and faster than a wireless connection. Then, between Ukuleles I decided to use Bluetooth to communicate them via serial port. This allows the two Ukuleles to talk without being connected making it more comfortable for the players. For this reason, I have 3 different codes, 1 for the slave boards and 2 for the master's boards, one for the teacher's ukulele and one for the student. In the future I will make the same code for the 2 master boards, but I didn't had the time. Let's check the code.

Codification message

First, before explaining the codes I made, I would like to explain how the communication works, how the frets are represented and how they codify the information. The communication is via I2C between boards and serial between devices, can be Bluetooth or the FTDI cable, but always using the serial protocol. Then, all the communication is made by reading and writing bytes, so I decided to codify the messages that I needed to send to send the less bytes possible and avoid errors. After some tests and talking with Oscar, I decided to send one byte for each fret, and that byte will say which fret I'm reading and the status of it. Knowing that the fret are 4 buttons or LED's with two possible states each (High or Low), with 4 bits I could represent all the possible states. That leaves us 4 more bits to represent the fret that we are representing. Finally we decided to use the first four bits for the fret number and the last 4 for the state of the fret.

This codification was a great improvement, because with other tests I had some problems reading strings or integers, sending bytes make the job much more easy and avoid any possible error. During Week 8, I had the opportunity to learn about bit operation studying C code, and that was really useful for making a optimized code.

Master's board

Teacher Ukulele

This is the code for the teacher master board, and it have two functions. It starts waiting for a byte from the connected device through the serial. This byte can be the 'r' character or the 'w' character, for reading or writing. If it receives an 'r' it reads the state of the frets, that act like an input board in this case, with the I2C communication and send back this state to the connected device. Otherwise, if it receives an 'w' it waits until it receives the desired state and then send them to the frets as outputs, to light up. This is the code:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(8, 7);

#include 

String mode = "";
int Dev[] = {1, 2, 3, 4, 5};
int nDev = 5; //Change this number accordingly to the number of devices
int State[] = {0, 0, 0, 0, 0};
int PrevState[] = {0, 0, 0, 0, 0};

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  mySerial.begin(9600);  // start serial for output
  delay(5000);
}

void loop() {
  while (mySerial.available() == 0) {}
  if (mySerial.available() > 0) {
    mode = mySerial.readStringUntil('\n');
    //mySerial.println(mode);
  }
  if (mode == "r") {
    readU();
  }
  if (mode == "w") {
    writeU();
  }
}

void readU() {
  for (byte i=0; i < nDev; i++) {
    Wire.requestFrom(Dev[i],1);
    while (Wire.available()){
      byte state = Wire.read();
      byte message = Dev[i] << 4;
      message = message | state;
      mySerial.write(message);
    }
  }
}

void writeU() {
  while (mySerial.available() == 0) {}
  for (byte i=0; i < nDev; i++) {
    byte m = mySerial.parseInt();
    Wire.beginTransmission(Dev[i]);
    Wire.write(m);              
    Wire.endTransmission();
  }
}

Student Ukulele

This code was made for the Student's Ukulele and it can only receive the desired state and it write it to the frets. This was a easy solution, but I will try to make a generic code for both and use the slide switch to switch between modes, so the student can also show the teacher. It is really simple, it just send an 'r' character to the other master to read it's state and then light's up accordingly.

#include 
SoftwareSerial mySerial(8, 7);

#include 

String mode = "";
int Dev[] = {1, 2, 3, 4, 5};
int nDev = 5; //Change this number accordingly to the number of devices
int State[] = {0, 0, 0, 0, 0};
int PrevState[] = {0, 0, 0, 0, 0};
byte lec;

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  mySerial.begin(9600);  // start serial for output
  delay(5000);
}

void loop() {
  mySerial.write("r");
  mySerial.write('\n');
  while (mySerial.available() > 0) {
    lec = mySerial.read();
    Wire.beginTransmission(int(lec >> 4)); 
    Wire.write(int(lec & 15));             
    Wire.endTransmission();
  }
  delay(100);
}

Fret Boards

This code is generic and optimized for each fret, the only change you need to do is change the address number to the fret number you want it to be. This was the first time I used bit shifting and it felt like being in heaven, I was able to go from more than 100 lines of code to 2 lines. There I realized that learning C code could be a great improvement. This code is super simple, it only reads swipe from input or output depending on the Masters action, if it request the state it read the input buttons, if it sends the state it write the output LED's.

#include 

byte pins = 0b00000000;

void setup() {
  Wire.begin(5);                
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
}

void loop() {
  //delay(100);
}

void receiveEvent(int howMany) {
  DDRA = DDRA | B00001111;
  pins=Wire.read();
  PORTA = ((PORTA>>4)<<4)|pins;
}

void requestEvent() {
  PORTA = ((PORTA>>4)<<4);
  DDRA &= ~B00001111;
  Wire.write(PINA & (B00001111)); 
}

Processing

I decided to create an interface for the Ukulele during Interface and application programming Week with Processing. You can find all the documentation in this week page, but I will explain here what it does and the latest features.

In big terms, this interface have 2 different purposes, show what you are plying and show you how to play the chords. I created a chord library with the main chords to practice and learn it in a easy way. The interface present a ukulele that lights up like the prototype and some buttons to chose the different options.

After having the first version, I decided to go a little bit further, and I get the processing interface to create MIDI notes while you are plying using the MidiBus library, so now, you can connect the Ukulele to the computer using an FTDI cable and create digital music notes that then could be processed with professional softwares like Ableton for example to create electronic music.

This is the code:

import themidibus.*;
MidiBus myBus; 

import processing.serial.*;

Serial mySerial;
String DevRead = null;
String Dev = null;

Integer PrevStates[]={0,0,0,0,0,0};
Integer PrevStates2[]={0,0,0,0,0,0};
Integer States[]={0,0,0,0,0,0};

Integer PrevStrings[]={0,0,0,0};
Integer Strings[]={0,0,0,0};

Integer channel = 0;
Integer pitch1 = 69;
Integer pitch2 = 64;
Integer pitch3 = 60;
Integer pitch4 = 67;
Integer velocity = 127;


Float sc = 3.545;
Boolean Click = false;

PShape Ukulele;

PShape Uku;

PShape Frets;

PShape BWrite;
PShape BRead;
Boolean Read = true;

PShape ButC;
Boolean BC = false;
PShape ButD;
Boolean BD = false;
PShape ButE;
Boolean BE = false;
PShape ButF;
Boolean BF = false;
PShape ButG;
Boolean BG = false;
PShape ButA;
Boolean BA = false;
PShape ButB;
Boolean BB = false;
PShape ButM;
Boolean BM = false;
PShape ButM7;
Boolean BM7 = false;
PShape But7;
Boolean B7 = false;
PShape ButSos;
Boolean BSos = false;


PShape one1;
PShape one2;
PShape one3;
PShape one4;
PShape one5;
PShape one6;
PShape one7;
PShape one8;
PShape one9;
PShape one10;
PShape one11;
PShape one12;

PShape two1;
PShape two2;
PShape two3;
PShape two4;
PShape two5;
PShape two6;
PShape two7;
PShape two8;
PShape two9;
PShape two10;
PShape two11;
PShape two12;

PShape three1;
PShape three2;
PShape three3;
PShape three4;
PShape three5;
PShape three6;
PShape three7;
PShape three8;
PShape three9;
PShape three10;
PShape three11;
PShape three12;

PShape four1;
PShape four2;
PShape four3;
PShape four4;
PShape four5;
PShape four6;
PShape four7;
PShape four8;
PShape four9;
PShape four10;
PShape four11;
PShape four12;

void setup() {
  //Display Full Screen
  size(displayWidth, displayHeight);
  
  //Start serial comunication. Change port acording.
  mySerial = new Serial(this, "COM4", 9600);
  
  //Start MIDI bus
  MidiBus.list();
  myBus = new MidiBus(this, -1, "VirtualMIDISynth #1");
    

  Ukulele = loadShape("Uku.svg");
  Uku = Ukulele.getChild("Body");
  Frets = Ukulele.getChild("Frets");
  
  BWrite = Ukulele.getChild("BWrite");
  BRead = Ukulele.getChild("BRead");
  
  ButC = Ukulele.getChild("ButC");
  ButD = Ukulele.getChild("ButD");
  ButE = Ukulele.getChild("ButE");
  ButF = Ukulele.getChild("ButF");
  ButG = Ukulele.getChild("ButG");
  ButA = Ukulele.getChild("ButA");
  ButB = Ukulele.getChild("ButB");
  ButM = Ukulele.getChild("ButM");
  But7 = Ukulele.getChild("But7");
  ButM7 = Ukulele.getChild("ButM7");
  ButSos = Ukulele.getChild("ButSos");
  
  one1 = Ukulele.getChild("N1-1");
  two1 = Ukulele.getChild("N2-1");
  three1 = Ukulele.getChild("N3-1");
  four1 = Ukulele.getChild("N4-1");
  
  one2 = Ukulele.getChild("N1-2");
  two2 = Ukulele.getChild("N2-2");
  three2 = Ukulele.getChild("N3-2");
  four2 = Ukulele.getChild("N4-2");
  
  one3 = Ukulele.getChild("N1-3");
  two3 = Ukulele.getChild("N2-3");
  three3 = Ukulele.getChild("N3-3");
  four3 = Ukulele.getChild("N4-3");
  
  one4 = Ukulele.getChild("N1-4");
  two4 = Ukulele.getChild("N2-4");
  three4 = Ukulele.getChild("N3-4");
  four4 = Ukulele.getChild("N4-4");
  
  one5 = Ukulele.getChild("N1-5");
  two5 = Ukulele.getChild("N2-5");
  three5 = Ukulele.getChild("N3-5");
  four5 = Ukulele.getChild("N4-5");
  
  one6 = Ukulele.getChild("N1-6");
  two6 = Ukulele.getChild("N2-6");
  three6 = Ukulele.getChild("N3-6");
  four6 = Ukulele.getChild("N4-6");
}

void draw() {
  
  background(255);
  scale(sc);
  shape(Uku, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  shape(Frets, 0, -displayHeight/5.5, displayWidth, displayWidth/2);

  //print("Hello");
  
  if (ClickButtonRead()==true) {
    if (Read == true && Click == false) {
      Read = false;
    }  
    else if (Read == false && Click == false){
      Read = true;
    }
    Click = true;
  }  
  if (mousePressed == false){
    Click = false;
  }  
  if (Read == true) {
    //print("Reading");
    BRead.disableStyle();
    fill(255,200,0);
    strokeWeight(0.5);
    shape(BRead, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
    fill(255,255,255);
    shape(BWrite, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
    ReadU();
    //println("Prev after");
    //println(PrevStates);
    //println(States);
    //println("  ");
    MIDIU();
    //PrevStates2=States;
    arrayCopy(States, PrevStates);
    plotStates();
    //delay(100);
  }  
  else if (Read == false){
    BWrite.disableStyle();
    fill(255,200,0);
    strokeWeight(0.5);
    shape(BWrite, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
    fill(255,255,255);
    shape(BRead, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
    WriteU();
    plotStates();
  }
  
  
  if (ClickButtonRead()==true) {
    if (Read == true && Click == false) {
      Read = false;
    }  
    else if (Read == false && Click == false){
      Read = true;
    }
    Click = true;
  }  
  if (mousePressed == false){
    Click = false;
  }  
}

boolean ClickButtonRead(){
  return(mouseX>=sc*(0.101*displayWidth) && mouseX<=sc*(0.197*displayWidth) && mouseY>=sc*(0.038*displayHeight) && mouseY<=sc*(0.058*displayHeight) && mousePressed == true);
}
boolean ClickButC(){
  //fill(0,0,250);
  //rect(0.057*displayWidth,0.1752*displayHeight,0.0183*displayWidth,0.0175*displayHeight);
  //shape(ButC,  0, -displayHeight/5.5, displayWidth, displayWidth/2);
  return(mouseX>=sc*(0.057*displayWidth) && mouseX<=sc*((0.0753)*(displayWidth)) && mouseY>=sc*((0.1752)*(displayHeight)) && mouseY<=sc*((0.1927)*(displayHeight))&& mousePressed == true);
}
boolean ClickButD(){
  //fill(0,0,200);
  //rect(0.0773*displayWidth,0.1752*displayHeight,0.0183*displayWidth,0.0175*displayHeight);
  //shape(ButD,  0, -displayHeight/5.5, displayWidth, displayWidth/2);
  return(mouseX>=sc*(0.0773*displayWidth) && mouseX<=sc*((0.0956)*(displayWidth)) && mouseY>=sc*((0.1752)*(displayHeight)) && mouseY<=sc*((0.1927)*(displayHeight))&& mousePressed == true);
}
boolean ClickButE(){
  //fill(0,0,180);
  //rect(0.0976*displayWidth,0.1752*displayHeight,0.0183*displayWidth,0.0175*displayHeight);
  //shape(ButE,  0, -displayHeight/5.5, displayWidth, displayWidth/2);
  return(mouseX>=sc*(0.0906*displayWidth) && mouseX<=sc*((0.1159)*(displayWidth)) && mouseY>=sc*((0.1752)*(displayHeight)) && mouseY<=sc*((0.1927)*(displayHeight))&& mousePressed == true);
}
boolean ClickButF(){
  //fill(0,0,160);
  //rect(0.1179*displayWidth,0.1752*displayHeight,0.0183*displayWidth,0.0175*displayHeight);
  //shape(ButF,  0, -displayHeight/5.5, displayWidth, displayWidth/2);
  return(mouseX>=sc*(0.1109*displayWidth) && mouseX<=sc*((0.1362)*(displayWidth)) && mouseY>=sc*((0.1752)*(displayHeight)) && mouseY<=sc*((0.1927)*(displayHeight))&& mousePressed == true);
}
boolean ClickButG(){
  //fill(0,0,140);
  //rect(0.1382*displayWidth,0.1752*displayHeight,0.0183*displayWidth,0.0175*displayHeight);
  //shape(ButG,  0, -displayHeight/5.5, displayWidth, displayWidth/2);
  return(mouseX>=sc*(0.1312*displayWidth) && mouseX<=sc*((0.1565)*(displayWidth)) && mouseY>=sc*((0.1752)*(displayHeight)) && mouseY<=sc*((0.1927)*(displayHeight))&& mousePressed == true);
}
boolean ClickButA(){
  //fill(0,0,120);
  //rect(0.1585*displayWidth,0.1752*displayHeight,0.0183*displayWidth,0.0175*displayHeight);
  //shape(ButA,  0, -displayHeight/5.5, displayWidth, displayWidth/2);
  return(mouseX>=sc*(0.1515*displayWidth) && mouseX<=sc*((0.1768)*(displayWidth)) && mouseY>=sc*((0.1752)*(displayHeight)) && mouseY<=sc*((0.1927)*(displayHeight))&& mousePressed == true);
}
boolean ClickButB(){
  //fill(0,0,110);
  //rect(0.1788*displayWidth,0.1752*displayHeight,0.0183*displayWidth,0.0175*displayHeight);
  //shape(ButB,  0, -displayHeight/5.5, displayWidth, displayWidth/2);
  return(mouseX>=sc*(0.1718*displayWidth) && mouseX<=sc*((0.1971)*(displayWidth)) && mouseY>=sc*((0.1752)*(displayHeight)) && mouseY<=sc*((0.1927)*(displayHeight))&& mousePressed == true);
}
boolean ClickButM(){
  //fill(0,0,250);
  //rect(0.057*displayWidth,0.1957*displayHeight,0.0183*displayWidth,0.0175*displayHeight);
  //shape(ButM,  0, -displayHeight/5.5, displayWidth, displayWidth/2);
  return(mouseX>=sc*(0.057*displayWidth) && mouseX<=sc*((0.0753)*(displayWidth)) && mouseY>=sc*((0.1957)*(displayHeight)) && mouseY<=sc*((0.2132)*(displayHeight))&& mousePressed == true);
}
boolean ClickBut7(){
  //fill(0,0,200);
  //rect(0.0773*displayWidth,0.1957*displayHeight,0.0183*displayWidth,0.0175*displayHeight);
  //shape(But7,  0, -displayHeight/5.5, displayWidth, displayWidth/2);
  return(mouseX>=sc*(0.0773*displayWidth) && mouseX<=sc*((0.0956)*(displayWidth)) && mouseY>=sc*((0.1957)*(displayHeight)) && mouseY<=sc*((0.2132)*(displayHeight))&& mousePressed == true);
}
boolean ClickButM7(){
  //fill(0,0,200);
  //rect(0.0976*displayWidth,0.1957*displayHeight,0.0183*displayWidth,0.0175*displayHeight);
  //shape(ButM7,  0, -displayHeight/5.5, displayWidth, displayWidth/2);
  return(mouseX>=sc*(0.0906*displayWidth) && mouseX<=sc*((0.1159)*(displayWidth)) && mouseY>=sc*((0.1957)*(displayHeight)) && mouseY<=sc*((0.2132)*(displayHeight))&& mousePressed == true);
}
boolean ClickButSos(){
  //fill(0,0,200);
  //rect(0.1382*displayWidth,0.1957*displayHeight,0.0183*displayWidth,0.0175*displayHeight);
  //shape(ButSos,  0, -displayHeight/5.5, displayWidth, displayWidth/2);
  return(mouseX>=sc*(0.1312*displayWidth) && mouseX<=sc*((0.1565)*(displayWidth)) && mouseY>=sc*((0.1957)*(displayHeight)) && mouseY<=sc*((0.2132)*(displayHeight))&& mousePressed == true);
}

void ReadU(){
  //PrevStates=States;
  if (mySerial.available() > 0) {
    //PrevStates = States;
    //DevRead = mySerial.readStringUntil('\n');
    ////println(DevRead);
    //if (DevRead != null){
    //  int index=DevRead.indexOf(" ");
    //  String device=DevRead.substring(0,index);
    //  String state=DevRead.substring(index+1,DevRead.length()-1);
    //  States[int(device)-1]=int(float(state));
    //  //println(States);
    //  //delay(500);
    int lec = mySerial.read();
    int DeviceRead = int(lec >> 4);
    int StateRead = int(lec & 15);
    if (DeviceRead < States.length){
      States[DeviceRead] = StateRead;
    }
  }
  else {
    //println("ELSE");
    mySerial.write("r");
    mySerial.write('\n');
  }  
}  


void WriteU(){
  //print("Writing");
  checkButtons();
  paintButtons();
  if (BC == false && BD == false && BE == false && BF == false && BG == false && BA == false && BB == false){
    for (int i=0; i<States.length; i++){
      States[i]=0;
    }
  }
  //------------------------------------------------------------------------------
  // C Chords
  
  
  if (BC == true && BM == false && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 2){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BC == true && BM == true && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 2){
        States[i] = 7;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BC == true && BM == false && B7 == true && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BC == true && BM == false && B7 == false && BM7 == true && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 2){
        States[i] = 15;
      }
      else{
        States[i] = 0;
      }
    }
  }
  //------------------------------------------------------------------------------
  //C# Chords
  
  if (BC == true && BM == false && B7 == false && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 14;
      }
      else if (i == 3){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BC == true && BM == true && B7 == false && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 12;
      }
      else if (i == 3){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BC == true && BM == false && B7 == true && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 14;
      }
      else if (i == 1){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BC == true && BM == false && B7 == false && BM7 == true && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 12;
      }
      else if (i == 1){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  //------------------------------------------------------------------------------
  //D Chords
  
  
  if (BD == true && BM == false && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 14;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BD == true && BM == true && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 2;
      }
      else if (i == 1){
        States[i] = 12;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BD == true && BM == false && B7 == true && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 14;
      }
      else if (i == 2){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BD == true && BM == false && B7 == false && BM7 == true && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 2;
      }
      else if (i == 1){
        States[i] = 12;
      }
      else if (i == 2){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  //------------------------------------------------------------------------------
  // D# Chords
  
  if (BD == true && BM == false && B7 == false && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 1;
      }
      else if (i == 2){
        States[i] = 6;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BD == true && BM == true && B7 == false && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 1;
      }
      else if (i == 1){
        States[i] = 2;
      }
      else if (i == 3){
        States[i] = 12;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BD == true && BM == false && B7 == true && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 2){
        States[i] = 14;
      }
      else if (i == 3){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BD == true && BM == false && B7 == false && BM7 == true && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 2;
      }
      else if (i == 2){
        States[i] = 12;
      }
      else if (i == 3){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  //----------------------------------------------------------------------------
  //E Chords
  
  if (BE == true && BM == false && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 8;
      }
      else if (i == 1){
        States[i] = 1;
      }
      else if (i == 3){
        States[i] = 4;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BE == true && BM == true && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 1;
      }
      else if (i == 2){
        States[i] = 2;
      }
      else if (i == 3){
        States[i] = 4;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BE == true && BM == false && B7 == true && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 8;
      }
      else if (i == 1){
        States[i] = 5;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BE == true && BM == false && B7 == false && BM7 == true && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 5;
      }
      else{
        States[i] = 0;
      }
    }
  }
  //------------------------------------------------------------------------------
  //F Chords
  
  
  if (BF == true && BM == false && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 2;
      }
      else if (i == 1){
        States[i] = 8;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BF == true && BM == true && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 10;
      }
      else if (i == 2){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BF == true && BM == false && B7 == true && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 2;
      }
      else if (i == 1){
        States[i] = 8;
      }
      else if (i == 2){
        States[i] = 5;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BF == true && BM == false && B7 == false && BM7 == true && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 10;
      }
      else if (i == 2){
        States[i] = 5;
      }
      else{
        States[i] = 0;
      }
    }
  }
  //------------------------------------------------------------------------------
  // F# Chords
  
  if (BF == true && BM == false && B7 == false && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 5;
      }
      else if (i == 1){
        States[i] = 2;
      }
      else if (i == 2){
        States[i] = 8;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BF == true && BM == true && B7 == false && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 4;
      }
      else if (i == 1){
        States[i] = 10;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BF == true && BM == false && B7 == true && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 2;
      }
      else if (i == 2){
        States[i] = 8;
      }
      else if (i == 3){
        States[i] = 5;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BF == true && BM == false && B7 == false && BM7 == true && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 10;
      }
      else if (i == 3){
        States[i] = 5;
      }
      else{
        States[i] = 0;
      }
    }
  }
  //------------------------------------------------------------------------------
  //G Chords
  
  
  if (BG == true && BM == false && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 5;
      }
      else if (i == 2){
        States[i] = 2;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BG == true && BM == true && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 1;
      }
      else if (i == 1){
        States[i] = 4;
      }
      else if (i == 2){
        States[i] = 2;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BG == true && BM == false && B7 == true && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 2;
      }
      else if (i == 1){
        States[i] = 5;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BG == true && BM == false && B7 == false && BM7 == true && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 3;
      }
      else if (i == 1){
        States[i] = 4;
      }
      else{
        States[i] = 0;
      }
    }
  }
  //------------------------------------------------------------------------------
  // G# Chords
  
  if (BG == true && BM == false && B7 == false && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 2){
        States[i] = 5;
      }
      else if (i == 3){
        States[i] = 2;
      }
      else if (i == 4){
        States[i] = 8;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BF == true && BM == true && B7 == false && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 1;
      }
      else if (i == 2){
        States[i] = 4;
      }
      else if (i == 3){
        States[i] = 10;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BG == true && BM == false && B7 == true && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 8;
      }
      else if (i == 1){
        States[i] = 2;
      }
      else if (i == 2){
        States[i] = 5;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BG == true && BM == false && B7 == false && BM7 == true && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 8;
      }
      else if (i == 1){
        States[i] = 3;
      }
      else if (i == 2){
        States[i] = 4;
      }
      else{
        States[i] = 0;
      }
    }
  }
    //------------------------------------------------------------------------------
  //A Chords
  
  
  if (BA == true && BM == false && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 4;
      }
      else if (i == 1){
        States[i] = 8;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BA == true && BM == true && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 8;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BA == true && BM == false && B7 == true && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 4;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BA == true && BM == false && B7 == false && BM7 == true && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 0;
      }
      else{
        States[i] = 0;
      }
    }
  }
  //------------------------------------------------------------------------------
  // A# Chords
  
  if (BA == true && BM == false && B7 == false && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 3;
      }
      else if (i == 1){
        States[i] = 4;
      }
      else if (i == 2){
        States[i] = 8;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BA == true && BM == true && B7 == false && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 7;
      }
      else if (i == 2){
        States[i] = 8;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BA == true && BM == false && B7 == true && BM7 == false && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 11;
      }
      else if (i == 1){
        States[i] = 4;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BA == true && BM == false && B7 == false && BM7 == true && BSos == true){
    for (int i=0; i<States.length; i++){
      if (i == 0){
        States[i] = 15;
      }
      else{
        States[i] = 0;
      }
    }
  }
      //------------------------------------------------------------------------------
  //B Chords
  
  
  if (BB == true && BM == false && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 3;
      }
      else if (i == 2){
        States[i] = 4;
      }
      else if (i == 3){
        States[i] = 8;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BB == true && BM == true && B7 == false && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 7;
      }
      if (i == 3){
        States[i] = 8;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BB == true && BM == false && B7 == true && BM7 == false && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 11;
      }
      if (i == 2){
        States[i] = 4;
      }
      else{
        States[i] = 0;
      }
    }
  }
  if (BB == true && BM == false && B7 == false && BM7 == true && BSos == false){
    for (int i=0; i<States.length; i++){
      if (i == 1){
        States[i] = 1;
      }
      else{
        States[i] = 0;
      }
    }
  }
  
  
  mySerial.write("w");
  mySerial.write('\n');
  mySerial.write(States[0]+"  "+States[1]+"   "+States[2]+"   "+States[3]+"   "+States[4]);
}

void checkButtons(){
  if (ClickButC()==true) {
    if (BC == true && Click == false) {
      BC = false;
    }  
    else if (BC == false && Click == false){
      BC = true;
      BD = false;
      BE = false;
      BF = false;
      BG = false;
      BA = false;
      BB = false;
    }
    Click = true;
  }    
  if (ClickButD()==true) {
    if (BD == true && Click == false) {
      BD = false;
    }  
    else if (BD == false && Click == false){
      BD = true;
      BC = false;
      BE = false;
      BF = false;
      BG = false;
      BA = false;
      BB = false;
    }
    Click = true;
  } 
  if (ClickButE()==true) {
    if (BE == true && Click == false) {
      BE = false;
    }  
    else if (BE == false && Click == false){
      BE = true;
      BD = false;
      BC = false;
      BF = false;
      BG = false;
      BA = false;
      BB = false;
      BSos = false;
    }
    Click = true;
  } 
  if (ClickButF()==true) {
    if (BF == true && Click == false) {
      BF = false;
    }  
    else if (BF == false && Click == false){
      BF = true;
      BD = false;
      BE = false;
      BC = false;
      BG = false;
      BA = false;
      BB = false;
    }
    Click = true;
  } 
  if (ClickButG()==true) {
    if (BG == true && Click == false) {
      BG = false;
    }  
    else if (BG == false && Click == false){
      BG = true;
      BD = false;
      BE = false;
      BF = false;
      BC = false;
      BA = false;
      BB = false;
    }
    Click = true;
  } 
  if (ClickButA()==true) {
    if (BA == true && Click == false) {
      BA = false;
    }  
    else if (BA == false && Click == false){
      BA = true;
      BD = false;
      BE = false;
      BF = false;
      BG = false;
      BC = false;
      BB = false;
    }
    Click = true;
  } 
  if (ClickButB()==true) {
    if (BB == true && Click == false) {
      BB = false;
    }  
    else if (BB == false && Click == false){
      BB = true;
      BD = false;
      BE = false;
      BF = false;
      BG = false;
      BA = false;
      BC = false;
      BSos = false;
    }
    Click = true;
  } 
  if (ClickButM()==true) {
    if (BM == true && Click == false) {
      BM = false;
    }  
    else if (BM == false && Click == false){
      BM = true;
      B7 = false;
      BM7 = false;
    }
    Click = true;
  } 
  if (ClickBut7()==true) {
    if (B7 == true && Click == false) {
      B7 = false;
    }  
    else if (B7 == false && Click == false){
      B7 = true;
      BM = false;
      BM7 = false;
    }
    Click = true;
  } 
  if (ClickButM7()==true) {
    if (BM7 == true && Click == false) {
      BM7 = false;
    }  
    else if (BM7 == false && Click == false){
      BM7 = true;
      B7 = false;
      BM = false;
    }
    Click = true;
  } 
  if (ClickButSos()==true) {
    if (BSos == true && Click == false) {
      BSos = false;
    }  
    else if (BSos == false && Click == false && BE == false && BB == false){
      BSos = true;
    }
    Click = true;
  } 
}

void paintButtons(){
  ButC.disableStyle();
  ButD.disableStyle();
  ButE.disableStyle();
  ButF.disableStyle();
  ButG.disableStyle();
  ButA.disableStyle();
  ButB.disableStyle();
  ButM.disableStyle();
  But7.disableStyle();
  ButM7.disableStyle();
  ButSos.disableStyle();
  if (BC == true) {
    fill(255,200,0);
    shape(ButC,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if(BC == false){
    fill(255,255,255);
    shape(ButC,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  
 if (BD == true) {
    fill(255,200,0);
    shape(ButD,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if(BD == false){
    fill(255,255,255);
    shape(ButD,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  if (BE == true) {
    fill(255,200,0);
    shape(ButE,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if(BE == false){
    fill(255,255,255);
    shape(ButE,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  if (BF == true) {
    fill(255,200,0);
    shape(ButF,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if(BF == false){
    fill(255,255,255);
    shape(ButF,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  if (BG == true) {
    fill(255,200,0);
    shape(ButG,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if(BG == false){
    fill(255,255,255);
    shape(ButG,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  if (BA == true) {
    fill(255,200,0);
    shape(ButA,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if(BA == false){
    fill(255,255,255);
    shape(ButA,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  if (BB == true) {
    fill(255,200,0);
    shape(ButB,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if(BB == false){
    fill(255,255,255);
    shape(ButB,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  if (BM == true) {
    fill(255,200,0);
    shape(ButM,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if(BM == false){
    fill(255,255,255);
    shape(ButM,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  if (B7 == true) {
    fill(255,200,0);
    shape(But7,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if(B7 == false){
    fill(255,255,255);
    shape(But7,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  if (BM7 == true) {
    fill(255,200,0);
    shape(ButM7,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if(BM7 == false){
    fill(255,255,255);
    shape(ButM7,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  if (BSos == true) {
    fill(255,200,0);
    shape(ButSos,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if(BSos == false){
    fill(255,255,255);
    shape(ButSos,0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  
}



void plotStates() {
  if ((States[5] & unbinary("0001")) > 0){
    one6.disableStyle();
    fill(255, 51, 102);
    shape(one6, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[4] & unbinary("0001")) > 0){
    one5.disableStyle();
    fill(255, 51, 102);
    shape(one5, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[3] & unbinary("0001")) > 0){
    one4.disableStyle();
    fill(255, 51, 102);
    shape(one4, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[2] & unbinary("0001")) > 0){
    one3.disableStyle();
    fill(255, 51, 102);
    shape(one3, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[1] & unbinary("0001")) > 0){
    one2.disableStyle();
    fill(255, 51, 102);
    shape(one2, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[0] & unbinary("0001")) > 0){
    one1.disableStyle();
    fill(255, 51, 102);
    shape(one1, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }

  if ((States[5] & unbinary("0010")) > 0){
    two6.disableStyle();
    fill(255, 51, 102);
    shape(two6, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[4] & unbinary("0010")) > 0){
    two5.disableStyle();
    fill(255, 51, 102);
    shape(two5, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[3] & unbinary("0010")) > 0){
    two4.disableStyle();
    fill(255, 51, 102);
    shape(two4, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[2] & unbinary("0010")) > 0){
    two3.disableStyle();
    fill(255, 51, 102);
    shape(two3, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[1] & unbinary("0010")) > 0){
    two2.disableStyle();
    fill(255, 51, 102);
    shape(two2, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[0] & unbinary("0010")) > 0){
    two1.disableStyle();
    fill(255, 51, 102);
    shape(two1, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }

  if ((States[5] & unbinary("0100")) > 0){
    three6.disableStyle();
    fill(255, 51, 102);
    shape(three6, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[4] & unbinary("0100")) > 0){
    three5.disableStyle();
    fill(255, 51, 102);
    shape(three5, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[3] & unbinary("0100")) > 0){
    three4.disableStyle();
    fill(255, 51, 102);
    shape(three4, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[2] & unbinary("0100")) > 0){
    three3.disableStyle();
    fill(255, 51, 102);
    shape(three3, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[1] & unbinary("0100")) > 0){
    three2.disableStyle();
    fill(255, 51, 102);
    shape(three2, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[0] & unbinary("0100")) > 0){
    three1.disableStyle();
    fill(255, 51, 102);
    shape(three1, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }

  if ((States[5] & unbinary("1000")) > 0){
    four6.disableStyle();
    fill(255, 51, 102);
    shape(four6, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[4] & unbinary("1000")) > 0){
    four5.disableStyle();
    fill(255, 51, 102);
    shape(four5, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[3] & unbinary("1000")) > 0){
    four4.disableStyle();
    fill(255, 51, 102);
    shape(four4, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[2] & unbinary("1000")) > 0){
    four3.disableStyle();
    fill(255, 51, 102);
    shape(four3, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[1] & unbinary("1000")) > 0){
    four2.disableStyle();
    fill(255, 51, 102);
    shape(four2, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }
  else if ((States[0] & unbinary("1000")) > 0){
    four1.disableStyle();
    fill(255, 51, 102);
    shape(four1, 0, -displayHeight/5.5, displayWidth, displayWidth/2);
  }  
}  

void MIDIU(){
  ReadStrings();
  ReadPrevStrings();
  println(States);
  println(PrevStates);
  println(" ");
  if (Strings[0] != PrevStrings[0]){
    if (PrevStrings[0] == 0){
      myBus.sendNoteOn(0, (pitch1 + Strings[0]), velocity);
    }
    else if (PrevStrings[0] != 0){
      myBus.sendNoteOff(0, (pitch1 + PrevStrings[0]), velocity);
      if (Strings[0] != 0){
        myBus.sendNoteOn(0, (pitch1 + Strings[0]), velocity);
      }
    }
  }  
  
  if (Strings[1] != PrevStrings[1]){
    if (PrevStrings[1] == 0){
      myBus.sendNoteOn(0, (pitch2 + Strings[1]), velocity);
    }
    else if (PrevStrings[1] != 0){
      myBus.sendNoteOff(0, (pitch2 + PrevStrings[1]), velocity);
      if (Strings[1] != 0){
        myBus.sendNoteOn(0, (pitch2 + Strings[1]), velocity);
      }
    }
  }  
  
  if (Strings[2] != PrevStrings[2]){
    if (PrevStrings[2] == 0){
      myBus.sendNoteOn(0, (pitch3 + Strings[2]), velocity);
    }
    else if (PrevStrings[2] != 0){
      myBus.sendNoteOff(0, (pitch3 + PrevStrings[2]), velocity);
      if (Strings[2] != 0){
        myBus.sendNoteOn(0, (pitch3 + Strings[2]), velocity);
      }
    }
  }  
  
  if (Strings[3] != PrevStrings[3]){
    if (PrevStrings[3] == 0){
      myBus.sendNoteOn(0, (pitch4 + Strings[3]), velocity);
    }
    else if (PrevStrings[3] != 0){
      myBus.sendNoteOff(0, (pitch4 + PrevStrings[3]), velocity);
      if (Strings[3] != 0){
        myBus.sendNoteOn(0, (pitch4 + Strings[3]), velocity);
      }
    }
  }  
}  

void ReadStrings(){
  if ((States[5] & unbinary("0001")) > 0){
    Strings[0] = 6;
  }
  else if ((States[4] & unbinary("0001")) > 0){
    Strings[0] = 5;
  }
  else if ((States[3] & unbinary("0001")) > 0){
    Strings[0] = 4;
  }
  else if ((States[2] & unbinary("0001")) > 0){
    Strings[0] = 3;
  }
  else if ((States[1] & unbinary("0001")) > 0){
    Strings[0] = 2;
  }
  else if ((States[0] & unbinary("0001")) > 0){
    Strings[0] = 1;
  }
  else {
    Strings[0] = 0;
  }



  if ((States[5] & unbinary("0010")) > 0){
    Strings[1] = 6;
  }
  else if ((States[4] & unbinary("0010")) > 0){
    Strings[1] = 5;
  }
  else if ((States[3] & unbinary("0010")) > 0){
    Strings[1] = 4;
  }
  else if ((States[2] & unbinary("0010")) > 0){
    Strings[1] = 3;
  }
  else if ((States[1] & unbinary("0010")) > 0){
    Strings[1] = 2;
  }
  else if ((States[0] & unbinary("0010")) > 0){
    Strings[1] = 1;
  }
  else {
    Strings[1] = 0;
  }
  
  
  
  if ((States[5] & unbinary("0100")) > 0){
    Strings[2] = 6;
  }
  else if ((States[4] & unbinary("0100")) > 0){
    Strings[2] = 5;
  }
  else if ((States[3] & unbinary("0100")) > 0){
    Strings[2] = 4;
  }
  else if ((States[2] & unbinary("0100")) > 0){
    Strings[2] = 3;
  }
  else if ((States[1] & unbinary("0100")) > 0){
    Strings[2] = 2;
  }
  else if ((States[0] & unbinary("0100")) > 0){
    Strings[2] = 1;
  }
  else {
    Strings[2] = 0;
  }
  
  
  
  if ((States[5] & unbinary("1000")) > 0){
    Strings[3] = 6;
  }
  else if ((States[4] & unbinary("1000")) > 0){
    Strings[3] = 5;
  }
  else if ((States[3] & unbinary("1000")) > 0){
    Strings[3] = 4;
  }
  else if ((States[2] & unbinary("1000")) > 0){
    Strings[3] = 3;
  }
  else if ((States[1] & unbinary("1000")) > 0){
    Strings[3] = 2;
  }
  else if ((States[0] & unbinary("1000")) > 0){
    Strings[3] = 1;
  }
  else {
    Strings[3] = 0;
  }
}


void ReadPrevStrings(){
  if ((PrevStates[5] & unbinary("0001")) > 0){
    PrevStrings[0] = 6;
  }
  else if ((PrevStates[4] & unbinary("0001")) > 0){
    PrevStrings[0] = 5;
  }
  else if ((PrevStates[3] & unbinary("0001")) > 0){
    PrevStrings[0] = 4;
  }
  else if ((PrevStates[2] & unbinary("0001")) > 0){
    PrevStrings[0] = 3;
  }
  else if ((PrevStates[1] & unbinary("0001")) > 0){
    PrevStrings[0] = 2;
  }
  else if ((PrevStates[0] & unbinary("0001")) > 0){
    PrevStrings[0] = 1;
  }
  else {
    PrevStrings[0] = 0;
  }



  if ((PrevStates[5] & unbinary("0010")) > 0){
    PrevStrings[1] = 6;
  }
  else if ((PrevStates[4] & unbinary("0010")) > 0){
    PrevStrings[1] = 5;
  }
  else if ((PrevStates[3] & unbinary("0010")) > 0){
    PrevStrings[1] = 4;
  }
  else if ((PrevStates[2] & unbinary("0010")) > 0){
    PrevStrings[1] = 3;
  }
  else if ((PrevStates[1] & unbinary("0010")) > 0){
    PrevStrings[1] = 2;
  }
  else if ((PrevStates[0] & unbinary("0010")) > 0){
    PrevStrings[1] = 1;
  }
  else {
    PrevStrings[1] = 0;
  }
  
  
  
  if ((PrevStates[5] & unbinary("0100")) > 0){
    PrevStrings[2] = 6;
  }
  else if ((PrevStates[4] & unbinary("0100")) > 0){
    PrevStrings[2] = 5;
  }
  else if ((PrevStates[3] & unbinary("0100")) > 0){
    PrevStrings[2] = 4;
  }
  else if ((PrevStates[2] & unbinary("0100")) > 0){
    PrevStrings[2] = 3;
  }
  else if ((PrevStates[1] & unbinary("0100")) > 0){
    PrevStrings[2] = 2;
  }
  else if ((PrevStates[0] & unbinary("0100")) > 0){
    PrevStrings[2] = 1;
  }
  else {
    PrevStrings[2] = 0;
  }
  
  
  
  if ((PrevStates[5] & unbinary("1000")) > 0){
    PrevStrings[3] = 6;
  }
  else if ((PrevStates[4] & unbinary("1000")) > 0){
    PrevStrings[3] = 5;
  }
  else if ((PrevStates[3] & unbinary("1000")) > 0){
    PrevStrings[3] = 4;
  }
  else if ((PrevStates[2] & unbinary("1000")) > 0){
    PrevStrings[3] = 3;
  }
  else if ((PrevStates[1] & unbinary("1000")) > 0){
    PrevStrings[3] = 2;
  }
  else if ((PrevStates[0] & unbinary("1000")) > 0){
    PrevStrings[3] = 1;
  }
  else {
    PrevStrings[3] = 0;
  }
}


Download files here.