Code umwandeln Arduino

Hallo,
gibt es eine einfache Möglichkeit Sketche, die in Visual geschrieben wurden, so zu bearbeiten, dass es in der Arduino IDE keine Probleme beim kompilieren gibt ?

Ich habe einen alten Code von Github, den ich in der Arduino IDE etwas erweitern möchte.
Aber die IDE kommt mit etlichen Befehlen nicht zurecht.

Danke vorab.

Ja, Sie nehmen den Code und entfernen die Fehler... :woozy_face:

Visual was ?
Und ja, die Lösung wurde schon geschrieben.

Visual Fortran / Basic / C?

Gruß Tommy

Ich glaube es müsste C sein.
Soll ich mal den Code hier einstellen bzw Teile davon ?
Er ist sehr lang.
Und beim kompilieren gibt es diverse Fehler.

Ja...

Haben Sie dieses

Teensyduino - Add-on for Arduino IDE to use Teensy USB development board

Das Board habe ich noch nicht.
Aber ich habe schon Teensyduino installiert um die ganze Boardpalette in der IDE zu haben.

Wenn ich 'VERIFY' mache kommt ein Fehler nach dem anderen als Bsp:

PROGMEMM U_char
usbMIDI was not declared in this script

und mehr...

Es sollte ziemlich einfach sein:

  • Ändere alle prog_uchar in const unsigned char.
  • "usbMIDI not in scope" bedeutet, dass du auf "Tools > USB Type" klicken musst, um den USB als MIDI zu konfigurieren.

image

Super.Danke vorab.Mache ich mal gleich und dann schaue ich mal weiter.
Dankesehr

C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:211:28: error: expected unqualified-id before '.' token
 PROGMEM const unsigned char. sampleData[5338] = {
                            ^
In file included from C:\Users\User\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy/WString.h:29:0,
                 from C:\Users\User\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy/Print.h:27,
                 from C:\Users\User\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy/Stream.h:24,
                 from c:\users\user\appdata\local\arduino15\packages\teensy\hardware\avr\1.58.1\cores\usb_midi\usb_api.h:6,
                 from C:\Users\User\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy/usb_api.h:10,
                 from C:\Users\User\AppData\Local\Arduino15\packages\teensy\hardware\avr\1.58.1\cores\teensy/WProgram.h:22,
                 from C:\Users\User\AppData\Local\Temp\arduino\sketches\7FA95C5F5EEA2BCEEB29CD9338F8A608\pch\Arduino.h:1:
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino: In function 'void mainSampleEngine(byte, byte, byte, long int)':
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:614:7: error: 'sampleData' was not declared in this scope
       sampleData + (sampleStarts[sampleNumber]) + sampleCounter);
       ^
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:614:7: note: suggested alternative: 'sampleStarts'
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:589:14: warning: unused variable 'sampleCounterIncrement' [-Wunused-variable]
  static byte sampleCounterIncrement = 1;
              ^~~~~~~~~~~~~~~~~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino: In function 'void mainPolyEngine(byte, byte, byte)':
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:676:17: warning: unused variable 'decayOn' [-Wunused-variable]
  static boolean decayOn = LOW;
                 ^~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:678:22: warning: unused variable 'decayInterval' [-Wunused-variable]
  static unsigned int decayInterval;
                      ^~~~~~~~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:679:23: warning: unused variable 'previousDecayMicros' [-Wunused-variable]
  static unsigned long previousDecayMicros;
                       ^~~~~~~~~~~~~~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:684:17: warning: unused variable 'tremOn' [-Wunused-variable]
  static boolean tremOn = LOW;
                 ^~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino: In function 'void mainLeadOneEngine(byte, byte, float, boolean, byte, long unsigned int)':
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:1115:15: warning: unused variable 'vibFactor' [-Wunused-variable]
  static float vibFactor = 1.0;
               ^~~~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino: In function 'void mainLeadTwoEngine(byte, byte, float, boolean, byte, long unsigned int)':
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:1562:15: warning: unused variable 'vibFactor' [-Wunused-variable]
  static float vibFactor = 1.0;
               ^~~~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino: In function 'void mainLeadThreeEngine(byte, byte, float, boolean, byte, long unsigned int)':
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:2006:15: warning: unused variable 'vibFactor' [-Wunused-variable]
  static float vibFactor = 1.0;
               ^~~~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino: At global scope:
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:2006:15: warning: 'vibFactor' defined but not used [-Wunused-variable]
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:1562:15: warning: 'vibFactor' defined but not used [-Wunused-variable]
  static float vibFactor = 1.0;
               ^~~~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:1115:15: warning: 'vibFactor' defined but not used [-Wunused-variable]
  static float vibFactor = 1.0;
               ^~~~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:684:17: warning: 'tremOn' defined but not used [-Wunused-variable]
  static boolean tremOn = LOW;
                 ^~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:679:23: warning: 'previousDecayMicros' defined but not used [-Wunused-variable]
  static unsigned long previousDecayMicros;
                       ^~~~~~~~~~~~~~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:678:22: warning: 'decayInterval' defined but not used [-Wunused-variable]
  static unsigned int decayInterval;
                      ^~~~~~~~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:676:17: warning: 'decayOn' defined but not used [-Wunused-variable]
  static boolean decayOn = LOW;
                 ^~~~~~~
C:\Users\User\Desktop\Neuer Ordner\SID_Synth_1_01\SID_Synth_1_01.ino:589:14: warning: 'sampleCounterIncrement' defined but not used [-Wunused-variable]
  static byte sampleCounterIncrement = 1;
              ^~~~~~~~~~~~~~~~~~~~~~

exit status 1

Compilation error: expected unqualified-id before '.' token

Warum haben Sie da einen Punkt?

image

Hi
bis auf einen Fehler gibt es beim compilen bis jetzt keine Fehler.
1 Fehler besteht noch und ich komme nicht weiter:
'prog_uchar' does not name a type;did u mean 'getchar'

PROGMEM prog_uchar sampleData[5338] = { 0,0,46,64 usw usw}

Verschiedene Ansätze im Web brachten nichts.

Dankeschön

Wo kommt denn das her?

Das war doch vorher anders:

???

Was ist das?
Wo kommt das her?

Gehirn einschalten klappt auch nicht?

Das ist ein kopierter Sketch und wohl eine veraltete Schreibweise.

Sobald ich da in char ändere oder const davor setze, dann tun sich an anderer Stelle Probleme auf.

Mein Rat:
Kundig machen!

Tipp, für den Anfang: prog_uchar gibts wohl nur für den AVR
Hast du da einen AVR?
Nein!
Aua.


/**
   \ingroup avr_pgmspace
   \typedef prog_uchar
   \note DEPRECATED

   This typedef is now deprecated because the usage of the __progmem__ 
   attribute on a type is not supported in GCC. However, the use of the 
   __progmem__ attribute on a variable declaration is supported, and this is 
   now the recommended usage.

   The typedef is only visible if the macro __PROG_TYPES_COMPAT__
   has been defined before including <avr/pgmspace.h> (either by a
   \c \#define directive, or by a -D compiler option.)

   Type of an "unsigned char" object located in flash ROM.
*/
typedef unsigned char PROGMEM prog_uchar;

Zu den "anderen Problemen" kann ich nix sagen.
Da die scheinbar geheim bleiben müssen.

Hier mal der Sketch.
Geht um die Steuerung eines MOS6581 via Midi.

unsigned long clock = 1843200;
byte transferHoldTime = 4;
byte bendRange = 12;  // Default bend range
boolean noVelocity = LOW; // Defaults all velocities to 127
const float vibRateFactor = 1.0;
const float vibDepth = 0.0004;
const byte CSchip0pin = 9;

boolean filterInclusionOne = LOW;
boolean filterInclusionTwo = LOW;
boolean filterInclusionThree = LOW;




//////////////////////////////////////////
//  PCM AUDIO DATA
//////////////////////////////////////////


// 4ks/s, 8 Bit Audio (although 8 bit is converted to 4 bit for playback)
PROGMEM prog_uchar sampleData[5338] = {
  0, 0, 46, 64, 40, 0, 209, 255, 255, 255, 255, 197, 226, 224, 255, 255, 255, 215, 171, 165, 200, 215, 189, 206, 215, 194, 165, 142, 2, 0, 0, 0, 0, 72, 0, 0, 0, 0, 0, 23, 26, 14, 0, 0, 0, 43, 37, 0, 0, 34, 69, 151, 229, 125, 128, 148, 160, 197, 255, 255, 255, 255, 174, 107, 125, 139, 183, 69, 162, 229, 255, 241, 142, 136, 186, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 244, 197, 206, 189, 157, 136, 194, 107, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 75, 148, 162, 174, 238, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 235, 215, 203, 180, 171, 151, 130, 104, 75, 40, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 14, 40, 58, 87, 136, 136, 113, 128, 177, 206, 206, 218, 244, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 235, 226, 157, 110, 119, 122, 107, 93, 72, 69, 49, 52, 66, 55, 52, 46, 26, 14, 20, 29, 34, 61, 84, 96, 107, 122, 133, 154, 174, 183, 209, 221, 244, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 250, 226, 203, 177, 174, 160, 145, 125, 96, 90, 78, 66, 58, 43, 23, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 43, 69, 90, 113, 139, 154, 183, 194, 218, 229, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 250, 238, 226, 221, 206, 200, 189, 174, 168, 151, 142, 133, 119, 113, 101, 90, 84, 75, 72, 66, 61, 61, 58, 61, 58, 64, 69, 75, 78, 87, 98, 110, 119, 130, 145, 154, 174, 183, 209, 224, 232, 244, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 250, 241, 224, 215, 192, 183, 165, 151, 133, 116, 96, 81, 58, 46, 29, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 17, 34, 49, 64, 84, 90, 113, 125, 139, 157, 171, 177, 194, 203, 218, 224, 232, 235, 244, 250, 253, 253, 255, 253, 253, 253, 244, 241, 238, 226, 224, 212, 206, 194, 186, 171, 162, 148, 142, 128, 122, 104, 96, 87, 81, 69, 66, 55, 52, 52, 40, 40, 34, 40, 37, 34, 37, 40, 40, 46, 46, 61, 55, 69, 75, 84, 90, 98, 110, 119, 133, 139, 151, 160, 165, 180, 186, 200, 203, 212, 221, 224, 229, 235, 232, 244, 244, 241, 244, 244, 238, 244, 235, 235, 224, 224, 218, 212, 200, 194, 183, 177, 171, 157, 151, 136, 130, 119, 110, 101, 90, 81, 75, 66, 58, 52, 43, 43, 37, 32, 32, 23, 29, 26, 26, 26, 26, 32, 32, 37, 40, 46, 46, 58, 61, 69, 75, 84, 90, 98, 110, 113, 122, 130, 142, 145, 154, 162, 162, 174, 180, 183, 192, 192, 194, 197, 197, 200, 203, 203, 200, 200, 200, 197, 197, 192, 186, 189, 180, 171, 171, 165, 160, 148, 148, 139, 133, 125, 119, 110, 107, 101, 87, 90, 81, 78, 72, 69, 61, 61, 58, 64, 52, 55, 55, 55, 52, 58, 58, 58, 64, 64, 66, 75, 75, 78, 84, 93, 96, 98, 104, 110, 116, 128, 125, 139, 139, 145, 148, 157, 160, 160, 171, 171, 174, 174, 180, 183, 183, 183, 183, 183, 186, 183, 180, 183, 177, 180, 171, 174, 168, 165, 162, 162, 151, 151, 145, 142, 139, 133, 128, 125, 119, 116, 107, 104, 104, 96, 96, 90, 90, 84, 84, 81, 81, 78, 78, 78, 81, 78, 78, 81, 78, 81, 84, 87, 90, 90, 96, 93, 101, 101, 110, 107, 116, 119, 119, 128, 128, 133, 136, 142, 145, 145, 151, 151, 157, 160, 157, 165, 160, 168, 162, 168, 165, 171, 162, 168, 162, 162, 171, 157, 165, 160, 157, 157, 154, 151, 151, 145, 145, 142, 139, 133, 133, 133, 122, 128, 119, 119, 113, 119, 107, 110, 104, 104, 104, 98, 101, 96, 98, 98, 96, 96, 96, 96, 98, 98, 96, 98, 98, 101, 101, 107, 107, 107, 110, 107, 113, 119, 119, 119, 122, 255, 197, 207, 244, 229, 192, 164, 255, 128, 162, 201, 249, 176, 137, 78, 120, 82, 115, 95, 65, 137, 82, 120, 122, 72, 80, 67, 0, 53, 72, 70, 38, 8, 58, 50, 43, 60, 45, 77, 78, 90, 32, 72, 68, 43, 45, 58, 95, 83, 95, 47, 88, 78, 90, 92, 65, 108, 85, 95, 146, 144, 125, 129, 129, 136, 113, 134, 144, 107, 115, 115, 122, 123, 218, 3, 0, 161, 104, 255, 181, 77, 255, 0, 0, 0, 10, 47, 60, 104, 255, 101, 255, 107, 0, 255, 148, 47, 0, 128, 104, 242, 13, 114, 0, 255, 0, 255, 0, 0, 225, 255, 255, 158, 168, 255, 255, 43, 255, 0, 40, 0, 64, 134, 104, 165, 87, 3, 30, 195, 94, 144, 255, 147, 235, 160, 73, 55, 54, 48, 16, 75, 5, 83, 123, 176, 226, 255, 255, 213, 72, 105, 95, 102, 0, 58, 255, 191, 151, 102, 151, 169, 223, 213, 226, 255, 255, 232, 25, 80, 44, 16, 48, 92, 35, 25, 0, 0, 0, 0, 179, 214, 169, 179, 216, 147, 163, 238, 186, 255, 255, 255, 188, 45, 76, 45, 66, 16, 55, 102, 10, 19, 0, 11, 0, 104, 245, 189, 198, 154, 197, 223, 188, 166, 198, 244, 253, 255, 102, 0, 29, 66, 101, 120, 101, 95, 150, 91, 47, 0, 0, 39, 244, 160, 104, 66, 64, 54, 92, 86, 75, 100, 178, 204, 251, 88, 95, 185, 216, 255, 251, 247, 214, 186, 191, 188, 120, 75, 69, 189, 128, 83, 30, 0, 0, 2, 16, 63, 77, 69, 94, 161, 166, 86, 129, 175, 229, 255, 255, 255, 255, 226, 194, 166, 185, 150, 102, 160, 148, 101, 60, 19, 0, 0, 0, 2, 29, 60, 110, 77, 114, 110, 83, 132, 157, 201, 229, 255, 255, 255, 254, 229, 198, 163, 142, 169, 157, 139, 101, 83, 47, 20, 0, 0, 0, 0, 0, 27, 55, 95, 111, 77, 104, 144, 167, 189, 214, 241, 255, 255, 255, 255, 247, 216, 189, 151, 122, 154, 135, 97, 73, 58, 39, 16, 0, 0, 0, 0, 5, 30, 58, 91, 125, 129, 113, 141, 167, 182, 198, 210, 235, 245, 255, 254, 244, 222, 201, 167, 144, 110, 104, 128, 97, 80, 67, 54, 44, 26, 16, 5, 8, 19, 35, 63, 83, 114, 135, 158, 138, 132, 163, 169, 180, 188, 201, 219, 229, 236, 228, 216, 195, 172, 150, 123, 100, 88, 116, 105, 86, 77, 69, 64, 47, 35, 25, 20, 29, 44, 63, 86, 107, 135, 153, 175, 147, 144, 163, 169, 175, 179, 194, 204, 216, 216, 214, 198, 185, 161, 141, 122, 97, 83, 76, 104, 102, 86, 83, 82, 76, 64, 54, 33, 38, 41, 57, 75, 101, 125, 148, 175, 188, 197, 205, 201, 200, 188, 180, 166, 154, 141, 132, 120, 113, 110, 101, 100, 97, 97, 101, 104, 105, 94, 97, 107, 111, 116, 123, 138, 139, 153, 153, 160, 157, 151, 147, 139, 135, 125, 119, 119, 113, 107, 110, 105, 110, 110, 110, 114, 114, 119, 119, 125, 129, 129, 132, 130, 132, 132, 133, 132, 130, 132, 130, 130, 129, 129, 128, 128, 125, 128, 123, 125, 122, 128, 122, 129, 141, 133, 132, 132, 130, 125, 119, 113, 110, 107, 105, 111, 111, 114, 119, 129, 129, 133, 135, 139, 138, 141, 138, 135, 139, 132, 133, 130, 130, 128, 122, 122, 120, 120, 122, 116, 120, 120, 120, 120, 122, 119, 123, 128, 125, 123, 128, 129, 128, 128, 129, 129, 128, 128, 128, 128, 123, 129, 116, 114, 119, 116, 120, 122, 129, 132, 138, 138, 141, 139, 139, 135, 132, 130, 123, 120, 119, 114, 114, 113, 116, 114, 113, 116, 119, 119, 120, 123, 123, 129, 129, 129, 130, 129, 130, 130, 130, 130, 129, 129, 129, 129, 120, 125, 123, 145, 132, 99, 137, 123, 156, 138, 148, 161, 119, 152, 132, 96, 141, 99, 109, 108, 123, 124, 68, 124, 118, 106, 146, 144, 156, 175, 130, 116, 143, 105, 117, 139, 170, 120, 103, 106, 111, 93, 113, 130, 76, 132, 133, 119, 132, 152, 155, 138, 155, 122, 109, 113, 103, 82, 96, 132, 133, 129, 99, 134, 113, 147, 125, 133, 148, 161, 162, 133, 113, 107, 102, 119, 113, 148, 164, 121, 79, 82, 170, 174, 181, 182, 181, 182, 180, 175, 171, 168, 160, 158, 150, 146, 139, 136, 125, 129, 112, 123, 85, 14, 21, 9, 14, 15, 23, 32, 42, 49, 57, 66, 72, 81, 83, 95, 99, 110, 116, 132, 133, 148, 141, 193, 246, 240, 255, 252, 254, 250, 249, 239, 237, 226, 220, 212, 202, 193, 186, 177, 167, 160, 152, 144, 136, 130, 120, 119, 100, 35, 40, 27, 28, 26, 28, 29, 36, 39, 46, 52, 58, 66, 73, 80, 89, 93, 103, 107, 113, 120, 125, 131, 136, 141, 146, 149, 151, 203, 207, 210, 211, 210, 208, 203, 199, 196, 187, 182, 177, 168, 164, 157, 149, 144, 137, 132, 125, 119, 116, 109, 107, 102, 97, 95, 93, 87, 90, 82, 87, 67, 34, 41, 34, 40, 39, 45, 46, 53, 56, 67, 68, 78, 82, 90, 95, 102, 107, 113, 119, 124, 130, 135, 138, 144, 146, 152, 151, 160, 176, 176, 185, 190, 188, 192, 192, 194, 183, 162, 162, 158, 180, 174, 174, 158, 152, 150, 146, 145, 139, 147, 150, 148, 133, 122, 125, 133, 132, 128, 123, 120, 122, 120, 122, 112, 108, 106, 109, 110, 110, 113, 117, 100, 94, 105, 119, 116, 100, 100, 108, 116, 113, 107, 108, 108, 110, 110, 112, 116, 120, 124, 120, 120, 129, 130, 131, 131, 136, 139, 138, 134, 145, 159, 150, 141, 146, 150, 148, 157, 138, 145, 142, 136, 145, 137, 145, 142, 137, 133, 138, 128, 137, 132, 130, 134, 128, 135, 128, 129, 131, 120, 124, 128, 118, 119, 123, 122, 120, 121, 120, 115, 119, 112, 117, 118, 121, 120, 120, 123, 120, 124, 130, 130, 123, 121, 125, 124, 128, 124, 130, 131, 122, 131, 132, 129, 130, 125, 132, 125, 129, 123, 125, 124, 123, 124, 128, 124, 128, 128, 123, 125, 129, 125, 123, 123, 122, 125, 128, 128, 124, 124, 122, 128, 123, 128, 124, 125, 125, 130, 128, 131, 129, 130, 128, 128, 128, 128, 130, 130, 125, 128, 128, 129, 133, 125, 132, 125, 134, 131, 131, 132, 130, 133, 129, 131, 130, 131, 131, 130, 130, 129, 129, 130, 128, 128, 128, 125, 132, 129, 128, 125, 129, 129, 125, 128, 125, 128, 131, 123, 128, 128, 125, 125, 122, 125, 122, 123, 123, 122, 124, 122, 124, 124, 121, 123, 122, 123, 121, 123, 121, 122, 121, 122, 123, 122, 123, 121, 122, 123, 122, 123, 121, 125, 123, 128, 123, 124, 124, 125, 123, 125, 124, 125, 124, 124, 124, 123, 128, 124, 125, 124, 125, 125, 125, 125, 124, 125, 128, 128, 125, 128, 125, 125, 128, 128, 128, 128, 128, 125, 128, 128, 129, 125, 119, 98, 74, 30, 55, 94, 103, 103, 102, 109, 110, 122, 104, 102, 92, 83, 90, 95, 99, 107, 112, 118, 134, 139, 156, 160, 167, 174, 166, 168, 165, 166, 167, 171, 173, 160, 147, 143, 140, 136, 136, 133, 136, 141, 144, 148, 155, 164, 168, 166, 169, 172, 169, 166, 176, 168, 171, 155, 148, 143, 130, 123, 128, 125, 125, 128, 128, 134, 135, 141, 147, 151, 150, 157, 149, 148, 150, 142, 144, 139, 133, 130, 123, 112, 116, 108, 110, 107, 115, 112, 120, 118, 131, 125, 139, 130, 132, 130, 134, 122, 123, 113, 146, 249, 228, 211, 191, 182, 166, 159, 150, 149, 146, 143, 148, 149, 154, 152, 154, 146, 144, 132, 125, 117, 108, 95, 89, 72, 63, 56, 47, 44, 44, 45, 46, 52, 55, 67, 69, 78, 86, 87, 87, 89, 85, 84, 76, 72, 67, 60, 49, 47, 43, 41, 43, 43, 47, 54, 58, 68, 73, 84, 94, 95, 103, 104, 104, 103, 105, 97, 96, 88, 89, 78, 80, 74, 76, 77, 80, 87, 88, 96, 104, 111, 120, 122, 132, 133, 138, 135, 138, 130, 142, 104, 69, 98, 59, 59, 43, 55, 53, 84, 76, 70, 114, 119, 148, 125, 171, 183, 214, 224, 215, 212, 206, 197, 165, 147, 160, 164, 125, 104, 116, 122, 110, 130, 152, 151, 166, 196, 195, 212, 213, 255, 229, 229, 227, 202, 182, 184, 178, 159, 151, 137, 139, 133, 111, 109, 125, 148, 174, 170, 182, 193, 208, 221, 206, 239, 201, 202, 231, 185, 187, 146, 150, 148, 130, 116, 123, 113, 125, 138, 137, 171, 157, 162, 150, 167, 186, 199, 216, 174, 167, 173, 151, 150, 144, 121, 118, 116, 116, 106, 105, 108, 125, 139, 142, 145, 148, 145, 158, 152, 156, 173, 137, 133, 143, 117, 132, 120, 113, 93, 119, 99, 95, 105, 117, 103, 118, 138, 133, 137, 141, 148, 138, 160, 144, 144, 137, 146, 134, 116, 110, 109, 108, 95, 72, 92, 113, 105, 84, 107, 115, 121, 117, 132, 134, 135, 131, 135, 112, 129, 131, 105, 105, 106, 87, 78, 105, 74, 102, 97, 100, 111, 110, 114, 115, 114, 116, 116, 115, 136, 138, 118, 110, 108, 109, 105, 106, 103, 104, 89, 103, 91, 86, 113, 109, 113, 111, 116, 114, 117, 115, 116, 116, 115, 115, 115, 113, 112, 112, 109, 111, 103, 94, 98, 111, 114, 114, 115, 117, 118, 117, 118, 118, 119, 120, 118, 119, 117, 118, 116, 117, 115, 114, 115, 113, 115, 115, 115, 113, 118, 116, 117, 118, 118, 122, 117, 120, 121, 120, 120, 118, 120, 118, 119, 116, 118, 116, 118, 117, 118, 117, 119, 119, 120, 120, 121, 120, 122, 123, 122, 122, 124, 121, 122, 123, 121, 120, 122, 119, 121, 120, 121, 121, 121, 121, 124, 122, 123, 123, 128, 124, 128, 128, 125, 125, 129, 124, 125, 124, 124, 123, 124, 123, 124, 123, 125, 125, 124, 130, 124, 129, 129, 130, 128, 128, 106, 165, 138, 133, 116, 82, 89, 124, 92, 113, 119, 161, 158, 145, 155, 136, 149, 154, 135, 104, 69, 59, 30, 78, 132, 207, 184, 181, 209, 212, 213, 182, 136, 102, 74, 19, 53, 15, 27, 25, 42, 62, 141, 165, 202, 201, 235, 255, 254, 231, 238, 164, 140, 86, 88, 72, 52, 44, 4, 32, 54, 106, 136, 166, 166, 194, 232, 198, 218, 165, 139, 110, 63, 65, 64, 60, 47, 65, 37, 83, 105, 138, 154, 182, 184, 182, 199, 192, 205, 169, 189, 150, 110, 123, 109, 93, 81, 76, 56, 75, 78, 101, 99, 110, 99, 131, 113, 119, 178, 130, 138, 90, 100, 101, 93, 86, 109, 98, 66, 120, 119, 158, 149, 192, 190, 181, 194, 207, 204, 185, 209, 141, 144, 134, 112, 122, 69, 93, 68, 91, 92, 73, 76, 120, 128, 106, 151, 104, 156, 103, 145, 122, 100, 159, 101, 139, 125, 112, 158, 159, 162, 169, 166, 154, 163, 170, 174, 175, 200, 160, 160, 163, 138, 163, 124, 161, 79, 92, 121, 101, 90, 86, 119, 80, 64, 95, 69, 110, 106, 125, 95, 84, 186, 110, 135, 164, 132, 166, 189, 165, 156, 172, 141, 169, 171, 161, 207, 185, 142, 174, 166, 181, 124, 133, 156, 109, 63, 90, 120, 75, 95, 51, 17, 50, 76, 64, 29, 66, 82, 47, 91, 100, 47, 104, 122, 94, 153, 103, 154, 98, 123, 135, 152, 132, 112, 166, 158, 149, 150, 203, 178, 165, 145, 141, 133, 112, 103, 89, 106, 90, 100, 75, 102, 89, 54, 63, 69, 57, 54, 78, 108, 90, 98, 108, 115, 110, 128, 146, 131, 136, 146, 146, 145, 184, 151, 166, 135, 138, 181, 202, 122, 159, 148, 171, 136, 162, 153, 94, 122, 103, 116, 89, 125, 92, 96, 84, 98, 98, 90, 84, 72, 104, 133, 115, 83, 146, 162, 150, 162, 149, 159, 149, 159, 144, 159, 143, 166, 164, 152, 133, 185, 181, 169, 169, 146, 204, 135, 156, 128, 115, 123, 119, 120, 116, 120, 118, 125, 104, 115, 106, 50, 106, 64, 112, 128, 135, 148, 141, 148, 141, 145, 142, 143, 141, 142, 141, 146, 153, 155, 136, 135, 140, 138, 134, 135, 133, 136, 133, 135, 132, 134, 132, 135, 132, 133, 131, 134, 131, 133, 132, 132, 131, 133, 130, 133, 124, 130, 132, 131, 131, 131, 132, 130, 130, 132, 130, 130, 131, 129, 131, 130, 129, 131, 128, 131, 130, 129, 129, 129, 129, 130, 130, 128, 128, 130, 128, 129, 128, 92, 166, 168, 50, 106, 125, 248, 162, 92, 174, 241, 120, 150, 140, 139, 172, 132, 109, 154, 179, 179, 140, 138, 169, 157, 150, 92, 113, 154, 142, 142, 125, 132, 130, 122, 123, 116, 116, 112, 112, 104, 103, 85, 81, 64, 72, 20, 101, 61, 17, 67, 112, 47, 76, 79, 84, 73, 61, 124, 55, 103, 99, 82, 128, 120, 142, 71, 59, 117, 146, 91, 61, 139, 162, 165, 165, 161, 162, 155, 156, 150, 154, 138, 84, 153, 145, 149, 141, 149, 129, 100, 129, 111, 112, 112, 65, 77, 130, 112, 96, 89, 154, 112, 105, 116, 153, 109, 95, 103, 37, 80, 125, 120, 72, 123, 116, 129, 119, 134, 125, 144, 130, 149, 132, 165, 113, 185, 187, 105, 123, 216, 128, 152, 219, 90, 173, 255, 141, 105, 197, 197, 170, 121, 254, 115, 149, 246, 161, 66, 226, 202, 119, 147, 187, 164, 160, 189, 153, 141, 174, 154, 164, 145, 125, 181, 160, 153, 150, 155, 169, 120, 124, 181, 140, 163, 131, 146, 146, 212, 139, 106, 153, 140, 147, 137, 117, 145, 141, 140, 140, 139, 139, 136, 137, 136, 134, 130, 97, 126, 128, 150, 137, 125, 107, 125, 133, 147, 110, 106, 112, 130, 179, 140, 139, 93, 113, 124, 169, 141, 130, 88, 117, 131, 176, 134, 128, 78, 112, 100, 169, 158, 117, 66, 106, 149, 193, 150, 113, 158, 244, 129, 146, 70, 96, 66, 20, 118, 211, 166, 109, 54, 30, 126, 219, 240, 182, 76, 0, 96, 241, 255, 188, 74, 38, 130, 182, 218, 100, 56, 129, 67, 0, 67, 82, 150, 132, 140, 163, 211, 236, 226, 141, 42, 0, 81, 125, 141, 125, 139, 139, 255, 222, 120, 114, 115, 114, 115, 114, 57, 92, 133, 117, 130, 118, 132, 116, 138, 75, 0, 0, 50, 153, 158, 158, 156, 156, 156, 153, 154, 150, 153, 148, 150, 146, 150, 140, 152, 118, 73, 84, 84, 89, 108, 168, 158, 165, 156, 161, 153, 160, 149, 161, 114, 129, 158, 146, 150, 145, 147, 144, 144, 140, 141, 138, 139, 136, 136, 134, 134, 52, 115, 222, 56, 200, 136, 133, 156, 0, 95, 142, 157, 102, 173, 98, 172, 94, 159, 180, 166, 204, 70, 56, 95, 26, 220, 204, 12, 124, 67, 78, 108, 102, 252, 97, 115, 118, 57, 65, 81, 204, 232, 104, 129, 142, 174, 202, 122, 150, 107, 193, 80, 173, 107, 92, 60, 130, 62, 125, 74, 228, 136, 132, 82, 189, 6, 210, 112, 105, 147, 47, 179, 114, 150, 92, 189, 136, 195, 56, 218, 54, 56, 114, 239, 164, 137, 115, 71, 167, 128, 102, 95, 101, 136, 118, 129, 136, 172, 3, 133, 139, 117, 115, 122, 108, 121, 118, 75, 130, 68, 98, 150, 157, 162, 108, 87, 147, 74, 137, 197, 133, 128, 135, 99, 77, 95, 121, 194, 174, 72, 227, 104, 147, 145, 137, 198, 85, 129, 135, 140, 72, 136, 139, 68, 193, 98, 128, 77, 107, 164, 29, 220, 163, 77, 150, 146, 179, 64, 46, 118, 155, 130, 125, 169, 107, 119, 108, 183, 98, 132, 53, 217, 54, 200, 115, 125, 125, 97, 172, 114, 179, 105, 118, 46, 195, 121, 52, 114, 133, 77, 147, 162, 119, 56, 177, 115, 91, 129, 129, 94, 140, 139, 133, 117, 128, 164, 132, 97, 94, 140, 133, 107, 114, 170, 104, 155, 108, 71, 234, 90, 121, 255, 101, 101, 172, 102, 156, 108, 112, 102, 128, 101, 162, 74, 101, 149, 118, 169, 43, 146, 119, 146, 150, 94, 122, 102, 122, 164, 91, 164, 132, 87, 71, 87, 117, 145, 200, 164, 140, 15, 139, 139, 115, 159, 75, 190, 124, 128, 119, 62, 176, 169, 75, 195, 153, 94, 146, 105, 128, 190, 109, 132, 172, 65, 78, 125, 99, 157, 162, 30, 176, 157, 122, 107, 143, 139, 17, 115, 157, 139, 125, 95, 132, 122, 145, 200, 172, 52, 122, 114, 107, 137, 149, 159, 195, 135, 62, 109, 107, 136, 166, 111, 183, 172, 16, 130, 156, 143, 149, 77, 124, 109, 102, 107, 176, 119, 67, 189, 92, 195, 135, 91, 97, 107, 145, 139, 117, 143, 118, 85, 136, 150, 128, 101, 121, 145, 121, 132, 147, 130, 77, 101, 172, 105, 232, 67, 142, 153, 108, 50, 169, 132, 101, 101, 85, 193, 137, 59, 119, 112, 102, 101, 159, 195, 49, 111, 122, 129, 62, 162, 173, 99, 117, 121, 94, 143, 143, 143, 109, 54, 119, 155, 179, 101, 107, 137, 118, 135, 162, 135, 95, 64, 132, 159, 139, 174, 87, 137, 111, 149, 146, 162, 53, 84, 162, 182, 160, 109, 97, 118, 112, 143, 122, 137, 112, 107, 117, 143, 140, 90, 155, 132, 99, 152, 142, 121, 43, 115, 160, 99, 157, 136, 146, 135, 91, 173, 135, 142, 67, 71, 201, 95, 159, 145, 109, 130, 153, 104, 92, 169, 108, 95, 132, 122, 149, 137, 114, 137, 184, 150, 42, 143, 121, 163, 139, 108, 82, 118, 156, 145, 122, 114, 132, 115, 125, 135, 150, 95, 88, 84, 133, 183, 143, 56, 149, 152, 147, 167, 80, 112, 172, 125, 142, 108, 129, 136, 122, 99, 125, 117, 135, 139, 117, 121, 160, 119, 139, 99, 130, 102, 90, 182, 121, 39, 162, 187, 146, 122, 98, 65, 125, 98, 135, 183, 140, 146, 124, 80, 97, 164, 146, 142, 140, 153, 104, 124, 145, 98, 107, 153, 149, 117, 84, 125, 72, 77, 157, 152, 142, 152, 125, 115, 142, 77, 128, 137, 135, 140, 99, 122, 117, 143, 99, 104, 146, 72, 117, 75, 121, 152, 155, 81, 129, 162, 130, 149, 118, 105, 91, 117, 108, 176, 182, 136, 92, 133, 124, 85, 70, 182, 85, 149, 129, 122, 97, 125, 159, 146, 183, 132, 81, 129, 99, 125, 142, 133, 166, 125, 102, 112, 125, 139, 145, 145, 124, 90, 108, 130, 147, 84, 146, 163, 121, 156, 122, 124, 114, 114, 129, 95, 137, 124, 128, 130, 109, 130, 114, 159, 136, 81, 125, 132, 132, 125, 179, 111, 108, 121, 142, 105, 121, 104, 90, 155, 156, 115, 68, 99, 167, 137, 136, 129, 108, 137, 97, 115, 98, 95, 114, 169, 81, 111, 170, 170, 159, 136, 130, 114, 129, 133, 92, 97, 128, 114, 133, 130, 159, 122, 137, 129, 125, 150, 97, 101, 153, 128, 128, 129, 125, 104, 98, 121, 125, 115, 177, 145, 146, 124, 125, 102, 81, 140, 139, 125, 109, 133, 155, 112, 102, 147, 149, 105, 109, 104, 121, 125, 136, 162, 115, 104, 85, 108, 159, 137, 150, 139, 102, 135, 132, 156, 135, 136, 121, 101, 122, 122, 119, 115, 117, 137, 150, 140, 121, 128, 111, 130, 121, 136, 128, 108, 146, 122, 101, 105, 149, 143, 152, 135, 118, 107, 128, 135, 114, 121, 99, 135, 153, 147, 99, 104, 119, 112, 150, 122, 132, 150, 130, 108, 102, 108, 117, 137, 137, 124, 132, 119, 129, 119, 152, 129, 142, 147, 117, 130, 124, 107, 101, 122, 121, 140, 169, 119, 99, 88, 122, 118, 118, 130, 137, 145, 125, 132, 129, 130, 115, 128, 128, 108, 125, 143, 118, 115, 114, 130, 142, 125, 107, 107, 136, 119, 143, 115, 115, 112, 117, 143, 132, 133, 118, 111, 133, 129, 117, 132, 129, 139, 91, 132, 114, 140, 122, 128, 143, 119, 136, 137, 121, 118, 104, 109, 132, 137, 128, 147, 107, 107, 130, 129, 135, 125, 128, 121, 137, 139, 105, 109, 133, 140, 145, 115, 128, 129, 128, 125, 111, 112, 115, 130, 122, 137, 122, 121, 140, 118, 140, 118, 119, 115, 129, 132, 125, 133, 115, 128, 135, 130, 135, 117, 128, 121, 122, 135, 124, 115, 122, 135, 133, 114, 128, 115, 124, 128, 112, 132, 137, 136, 125, 129, 125, 124, 121, 136, 132, 118, 128, 124, 135, 118, 130, 132, 118, 115, 121, 118, 122, 156, 136, 117, 117, 130, 124, 122, 124, 124, 122, 139, 128, 145, 125, 111, 143, 121, 117, 105, 129, 117, 130, 142, 119, 117, 139, 136, 115, 117, 118, 125, 130, 129, 129, 130, 132, 129, 118, 124, 124, 118, 122, 124, 124, 136, 125, 121, 121, 133, 135, 119, 129, 133, 118, 124, 125, 125, 135, 115, 112, 128, 121, 119, 137, 140, 121, 118, 137, 125, 117, 122, 115, 132, 136, 121, 125, 122, 112, 128, 125, 132, 130, 122, 119, 124, 121, 130, 136, 119, 125, 122, 124, 128, 133, 129, 119, 128, 128, 132, 107, 112, 137, 121, 133, 132, 133, 130, 122, 125, 124, 128, 129, 121, 121, 124, 128, 130, 124, 118, 119, 135, 125, 132, 118, 118, 130, 128, 128, 129, 128, 118, 122, 125, 121, 122, 125, 130, 125, 132, 125, 121, 121, 129, 130, 118, 133, 128, 118, 124, 128, 124, 129, 121, 133, 125, 121, 117, 132, 122, 119, 135, 136, 121, 124, 114, 115, 129, 132, 124, 132, 130, 125, 130, 108, 128, 124, 133, 129, 122, 117, 132, 133, 129, 125, 129, 118, 118, 130, 124, 121, 124, 130, 121, 130, 122, 125, 119, 135, 125, 119, 130, 125, 125, 130, 129, 124, 130, 129, 118, 132, 128, 125, 125, 128, 136, 121, 125, 121, 124, 128, 124, 128, 125, 125, 129, 125, 128, 118, 128, 124, 133, 125, 128, 129, 125, 128, 124, 128, 124, 130, 124, 125, 122, 125, 125, 119, 125, 129, 132, 124, 129, 124, 121, 124, 128, 128, 128, 122, 128, 128, 122, 130, 121, 125, 128, 121, 133, 129, 125, 124, 124, 128, 124, 128, 122, 125, 129, 125, 125, 128, 129, 128, 130, 129, 124, 124, 124, 133, 124, 128, 122, 125, 125, 122, 124, 130, 125, 122, 124, 128, 125, 122, 125, 125, 128, 122, 129, 125, 124, 128, 125, 132, 122, 125, 122, 117, 125, 125, 128, 125, 128, 125, 128, 125, 128, 125, 125, 125, 128, 128, 125, 124, 130, 124, 129, 128, 128, 124, 125, 129, 128, 125, 124, 128, 128, 128, 128, 124, 129, 125, 128, 124, 128, 125, 128, 125, 124, 129, 125, 125, 125, 128, 129, 128, 128, 124, 128, 128, 125, 128, 128, 125, 124, 129, 128, 125, 125, 128, 125, 125, 129, 125, 125, 129, 125, 129, 125, 125, 129, 125, 128, 125, 124, 129, 125, 134, 137, 139, 142, 143, 145, 146, 147, 150, 151, 151, 153, 153, 155, 155, 156, 161, 225, 227, 220, 215, 210, 203, 200, 193, 189, 183, 178, 175, 170, 165, 163, 158, 155, 237, 255, 225, 197, 170, 140, 122, 73, 38, 18, 33, 7, 5, 14, 52, 60, 69, 112, 155, 179, 186, 210, 197, 208, 223, 187, 163, 162, 124, 111, 78, 34, 35, 5, 10, 17, 23, 29, 39, 37, 70, 92, 111, 162, 166, 193, 196, 187, 183, 175, 169, 164, 153, 98, 82, 59, 43, 30, 11, 28, 28, 40, 41, 53, 88, 104, 144, 175, 179, 210, 197, 198, 185, 185, 172, 176, 140, 124, 93, 53, 50, 25, 25, 37, 40, 50, 53, 80, 107, 105, 131, 176, 189, 181, 209, 215, 202, 202, 192, 189, 172, 144, 125, 100, 83, 59, 73, 50, 40, 50, 54, 62, 71, 109, 115, 124, 159, 167, 200, 187, 205, 223, 214, 210, 201, 198, 162, 147, 149, 113, 102, 72, 76, 71, 44, 53, 59, 66, 91, 121, 112, 147, 161, 159, 188, 196, 190, 222, 214, 215, 190, 163, 155, 160, 134, 113, 94, 75, 84, 85, 59, 58, 68, 103, 112, 116, 115, 131, 160, 174, 201, 187, 192, 178, 207, 187, 167, 169, 163, 162, 155, 118, 118, 112, 80, 85, 86, 90, 94, 98, 100, 103, 107, 107, 132, 156, 146, 180, 186, 181, 176, 174, 169, 168, 162, 162, 157, 157, 138, 108, 117, 96, 73, 82, 81, 89, 90, 94, 96, 102, 101, 110, 143, 148, 143, 155, 182, 178, 175, 171, 167, 166, 160, 160, 154, 154, 123, 108, 112, 79, 76, 79, 83, 88, 88, 95, 96, 101, 98, 119, 148, 145, 141, 164, 182, 172, 175, 167, 166, 160, 160, 154, 154, 136, 106, 112, 107, 114, 99, 72, 80, 81, 85, 87, 93, 93, 100, 97, 113, 145, 144, 141, 149, 180, 173, 173, 167, 167, 161, 157, 155, 153, 147, 110, 107, 109, 108, 110, 77, 74, 80, 82, 87, 88, 91, 97, 98, 133, 141, 139, 139, 140, 137, 166, 175, 168, 166, 161, 160, 156, 153, 151, 125, 105, 110, 106, 110, 110, 93, 70, 81, 79, 89, 86, 94, 91, 116, 139, 138, 137, 138, 137, 135, 162, 176, 164, 166, 159, 161, 151, 157, 125, 108, 110, 110, 111, 112, 110, 115, 100, 76, 80, 85, 86, 91, 91, 111, 139, 136, 137, 137, 137, 137, 134, 142, 174, 167, 166, 163, 159, 156, 148, 113, 113, 113, 113, 114, 113, 115, 114, 118, 87, 79, 85, 88, 91, 95, 133, 137, 135, 136, 136, 135, 135, 136, 132, 154, 174, 164, 165, 160, 125, 117, 120, 117, 121, 118, 120, 120, 121, 119, 123, 113, 84, 88, 89, 99, 131, 137, 134, 136, 135, 135, 134, 135, 133, 135, 132, 135, 132, 133, 132, 132, 133, 132, 131, 132, 131, 131, 131, 130, 132, 129, 130, 131, 129, 129, 131, 128, 129, 130, 129, 129, 128, 129, 128, 128, 129, 129, 126, 129, 128, 129, 125, 129, 128, 128, 128, 126, 128, 128, 128, 126, 126, 128, 126, 128, 126, 126, 129, 124, 130, 157, 196, 185, 181, 174, 172, 164, 163, 158, 150, 149, 144, 144, 138, 136, 131, 128, 129, 124, 120, 121, 116, 115, 114, 112, 110, 110, 105, 107, 106, 105, 105, 103, 103, 101, 105, 98, 100, 100, 103, 98, 105, 96, 106, 92, 116, 27, 0, 0, 0, 2, 20, 0, 140, 112, 122, 160, 125, 181, 147, 0, 141, 130, 8, 228, 138, 149, 174, 116, 107, 121, 172, 129, 164, 6, 93, 255, 166, 82, 147, 128, 191, 209, 202, 201, 196, 188, 186, 117, 212, 210, 121, 100, 206, 235, 120, 160, 128, 157, 128, 157, 131, 212, 134, 149, 122, 157, 114, 187, 131, 105, 98, 138, 186, 124, 148, 136, 141, 138, 136, 84, 139, 145, 141, 145, 139, 147, 139, 144, 136, 149, 92, 117, 150, 139, 147, 134, 87, 188, 157, 133, 145, 136, 138, 136, 139, 134, 134, 136, 133, 134, 129, 149, 190, 122, 129, 121, 128, 67, 116, 130, 128, 129, 128, 128, 128, 125, 125, 129, 122, 125, 122, 125, 121, 130, 181, 122, 115, 120, 115, 116, 120, 115
};


unsigned int sampleStarts[] = {
	0x0,
	0x310,
	0x354,
	0x394,
	0x597,
	0x7AD,
	0x9A8,
	0xB54,
	0xC19,
	0xCBB,
	0x1215,
	0x1423,
	0x14DA,
	0x9999,
	0xAAAA,
	0xBBBB,
	0xCCCC,
	0xDDDD,
	0xEEEE
};






//////////////////////////////////////////
//  SETUP AND LOOP
//////////////////////////////////////////

void setup() {
	usbMIDI.setHandleNoteOff(OnNoteOff);
	usbMIDI.setHandleNoteOn(OnNoteOn) ;
	usbMIDI.setHandleControlChange(OnControlChange) ;
	usbMIDI.setHandlePitchChange(OnPitchChange);

	// Sets all of PORTB as outputs.
	// (Pins 0,1,2,3,13,14,15,4)
	// Connected to D0-D7 on SID
	DDRB = DDRB | B11111111;
	
	// Sets some of PORTD as outputs. 
	// (Pins 21,20,19,18,17)
	// Connected to A0-A4 on SID
	DDRD = DDRD | B01011111;
	
	// Write Enable pins
	// Each connected to !CS Pin(s)
	pinMode(CSchip0pin, OUTPUT); 
	digitalWrite(CSchip0pin, HIGH);  
  

	// LED Pin
	pinMode(11, OUTPUT);
	
	writeFilter(0,0,0);

	for (byte i=1;i<4;i++) {
  	
		writePWM(2048,i,0);
		writeADSR(0xF0,i,0);
		for (byte j=1;j<4;j++) {
			writeFrequency(100*j,i,0);
			writeWaveform(B01000001,i,0);
			digitalWrite(11,HIGH);
			delay(40);
			digitalWrite(11,LOW);
			delay(40);
		}
	}
	mainSampleEngine(2,57,127,0);
	for (byte i=1;i<4;i++) {
		writeWaveform(B01000000,i,0);
		writeFrequency(0,i,0);
		
	}
	while (millis() < 3000) {
		mainSampleEngine(1,0,0,0);
	}
	mainSampleEngine(3,57,127,0);
}




void loop() {
	usbMIDI.read();
	ledFlash(1);
	mainSampleEngine(1,0,0,0);
	leadOneArpeggiator(1,0,0,0,0);
	mainLeadOneEngine(1,0,0,LOW,0,0);
	leadTwoArpeggiator(1,0,0,0,0);
	mainLeadTwoEngine(1,0,0,LOW,0,0);
	leadThreeArpeggiator(1,0,0,0,0);
	mainLeadThreeEngine(1,0,0,LOW,0,0);
	mainPolyEngine(1,0,0);
}





//////////////////////////////////////////
//  AUXILIARY FUNCTIONS
//////////////////////////////////////////


byte mostRecentNoteInArray (byte noteNumberArray[8], 
	unsigned long noteDurationArray[8]) {
	
	byte i = 0;
	unsigned long mostRecentNoteDuration = 0;
	byte mostRecentNote = 0;
  
	for (i=0;i<8;i++) {
		if (noteDurationArray[i] > mostRecentNoteDuration) {
			mostRecentNoteDuration = noteDurationArray[i];
			mostRecentNote = noteNumberArray[i];
		}
	}
	return mostRecentNote;
}



void eraseOldestNoteInArray (byte (&noteNumberArray)[6], 
	byte (&noteVelocityArray)[6], unsigned long (&noteDurationArray)[6]) {
  
	byte i = 0;
	unsigned long oldestDuration = 4294967295;
	byte oldestArrayPosition = 0;
  
	for (i=0;i<6;i++) {
		if (noteDurationArray[i] < oldestDuration) {
			oldestDuration = noteDurationArray[i];
			oldestArrayPosition = i;
		}
	}
	noteNumberArray[oldestArrayPosition] = 0;
	noteVelocityArray[oldestArrayPosition] = 0;
	noteDurationArray[oldestArrayPosition] = 0;
}



void eraseOldestNoteInArrayPoly (byte (&noteNumberArray)[3], 
	byte (&noteVelocityArray)[3], unsigned long (&noteDurationArray)[3]) {
  
	byte i = 0;
	unsigned long oldestDuration = 4294967295;
	byte oldestArrayPosition = 0;
  
	for (i=0;i<3;i++) {
		if (noteDurationArray[i] < oldestDuration) {
			oldestDuration = noteDurationArray[i];
			oldestArrayPosition = i;
		}
	}
	noteNumberArray[oldestArrayPosition] = 0;
	noteVelocityArray[oldestArrayPosition] = 0;
	noteDurationArray[oldestArrayPosition] = 0;
}
  


byte mostRecentVelocityInArray (byte noteVelocityArray[8], 
	unsigned long noteDurationArray[8]) {
  
	byte i = 0;
	unsigned long mostRecentNoteDuration = 0;
	byte mostRecentVelocity = 0;
  
	for (i=0;i<8;i++) {
		if (noteDurationArray[i] > mostRecentNoteDuration) {
			mostRecentNoteDuration = noteDurationArray[i];
			mostRecentVelocity = noteVelocityArray[i];
		}
	}
	return mostRecentVelocity;
}




void placeNoteInFirstEmptyArrayPosition(byte (&noteNumberArray)[8], 
	unsigned long (&noteDurationArray)[8], byte outsideNoteNumber) {
	
	boolean noteAlreadyStored = LOW;
	
	for (int arrayCounter = 0; arrayCounter < 8 ; arrayCounter++) {
		if (noteNumberArray[arrayCounter] == outsideNoteNumber) {
			noteAlreadyStored = HIGH;
			break;
		}
	}
	if (noteAlreadyStored == LOW) {
		for (int arrayCounter = 0; arrayCounter < 8; arrayCounter++) {
			if (noteNumberArray[arrayCounter] == 0) {
				noteNumberArray[arrayCounter] = outsideNoteNumber;
            noteDurationArray[arrayCounter] = millis();
				break;
			}
		}
	}
}



void placeNoteAndVelocityInFirstEmptyArrayPosition(byte (&noteNumberArray)[8], 
	unsigned long (&noteDurationArray)[8], byte outsideNoteNumber, 
	byte (&noteVelocityArray)[8], byte outsideNoteVelocity) {
	
	boolean noteAlreadyStored = LOW;
	
	for (int arrayCounter = 0; arrayCounter < 8 ; arrayCounter++) {
		if (noteNumberArray[arrayCounter] == outsideNoteNumber) {
			noteAlreadyStored = HIGH;
			break;
		}
	}
	if (noteAlreadyStored == LOW) {
		for (int arrayCounter = 0; arrayCounter < 8; arrayCounter++) {
			if (noteNumberArray[arrayCounter] == 0) {
				noteNumberArray[arrayCounter] = outsideNoteNumber;
				noteDurationArray[arrayCounter] = millis();
				noteVelocityArray[arrayCounter] = outsideNoteVelocity;
				break;
			}
		}
	}
}



void eraseThisNoteInArray(byte (&noteNumberArray)[8], 
	unsigned long (&noteDurationArray)[8], byte outsideNoteNumber) {
  
	for (int arrayCounter = 0; arrayCounter < 8; arrayCounter++) {
		if (noteNumberArray[arrayCounter] == outsideNoteNumber) {
			noteNumberArray[arrayCounter] = 0;
			noteDurationArray[arrayCounter] = 0;
		}
	}
}



boolean testArrayContentsForAtLeastTwoNotes (byte noteNumberArray[8]) {
  
	byte testArrayContents1 = 0;
	byte testArrayContents2 = 0;
	byte i = 0;
	byte j = 0;
		
	while (i < 8) {
		if (noteNumberArray[i] > 0) {
			testArrayContents1 = noteNumberArray[i];
			j = i+1;
			while (j < 8) {
				if (noteNumberArray[j] > 0) {
					testArrayContents2 = noteNumberArray[j];
					break;
				}
				j++;
			}
			break;
		}
		i++;
	}
	if ((testArrayContents1 > 0) && (testArrayContents2 > 0)) {
		return HIGH;
	}
	else {
		return LOW;
	}
}



byte findSingleNoteInArray (byte noteNumberArray[8]) {
	byte arrayContents = 0;
	byte i = 0;
  
	while (i < 8) {
		if (noteNumberArray[i] > 0) {
			arrayContents = noteNumberArray[i];
			break;
		}
		i++;
	}
	return arrayContents;
}



boolean testArrayContentsForNoNotes (byte noteNumberArray[8]) {
	
	byte testArrayContents1 = 0;
	byte testArrayContents2 = 0;
	byte i = 0;
	byte j = 0;
		
	while (i < 8) {
		if (noteNumberArray[i] > 0) {
			testArrayContents1 = noteNumberArray[i];
			j = i+1;
			while (j < 8) {
				if (noteNumberArray[j] > 0) {
					testArrayContents2 = noteNumberArray[j];
					break;
				}
				j++;
			}
			break;
		}
		i++;
	}
	if ((testArrayContents1 == 0) && (testArrayContents2 == 0)) {
		return HIGH;
	}
	else {
		return LOW;
	}
}




boolean testArrayContentsForNoNotesPoly (byte noteNumberArray[3]) {
  
	byte testArrayContents1 = 0;
	byte testArrayContents2 = 0;
	byte i = 0;
	byte j = 0;
		
	while (i < 3) {
		if (noteNumberArray[i] > 0) {
			testArrayContents1 = noteNumberArray[i];
			j = i+1;
			while (j < 3) {
				if (noteNumberArray[j] > 0) {
					testArrayContents2 = noteNumberArray[j];
					break;
				}
				j++;
			}
			break;
		}
		i++;
 	}
	if ((testArrayContents1 == 0) && (testArrayContents2 == 0)) {
		return HIGH;
	}
	else {
		return LOW;
	}
}





//////////////////////////////////////////
//  OSCILLATOR AND ARPEGGIATOR FUNCTIONS
//////////////////////////////////////////


void mainSampleEngine(byte dataType, byte noteNumber, 
	byte noteVelocity, long longData) {
 
	static boolean insideRun = LOW;
	static unsigned long sampleCounter = 0;
	static byte sampleCounterIncrement = 1;
	static unsigned long maxSampleCounter = 400;
	byte current8bitSampleLevel;
	byte current4bitSampleLevel;
	static byte sampleNumber;
	static byte insideNoteNumber;
	unsigned long currentMicros;
	static unsigned long previousMicros;
	unsigned long elapsedTimeInMicros;
	static unsigned long sampleRateMicros = 250;
	static byte sampleIncrementRate = 1;

	switch (dataType) {
		case 1: // Run in main loop
			if (insideRun == HIGH) {
				if (sampleCounter >= maxSampleCounter) {
					sampleCounter = 0;
					insideRun = LOW;
				}
			}
			if (insideRun == HIGH) {
				currentMicros = micros();
				elapsedTimeInMicros = currentMicros - previousMicros;
				if (elapsedTimeInMicros >= sampleRateMicros) {
					current8bitSampleLevel = pgm_read_byte_near(
						sampleData + (sampleStarts[sampleNumber]) + sampleCounter);
					current4bitSampleLevel = 
						(current8bitSampleLevel >> 4) & B00001111;
					writeFilter(5, current4bitSampleLevel, 0);   
					sampleCounter = sampleCounter + sampleIncrementRate;
					previousMicros = micros();
				}
			}
		break;
   
		case 2: // Note On
			if ((insideRun == LOW) || (insideRun == HIGH)) {
				insideNoteNumber = noteNumber;
				if ((noteNumber >= 48) && (noteNumber <= 66)) {
					sampleNumber = noteNumber - 48;
					sampleCounter = 0;
					maxSampleCounter = sampleStarts[sampleNumber + 1] - 
						sampleStarts[sampleNumber];
					insideRun = HIGH;
				}
			}
		break;
   
		case 3: // Note Off
			if (insideNoteNumber == noteNumber) {
				insideRun = LOW;
			}
		break;
   
		case 4: // Pitch Bend
			if (longData == 8192) {
				sampleRateMicros = 250;
			}
			if (longData <= 8191) {
				sampleRateMicros = map(longData,0,8191,150,249);
			}
			if (longData >= 8193) {   
				sampleRateMicros = map(longData,8193,16838,251,75);
			}
		break;
   
		case 5: // CC1 Change
			if (noteNumber > 0) {
			sampleRateMicros = 251 * pow(200,((float)noteNumber/127));
			}
			else {
				sampleRateMicros = 250;
			}
		break;
	}
}





void mainPolyEngine(byte dataType, byte noteNumber, byte noteVelocity) {
  
	static byte noteNumberArray[3];
	static byte noteVelocityArray[3];
	static unsigned long noteDurationsArray[3];
	static boolean insideRun = LOW;
	static boolean decayOn = LOW;
	static boolean arrayFull = LOW;
	static unsigned int decayInterval;
	static unsigned long previousDecayMicros;
	unsigned long currentMicros;
	static byte currentVelocityLevel1;
	static byte currentVelocityLevel2;
	static byte currentVelocityLevel3;
	static boolean tremOn = LOW;
	static int tremOffset = 0;
	static unsigned int tremInterval = 1000;
	static unsigned long previousTremMicros;
	unsigned long currentMillis;
	static float tremRateDivisor = 42.0; // Smaller is faster.
	static float tremAmount;
	static float tremAmount2;
	static int tremMaxDepth = 127;
	static byte insideCC1 = 0;
	static byte polyOneChip = 0;
	static byte leadOneChannel = 1;
	static byte leadTwoChannel = 2;
	static byte leadThreeChannel = 3;
	
	static byte attackAmount = 0;
	static byte decayAmount = 0;
	static byte sustainAmount = 15;
	static byte releaseAmount = 0;
	static unsigned int adsr = 0xF0;
	
	static byte basicWaveform = B1000000;
	static byte basicWaveformNumber = 0;
	static byte waveformModifier = B0;
	static byte waveformModifierNumber = 0;
	static byte waveformCombo = B1000000;
	static unsigned int pwmAmount = 2048;
	
	static byte tremVolume = 15;

	switch (dataType) {
		case 1: // Main Loop Run
      	
			if ((insideRun == HIGH) && (insideCC1 > 0)) {
				currentMicros = micros();
				if ((currentMicros - previousTremMicros) >= tremInterval) {
					currentMillis = millis();
					tremAmount = sin(((float)currentMillis * 
						1.0/(float)tremRateDivisor));
					tremAmount2 = tremAmount * (float)tremMaxDepth;
					tremOffset = tremAmount2 * ((float)insideCC1/127.0);
					if (tremOffset < 0) {  // Rectifies negative values.
						tremVolume = 15 - map(tremOffset,-127,0,15,0);
					}
					else {
						tremVolume = 15 - map(tremOffset,0,127,0,15);
					}
					writeFilter(5,tremVolume,0);
					previousTremMicros = micros();
				}
			}
		break;
    
		case 2: // Note On
			insideRun = HIGH;
			arrayFull = HIGH;
			for (byte i=0;i<3;i++) {
				if (noteNumberArray[i] == 0) {
					arrayFull = LOW;
				}
			}
			if (arrayFull == HIGH) {
				eraseOldestNoteInArrayPoly(noteNumberArray,
					noteVelocityArray,noteDurationsArray);    
			}
			for (byte i=0;i<3;i++) {
				if ((noteNumberArray[i] == 0) && (noteNumber > 0)) {
					noteNumberArray[i] = noteNumber;
					noteVelocityArray[i] = noteVelocity;
					noteDurationsArray[i] = millis();
					float pitchInHertz;
					pitchInHertz = 440.00 * 
						pow(2, ((noteNumber - 69) / 12.00));
					switch (i) {
						case 0: 
							writeFrequency(pitchInHertz,
								leadOneChannel,polyOneChip);
							writeWaveform((waveformCombo | 0x1),
								leadOneChannel,polyOneChip);
							currentVelocityLevel1 = noteVelocity;
						break;
            
						case 1: 
							writeFrequency(pitchInHertz,
								leadTwoChannel,polyOneChip);
							writeWaveform((waveformCombo | 0x1),
								leadTwoChannel,polyOneChip);
							currentVelocityLevel2 = noteVelocity;
						break;
            
						case 2: 
							writeFrequency(pitchInHertz,
								leadThreeChannel,polyOneChip);
							writeWaveform((waveformCombo | 0x1),
								leadThreeChannel,polyOneChip);
							currentVelocityLevel3 = noteVelocity;
						break;
					}
					break;
				}
			} 
		break;
    
		case 3: // Note Off
			for (byte i=0;i<3;i++) {
				if ((noteNumberArray[i] == noteNumber) && (noteNumber > 0)) {
					noteNumberArray[i] = 0;
					noteVelocityArray[i] = 0;
					noteDurationsArray[i] = 0;
					switch (i) {
						case 0: 
							writeWaveform((waveformCombo & B11111110),
								leadOneChannel,polyOneChip);
						break;
						
						case 1: 
							writeWaveform((waveformCombo & B11111110),
								leadTwoChannel,polyOneChip);
						break;
						
						case 2: 
							writeWaveform((waveformCombo & B11111110),
								leadThreeChannel,polyOneChip);
						break;
					}
					if (testArrayContentsForNoNotesPoly(noteNumberArray) == HIGH) {
						writeWaveform((waveformCombo & B11111110),
							leadOneChannel,polyOneChip);
						writeWaveform((waveformCombo & B11111110),
							leadTwoChannel,polyOneChip);
						writeWaveform((waveformCombo & B11111110),
							leadThreeChannel,polyOneChip);
						insideRun = LOW;
					}
					break;
				}
			}
		break;
    
		case 4: // Pitch Bend (Unused)
		break;
    
		case 5: // CC 1 Change
			insideCC1 = noteNumber;
			if (insideCC1 == 0) {
				tremVolume = 15;
				writeFilter(5,tremVolume,0);
			}
		break;
    
		case 6: 
		break;

		case 9: // Basic Waveform Selection
			basicWaveformNumber = map(noteNumber,0,127,0,7);
			switch (basicWaveformNumber) {
				case 0: // Pulse Only
					basicWaveform = B1000000;
				break;
				
				case 1: // Saw Only
					basicWaveform = B100000;
				break;
				
				case 2: // Triangle Only
					basicWaveform = B10000;
				break;
				
				case 3: // Pulse/Saw
					basicWaveform = B1100000;
				break;
				
				case 4: // Pulse/Triangle
					basicWaveform = B1010000;
				break;
				
				case 5: // Saw/Triangle
					basicWaveform = B110000;
				break;
				
				case 6: // Pulse/Saw/Triangle
					basicWaveform = B1110000;
				break;
				
				case 7: // Noise
					basicWaveform = B10000000;
				break;
			}
			waveformCombo = basicWaveform | waveformModifier;
			if (insideRun == HIGH) {
				writeWaveform((waveformCombo | 0x1),
					leadOneChannel,polyOneChip);
				writeWaveform((waveformCombo | 0x1),
					leadTwoChannel,polyOneChip);
				writeWaveform((waveformCombo | 0x1),
					leadThreeChannel,polyOneChip);
			}
			else {
				writeWaveform((waveformCombo & B11111110),
					leadOneChannel,polyOneChip);
				writeWaveform((waveformCombo & B11111110),
					leadTwoChannel,polyOneChip);
				writeWaveform((waveformCombo & B11111110),
					leadThreeChannel,polyOneChip);
			}
		break;

		case 10: // Waveform Modifier Selection
			waveformModifierNumber = map(noteNumber,0,127,0,3);
			switch (waveformModifierNumber) {
				case 0: // Normal
					waveformModifier = B0;
				break;
				
				case 1: // Ring Mod
					waveformModifier = B100;
				break;
				
				case 2: // Sync
					waveformModifier = B10;
				break;
				
				case 3: // Ring Mod and Sync
					waveformModifier = B110;
				break;
			}
			waveformCombo = basicWaveform | waveformModifier;
			if (insideRun == HIGH) {
				writeWaveform((waveformCombo | 0x1),
					leadOneChannel,polyOneChip);
				writeWaveform((waveformCombo | 0x1),
					leadTwoChannel,polyOneChip);
				writeWaveform((waveformCombo | 0x1),
					leadThreeChannel,polyOneChip);
			}
			else {
    			writeWaveform((waveformCombo & B11111110),
    				leadOneChannel,polyOneChip);
    			writeWaveform((waveformCombo & B11111110),
    				leadTwoChannel,polyOneChip);
    			writeWaveform((waveformCombo & B11111110),
    				leadThreeChannel,polyOneChip);
			}
		break;

		case 11: // Attack Amount
    		attackAmount = map(noteNumber,0,127,0,15);
    		adsr = (attackAmount << 12) | (decayAmount << 8) | 
    			(sustainAmount << 4) | releaseAmount;
    		writeADSR(adsr,leadOneChannel,polyOneChip);
    		writeADSR(adsr,leadTwoChannel,polyOneChip);
    		writeADSR(adsr,leadThreeChannel,polyOneChip);
		break;
		
		case 12: // Decay Amount
    		decayAmount = map(noteNumber,0,127,0,15);
    		adsr = (attackAmount << 12) | (decayAmount << 8) | 
    			(sustainAmount << 4) | releaseAmount;
    		writeADSR(adsr,leadOneChannel,polyOneChip);
    		writeADSR(adsr,leadTwoChannel,polyOneChip);
    		writeADSR(adsr,leadThreeChannel,polyOneChip);
		break;
		
		case 13: // Sustain Amount
    		sustainAmount = map(noteNumber,0,127,0,15);
    		adsr = (attackAmount << 12) | (decayAmount << 8) | 
    			(sustainAmount << 4) | releaseAmount;
    		writeADSR(adsr,leadOneChannel,polyOneChip);
    		writeADSR(adsr,leadTwoChannel,polyOneChip);
    		writeADSR(adsr,leadThreeChannel,polyOneChip);
		break;
		
		case 14: // Release Amount
    		releaseAmount = map(noteNumber,0,127,0,15);
    		adsr = (attackAmount << 12) | (decayAmount << 8) | 
    			(sustainAmount << 4) | releaseAmount;
    		writeADSR(adsr,leadOneChannel,polyOneChip);
    		writeADSR(adsr,leadTwoChannel,polyOneChip);
    		writeADSR(adsr,leadThreeChannel,polyOneChip);
		break;
		
		case 15: // PWM Amount
    		pwmAmount = map(noteNumber,0,127,2048,0);
    		writePWM(pwmAmount,leadOneChannel,polyOneChip);
    		writePWM(pwmAmount,leadTwoChannel,polyOneChip);
    		writePWM(pwmAmount,leadThreeChannel,polyOneChip);
		break;
	}
}





void leadOneArpeggiator(byte dataType, byte noteNumber, byte noteVelocity, 
	byte noteNumberFromArray, unsigned long noteDurationFromArray) {
  
	static byte arpNoteNumberArray[8];
	static unsigned long arpNoteDurationArray[8];
	static boolean arpeggiatorOn = LOW;
	unsigned long currentMillis;
	static unsigned long previousMillis;
	unsigned long elapsedTimeInMillis;
	static unsigned int arpeggiatorInterval = 20;
	static byte currentArpArrayPosition;
	static boolean arpeggiatorRunning = LOW;
	byte transitionNote;

	switch (dataType) {
		case 1: // Main Loop Run
			if ((arpeggiatorOn == HIGH) && (arpeggiatorRunning == HIGH)) {
				currentMillis = millis();
				elapsedTimeInMillis = currentMillis - previousMillis;
				if (elapsedTimeInMillis >= arpeggiatorInterval) {
					mainLeadOneEngine(3,arpNoteNumberArray[currentArpArrayPosition],
						0,HIGH,1,0);
					if (currentArpArrayPosition < 7) {
						currentArpArrayPosition++;
					}
					else {
						currentArpArrayPosition = 0;
					}
					while (arpNoteNumberArray[currentArpArrayPosition] == 0) {
						if (currentArpArrayPosition < 7) {
							currentArpArrayPosition++;
						}
						else {
							currentArpArrayPosition = 0;
						}
					}
					mainLeadOneEngine(2,arpNoteNumberArray[currentArpArrayPosition],
						0,HIGH,1,0);
					previousMillis = millis();
				}
			}
		break;
    
		case 2: // Note On
			if (arpeggiatorOn == HIGH) {
				placeNoteInFirstEmptyArrayPosition(arpNoteNumberArray,
					arpNoteDurationArray,noteNumber);
				arpeggiatorRunning = testArrayContentsForAtLeastTwoNotes(
					arpNoteNumberArray) ? HIGH : LOW;
				if (arpeggiatorRunning == LOW) {
					mainLeadOneEngine(2,noteNumber,0,HIGH,0,0);
				}
			}
		break;
    
		case 3: // Note Off
			if (arpeggiatorOn == HIGH) {
				eraseThisNoteInArray(arpNoteNumberArray,
					arpNoteDurationArray,noteNumber);
				arpeggiatorRunning = testArrayContentsForAtLeastTwoNotes(
					arpNoteNumberArray) ? HIGH : LOW;
				if (arpeggiatorRunning == LOW) {
					mainLeadOneEngine(3,noteNumber,0,HIGH,0,0);
					if (testArrayContentsForNoNotes(arpNoteNumberArray) == LOW) {
						mainLeadOneEngine(2,findSingleNoteInArray(arpNoteNumberArray),
							0,HIGH,0,0);
					}
				}
			}
		break;
    
		case 4: // CC4: Arpeggiator On/Off
			if ((noteNumber > 0) && (arpeggiatorOn == LOW)) {
				arpeggiatorOn = HIGH;
				mainLeadOneEngine(8,0,0,HIGH,0,0);
				arpeggiatorRunning = testArrayContentsForAtLeastTwoNotes(
					arpNoteNumberArray) ? HIGH : LOW;
			}
			if (noteNumber == 0) {
				arpeggiatorOn = LOW;
				mainLeadOneEngine(3,arpNoteNumberArray[currentArpArrayPosition],
					0,HIGH,0,0);
				transitionNote = mostRecentNoteInArray(
					arpNoteNumberArray,arpNoteDurationArray);
				if (transitionNote > 0) {
					mainLeadOneEngine(2,transitionNote,0,HIGH,0,0);
				}
				for (int i=0;i<8;i++) {
					mainLeadOneEngine(8,i,0,LOW,arpNoteNumberArray[i],
						arpNoteDurationArray[i]);
				}
			}
		break;
    
		case 5: // Changes Arpeggiator Speed
			arpeggiatorInterval = map(noteNumber,0,127,20,200);
		break;
		
		case 6: // Accepts noteNumberArray and noteDurationArray
			arpNoteNumberArray[noteNumber] = noteNumberFromArray;
			arpNoteDurationArray[noteNumber] = noteDurationFromArray;
		break;
	}
}




void mainLeadOneEngine(byte dataType, byte noteNumber, 
	float floatData, boolean noteComingFromArpeggiator, 
	byte arpNoteNumberFromArray, unsigned long arpNoteDurationFromArray) {
  	  
	static boolean insideRun = LOW;
	static byte noteNumberArray[8];
	static unsigned long noteDurationArray[8];
	static boolean portamentoOn = HIGH;
	static boolean portamentoRunning = LOW;
	static boolean portamentoDirection;
	unsigned long currentMicros;
	static unsigned long previousPortamentoChange;
	static unsigned long portamentoSpeedInMicros = 50;
	float portamentoStepInHertz;
	static float insidePitchInHertz;
	static float insideTargetPitchInHertz;
	static float insidePitchInHertzAfterFactors;
	static float insideBendFactor = 1.0;
	static boolean arpeggiatorOn = LOW;
	static const byte leadOneChannel = 1;
	static const byte leadOneChip = 0;   
      
	static float insideVibFactor = 1.0;
	static byte insideCC1 = 0;
	static float vibRateDivisor = 23.0;
	unsigned long currentVibCountMicros;
	static unsigned long sineVibSpeedStepsInMicros = 2000;
	static int vibAmount = 0;
	static float vibAmount2 = 0.0;
	static float vibFactor = 1.0;
	static int modWheelFactor = 0;
	static unsigned long previousVibCountMicros = 0;
	static unsigned long currentMillis;
	
	static byte attackAmount = 0;
	static byte decayAmount = 0;
	static byte sustainAmount = 15;
	static byte releaseAmount = 0;
	static unsigned int adsr = 0xF0;

	static byte basicWaveform = B1000000;
	byte basicWaveformNumber = 0;
	static byte waveformModifier = B0;
	byte waveformModifierNumber = 0;
	static byte waveformCombo = B1000000;
	
	static unsigned int pwmAmount = 2048;

	switch (dataType) {
		case 1: // Main Loop Run
			if ((insideRun == HIGH) && (portamentoOn == HIGH) && 
				(portamentoRunning == HIGH)) {
				
				currentMicros = micros();
				if ((portamentoDirection == LOW) && (currentMicros - 
					previousPortamentoChange >= portamentoSpeedInMicros)) {
					
					portamentoStepInHertz = 2.0 * (insidePitchInHertz / 1000.0);
					insidePitchInHertz = insidePitchInHertz - portamentoStepInHertz;
					previousPortamentoChange = micros();
            
					if (insidePitchInHertz <= insideTargetPitchInHertz) {
						portamentoRunning = LOW;
						insidePitchInHertz = insideTargetPitchInHertz;
					}
            
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,
						leadOneChannel,leadOneChip);
				}
          
				if ((portamentoDirection == HIGH) && (currentMicros - 
					previousPortamentoChange >= portamentoSpeedInMicros)) {
					
					portamentoStepInHertz = 2.0 * (insidePitchInHertz / 1000.0);
					insidePitchInHertz = insidePitchInHertz + portamentoStepInHertz;
					previousPortamentoChange = micros();
					if (insidePitchInHertz >= insideTargetPitchInHertz) {
						portamentoRunning = LOW;
						insidePitchInHertz = insideTargetPitchInHertz;
					}
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,
						leadOneChannel,leadOneChip);
				}
			}
        
			if (insideCC1 > 0) {
				currentVibCountMicros = micros();

				// Limits how often the chip can calculate the sine function.
				if  ((currentVibCountMicros - previousVibCountMicros) >= 
					sineVibSpeedStepsInMicros) {

					currentMillis = millis();
					
					//range of  1 to -1, sclaed to time, factored to 100 to -100
					vibAmount = sin(((float)currentMillis * 
						(float)vibRateFactor)/(float)vibRateDivisor) * 100.0; 
					previousVibCountMicros = micros();
					
					//Scales vibAmount to very small positive or negative amounts
					vibAmount2 = (float)vibAmount * vibDepth; 
					modWheelFactor = map(insideCC1, 0, 127, 0, 2000);
					insideVibFactor = 1.0 + (vibAmount2 * ((float)modWheelFactor/1000));
					
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,
						leadOneChannel,leadOneChip);
				}
			}
			else { 
				insideVibFactor = 1.0;
			}
		break;
    
		case 2: // Note On
			if ((arpeggiatorOn == LOW) || (noteComingFromArpeggiator == HIGH)) {
				if (insideRun == LOW) {
					insidePitchInHertz = 440.00 * 
						pow(2, ((noteNumber - 69) / 12.00));
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,
						leadOneChannel,leadOneChip);
					if (arpeggiatorOn == LOW) {
						placeNoteInFirstEmptyArrayPosition(noteNumberArray,
							noteDurationArray,noteNumber);
					}
					insideRun = HIGH;
					writeWaveform((waveformCombo | 0x1),
						leadOneChannel,leadOneChip);
					portamentoRunning = LOW;
				}
				else if ((insideRun == HIGH) && (arpeggiatorOn == LOW)) {
					insideTargetPitchInHertz = 440.00 * 
						pow(2, ((noteNumber - 69) / 12.00));
					portamentoRunning = HIGH;
					placeNoteInFirstEmptyArrayPosition(noteNumberArray,
						noteDurationArray,noteNumber);
					if (insideTargetPitchInHertz > insidePitchInHertz) {
						portamentoDirection = HIGH;
					}
					else {
						portamentoDirection = LOW;
					}
				} 
			}
		break;
      
		case 3: // Note Off
			if ((arpeggiatorOn == LOW) || (noteComingFromArpeggiator == HIGH)) {
				if (arpeggiatorOn == LOW) {
					eraseThisNoteInArray(noteNumberArray,noteDurationArray,noteNumber);
					if (mostRecentNoteInArray(noteNumberArray,
						noteDurationArray) == 0) {
         
						insideRun = LOW;
						writeWaveform((waveformCombo & B11111110),
							leadOneChannel,leadOneChip);
					}
					else {
						mainLeadOneEngine(2,mostRecentNoteInArray(noteNumberArray,
							noteDurationArray),0,LOW,0,0);
					}
				}
      
				if ((arpeggiatorOn == HIGH) && (arpNoteNumberFromArray == 0)) {
					insideRun = LOW;
					writeWaveform((waveformCombo & B11111110),
						leadOneChannel,leadOneChip);
				}
     	
     			// If notes are coming from main arp sequencer.
				if ((arpeggiatorOn == HIGH) && (arpNoteNumberFromArray == 1)) { 
					insideRun = LOW;
					writeWaveform((waveformCombo & B11111110),
						leadOneChannel,leadOneChip);
				}
			}
		break;
    
		case 4: // Pitch Bend
			insideBendFactor = floatData;
			insidePitchInHertzAfterFactors = insidePitchInHertz * 
				insideBendFactor * insideVibFactor;
			writeFrequency(insidePitchInHertzAfterFactors,
				leadOneChannel,leadOneChip);
		break;
    
		case 5: // ModWheel
      	insideCC1 = noteNumber;
		break;
    
		case 6:
      break;
    
		case 7: // CC5 Arp Speed Change
		break;
    
		case 8: // Arpeggiator Has Turned On Or Off.  Receive or transmit array.
			if (noteComingFromArpeggiator == HIGH) {
				arpeggiatorOn = HIGH;
				for (byte i=0;i<8;i++) {
					leadOneArpeggiator(6,i,0,noteNumberArray[i],noteDurationArray[i]);
				}
			}
      
			else {
				arpeggiatorOn = LOW;
				noteNumberArray[noteNumber] = arpNoteNumberFromArray;
				noteDurationArray[noteNumber] = arpNoteDurationFromArray;
			}
		break;

		case 9: // Basic Waveform Selection
			basicWaveformNumber = map(noteNumber,0,127,0,7);
			switch (basicWaveformNumber) {
				case 0: // Pulse Only
					basicWaveform = B1000000;
				break;
				
				case 1: // Saw Only
					basicWaveform = B100000;
				break;
				
				case 2: // Triangle Only
					basicWaveform = B10000;
				break;
				
				case 3: // Pulse/Saw
					basicWaveform = B1100000;
				break;
				
				case 4: // Pulse/Triangle
					basicWaveform = B1010000;
				break;
				
				case 5: // Saw/Triangle
					basicWaveform = B110000;
				break;
				
				case 6: // Pulse/Saw/Triangle
					basicWaveform = B1110000;
				break;
				
				case 7: // Noise
					basicWaveform = B10000000;
				break;
			}
			waveformCombo = basicWaveform | waveformModifier;
			if (insideRun == HIGH) {
				writeWaveform((waveformCombo | 0x1),leadOneChannel,leadOneChip);
			}
			else {
				writeWaveform((waveformCombo & B11111110),leadOneChannel,leadOneChip);
			}
		break;

		case 10: // Waveform Modifier Selection
			waveformModifierNumber = map(noteNumber,0,127,0,3);
			switch (waveformModifierNumber) {
				case 0: // Normal
					waveformModifier = B0;
				break;
				
				case 1: // Ring Mod
					waveformModifier = B100;
				break;
				
				case 2: // Sync
					waveformModifier = B10;
				break;
				
				case 3: // Ring Mod and Sync
					waveformModifier = B110;
				break;
			}
			waveformCombo = basicWaveform | waveformModifier;
			if (insideRun == HIGH) {
				writeWaveform((waveformCombo | 0x1),leadOneChannel,leadOneChip);
    		}
			else {
				writeWaveform((waveformCombo & B11111110),leadOneChannel,leadOneChip);
			}
		break;

		case 11: // Attack Amount
			attackAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadOneChannel,leadOneChip);
		break;

		case 12: // Decay Amount
			decayAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadOneChannel,leadOneChip);
		break;

		case 13: // Sustain Amount
			sustainAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadOneChannel,leadOneChip);
		break;

		case 14: // Release Amount
			releaseAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadOneChannel,leadOneChip);
		break;

		case 15: // PWM Amount
			pwmAmount = map(noteNumber,0,127,2048,0);
			writePWM(pwmAmount,leadOneChannel,leadOneChip);
		break;

		case 16: // Portamento Speed
			portamentoSpeedInMicros = map(noteNumber,0,127,1,2000);
		break;
    
	}
}




void leadTwoArpeggiator(byte dataType, byte noteNumber, 
	byte noteVelocity, byte noteNumberFromArray, 
	unsigned long noteDurationFromArray) {
  
	static byte arpNoteNumberArray[8];
	static unsigned long arpNoteDurationArray[8];
	static boolean arpeggiatorOn = LOW;
	unsigned long currentMillis;
	static unsigned long previousMillis;
	unsigned long elapsedTimeInMillis;
	static unsigned int arpeggiatorInterval = 20;
	static byte currentArpArrayPosition;
	static boolean arpeggiatorRunning = LOW;
	byte transitionNote;

	switch (dataType) {
		case 1: // Main Loop Run
      	if ((arpeggiatorOn == HIGH) && (arpeggiatorRunning == HIGH)) {
				currentMillis = millis();
				elapsedTimeInMillis = currentMillis - previousMillis;
				if (elapsedTimeInMillis >= arpeggiatorInterval) {
					mainLeadTwoEngine(3,arpNoteNumberArray[currentArpArrayPosition],
						0,HIGH,1,0);
					if (currentArpArrayPosition < 7) {
						currentArpArrayPosition++;
					}
					else {
						currentArpArrayPosition = 0;
					}
					while (arpNoteNumberArray[currentArpArrayPosition] == 0) {
						if (currentArpArrayPosition < 7) {
							currentArpArrayPosition++;
						}
						else {
							currentArpArrayPosition = 0;
						}
					}
					mainLeadTwoEngine(2,arpNoteNumberArray[currentArpArrayPosition],
						0,HIGH,1,0);
					previousMillis = millis();
				}
			}
		break;
    
		case 2: // Note On
			if (arpeggiatorOn == HIGH) {
				placeNoteInFirstEmptyArrayPosition(arpNoteNumberArray,
					arpNoteDurationArray,noteNumber);
				arpeggiatorRunning = testArrayContentsForAtLeastTwoNotes(
					arpNoteNumberArray) ? HIGH : LOW;
				if (arpeggiatorRunning == LOW) {
					mainLeadTwoEngine(2,noteNumber,0,HIGH,0,0);
				}
			}
		break;
	    
		case 3: // Note Off
			if (arpeggiatorOn == HIGH) {
				eraseThisNoteInArray(arpNoteNumberArray,
					arpNoteDurationArray,noteNumber);
				arpeggiatorRunning = testArrayContentsForAtLeastTwoNotes(
					arpNoteNumberArray) ? HIGH : LOW;
				if (arpeggiatorRunning == LOW) {
					mainLeadTwoEngine(3,noteNumber,0,HIGH,0,0);
					if (testArrayContentsForNoNotes(arpNoteNumberArray) == LOW) {
						mainLeadTwoEngine(2,findSingleNoteInArray(arpNoteNumberArray),
							0,HIGH,0,0);
					}
				}
			}
		break;
	    
		case 4: // CC4: Arpeggiator On/Off
			if ((noteNumber > 0) && (arpeggiatorOn == LOW)) {
				arpeggiatorOn = HIGH;
				mainLeadTwoEngine(8,0,0,HIGH,0,0);
				arpeggiatorRunning = testArrayContentsForAtLeastTwoNotes(
					arpNoteNumberArray) ? HIGH : LOW;
			}
			if (noteNumber == 0) {
				arpeggiatorOn = LOW;
				mainLeadTwoEngine(3,arpNoteNumberArray[currentArpArrayPosition],
					0,HIGH,0,0);
				transitionNote = mostRecentNoteInArray(arpNoteNumberArray,
					arpNoteDurationArray);
				if (transitionNote > 0) {
					mainLeadTwoEngine(2,transitionNote,0,HIGH,0,0);
				}
				for (int i=0;i<8;i++) {
					mainLeadTwoEngine(8,i,0,LOW,arpNoteNumberArray[i],arpNoteDurationArray[i]);
				}
			}
		break;
    
		case 5: // Changes Arpeggiator Speed
			arpeggiatorInterval = map(noteNumber,0,127,20,200);
		break;
    
		case 6: // Accepts noteNumberArray and noteDurationArray
			arpNoteNumberArray[noteNumber] = noteNumberFromArray;
			arpNoteDurationArray[noteNumber] = noteDurationFromArray;
		break;
	}
}






void mainLeadTwoEngine(byte dataType, byte noteNumber, float floatData, 
	boolean noteComingFromArpeggiator, byte arpNoteNumberFromArray, 
	unsigned long arpNoteDurationFromArray) {

	static boolean insideRun = LOW;
	static byte noteNumberArray[8];
	static unsigned long noteDurationArray[8];
	static boolean portamentoOn = HIGH;
	static boolean portamentoRunning = LOW;
	static boolean portamentoDirection;
	unsigned long currentMicros;
	static unsigned long previousPortamentoChange;
	static unsigned long portamentoSpeedInMicros = 50;
	float portamentoStepInHertz;
	static float insidePitchInHertz;
	static float insideTargetPitchInHertz;
	static float insidePitchInHertzAfterFactors;
	static float insideBendFactor = 1.0;
	static boolean arpeggiatorOn = LOW;
	// static unsigned long decayInterval;
	// static boolean decayOn = LOW;
	// static byte currentDecayLevel = 127;
	// static unsigned long previousDecayMicros;
	static const byte leadTwoChannel = 2;
	static const byte leadTwoChip = 0;   
      
	static float insideVibFactor = 1.0;
	static byte insideCC1 = 0;
	static float vibRateDivisor = 23.0;
	unsigned long currentVibCountMicros;
	static unsigned long sineVibSpeedStepsInMicros = 2000;
	static int vibAmount = 0;
	static float vibAmount2 = 0.0;
	static float vibFactor = 1.0;
	static int modWheelFactor = 0;
	static unsigned long previousVibCountMicros = 0;
	static unsigned long currentMillis;
	
	static byte attackAmount = 0;
	static byte decayAmount = 0;
	static byte sustainAmount = 15;
	static byte releaseAmount = 0;
	static unsigned int adsr = 0xF0;

	static byte basicWaveform = B1000000;
	byte basicWaveformNumber = 0;
	static byte waveformModifier = B0;
	byte waveformModifierNumber = 0;
	static byte waveformCombo = B1000000;

	static unsigned int pwmAmount = 2048;

	switch (dataType) {
		case 1: // Main Loop Run
			if ((insideRun == HIGH) && (portamentoOn == HIGH) && 
				(portamentoRunning == HIGH)) {
				
				currentMicros = micros();
				if ((portamentoDirection == LOW) && (currentMicros - 
					previousPortamentoChange >= portamentoSpeedInMicros)) {
					
					portamentoStepInHertz = 2.0 * (insidePitchInHertz / 1000.0);
					insidePitchInHertz = insidePitchInHertz - portamentoStepInHertz;
					previousPortamentoChange = micros();
            
					if (insidePitchInHertz <= insideTargetPitchInHertz) {
						portamentoRunning = LOW;
						insidePitchInHertz = insideTargetPitchInHertz;
					}
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,
						leadTwoChannel,leadTwoChip);
				}
				if ((portamentoDirection == HIGH) && (currentMicros - 
					previousPortamentoChange >= portamentoSpeedInMicros)) {
					
					portamentoStepInHertz = 2.0 * (insidePitchInHertz / 1000.0);
					insidePitchInHertz = insidePitchInHertz + portamentoStepInHertz;
					previousPortamentoChange = micros();
					if (insidePitchInHertz >= insideTargetPitchInHertz) {
						portamentoRunning = LOW;
						insidePitchInHertz = insideTargetPitchInHertz;
					}
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,
						leadTwoChannel,leadTwoChip);
				}
			}
			if (insideCC1 > 0) {
				currentVibCountMicros = micros();

				// Limits how often the chip can calculate the sine function.
				if  ((currentVibCountMicros - previousVibCountMicros) >= 
					sineVibSpeedStepsInMicros) { 
					
					currentMillis = millis();
					
					//range of  1 to -1, sclaed to time, factored to 100 to -100
					vibAmount = sin(((float)currentMillis * 
						(float)vibRateFactor)/(float)vibRateDivisor) * 100.0; 
					previousVibCountMicros = micros();
					
					//Scales vibAmount to very small positive or negative amounts
					vibAmount2 = (float)vibAmount * vibDepth; 
					modWheelFactor = map(insideCC1, 0, 127, 0, 2000);
					insideVibFactor = 1.0 + (vibAmount2 * 
						((float)modWheelFactor/1000));
                  
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,leadTwoChannel,
						leadTwoChip);
				}
			}
			else { 
				insideVibFactor = 1.0;
			}
		break;
    
		case 2: // Note On
			if ((arpeggiatorOn == LOW) || (noteComingFromArpeggiator == HIGH)) {
				if (insideRun == LOW) {
					insidePitchInHertz = 440.00 * 
						pow(2, ((noteNumber - 69) / 12.00));
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,
						leadTwoChannel,leadTwoChip);
					if (arpeggiatorOn == LOW) {
						placeNoteInFirstEmptyArrayPosition(noteNumberArray,
							noteDurationArray,noteNumber);
					}
					insideRun = HIGH;
					writeWaveform((waveformCombo | 0x1),
						leadTwoChannel,leadTwoChip);
					portamentoRunning = LOW;
				}
				else if ((insideRun == HIGH) && (arpeggiatorOn == LOW)) {
					insideTargetPitchInHertz = 440.00 * 
						pow(2, ((noteNumber - 69) / 12.00));
					portamentoRunning = HIGH;
					placeNoteInFirstEmptyArrayPosition(noteNumberArray,
						noteDurationArray,noteNumber);
					if (insideTargetPitchInHertz > insidePitchInHertz) {
						portamentoDirection = HIGH;
					}
					else {
						portamentoDirection = LOW;
					}
				}
			}
		break;
      
		case 3: // Note Off
			if ((arpeggiatorOn == LOW) || (noteComingFromArpeggiator == HIGH)) {
				if (arpeggiatorOn == LOW) {
					eraseThisNoteInArray(noteNumberArray,
						noteDurationArray,noteNumber);
					if (mostRecentNoteInArray(noteNumberArray,
						noteDurationArray) == 0) {
						
						insideRun = LOW;
						writeWaveform((waveformCombo & B11111110),
							leadTwoChannel,leadTwoChip);
					}
					else {
						mainLeadTwoEngine(2,mostRecentNoteInArray(noteNumberArray,
							noteDurationArray),0,LOW,0,0);
					}
				}
				if ((arpeggiatorOn == HIGH) && (arpNoteNumberFromArray == 0)) {
					insideRun = LOW;
					writeWaveform((waveformCombo & B11111110),
						leadTwoChannel,leadTwoChip);
				}
				
				// If notes are coming from main arp sequencer.
				if ((arpeggiatorOn == HIGH) && (arpNoteNumberFromArray == 1)) { 
					insideRun = LOW;
					writeWaveform((waveformCombo & B11111110),
						leadTwoChannel,leadTwoChip);
				}
			}
		break;
    
		case 4: // Pitch Bend
			insideBendFactor = floatData;
			insidePitchInHertzAfterFactors = insidePitchInHertz * 
				insideBendFactor * insideVibFactor;
			writeFrequency(insidePitchInHertzAfterFactors,
				leadTwoChannel,leadTwoChip);
		break;
    
		case 5: // ModWheel
			insideCC1 = noteNumber;
		break;
    
		case 6: // 
      break;
    
		case 7: // CC5 Arp Speed Change
		break;
    
		case 8: // Arpeggiator Has Turned On Or Off.  Receive or transmit array.
			if (noteComingFromArpeggiator == HIGH) {
				arpeggiatorOn = HIGH;
				for (byte i=0;i<8;i++) {
					leadTwoArpeggiator(6,i,0,noteNumberArray[i],
						noteDurationArray[i]);
				}
			}
			else {
				arpeggiatorOn = LOW;
				noteNumberArray[noteNumber] = arpNoteNumberFromArray;
				noteDurationArray[noteNumber] = arpNoteDurationFromArray;
			}
		break;

		case 9: // Basic Waveform Selection
			basicWaveformNumber = map(noteNumber,0,127,0,7);
			switch (basicWaveformNumber) {
				case 0: // Pulse Only
					basicWaveform = B1000000;
				break;
				
				case 1: // Saw Only
					basicWaveform = B100000;
				break;
				
				case 2: // Triangle Only
					basicWaveform = B10000;
				break;
				
				case 3: // Pulse/Saw
					basicWaveform = B1100000;
				break;
				
				case 4: // Pulse/Triangle
					basicWaveform = B1010000;
				break;
				
				case 5: // Saw/Triangle
					basicWaveform = B110000;
				break;
				
				case 6: // Pulse/Saw/Triangle
					basicWaveform = B1110000;
				break;
				
				case 7: // Noise
					basicWaveform = B10000000;
				break;
			}
			waveformCombo = basicWaveform | waveformModifier;
			if (insideRun == HIGH) {
				writeWaveform((waveformCombo | 0x1),
					leadTwoChannel,leadTwoChip);
			}
			else {
				writeWaveform((waveformCombo & B11111110),
					leadTwoChannel,leadTwoChip);
			}
		break;

		case 10: // Waveform Modifier Selection
			waveformModifierNumber = map(noteNumber,0,127,0,3);
			switch (waveformModifierNumber) {
				case 0: // Normal
					waveformModifier = B0;
				break;
				
				case 1: // Ring Mod
					waveformModifier = B100;
				break;
				
				case 2: // Sync
					waveformModifier = B10;
				break;
				
				case 3: // Ring Mod and Sync
					waveformModifier = B110;
				break;
			}
			waveformCombo = basicWaveform | waveformModifier;
			if (insideRun == HIGH) {
				writeWaveform((waveformCombo | 0x1),
					leadTwoChannel,leadTwoChip);
			}
			else {
				writeWaveform((waveformCombo & B11111110),
					leadTwoChannel,leadTwoChip);
			}
		break;

		case 11: // Attack Amount
			attackAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadTwoChannel,leadTwoChip);
		break;

		case 12: // Decay Amount
			decayAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadTwoChannel,leadTwoChip);
		break;

		case 13: // Sustain Amount
			sustainAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadTwoChannel,leadTwoChip);
		break;

		case 14: // Release Amount
			releaseAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadTwoChannel,leadTwoChip);
		break;

		case 15: // PWM Amount
			pwmAmount = map(noteNumber,0,127,2048,0);
			writePWM(pwmAmount,leadTwoChannel,leadTwoChip);
		break;

		case 16: // Portamento Speed
			portamentoSpeedInMicros = map(noteNumber,0,127,1,2000);
		break;
	}
}





void leadThreeArpeggiator(byte dataType, byte noteNumber, 
	byte noteVelocity, byte noteNumberFromArray, 
	unsigned long noteDurationFromArray) {
 
 	static byte arpNoteNumberArray[8];
	static unsigned long arpNoteDurationArray[8];
	static boolean arpeggiatorOn = LOW;
	unsigned long currentMillis;
	static unsigned long previousMillis;
	unsigned long elapsedTimeInMillis;
	static unsigned int arpeggiatorInterval = 20;
	static byte currentArpArrayPosition;
	static boolean arpeggiatorRunning = LOW;
	byte transitionNote;

	switch (dataType) {
		case 1: // Main Loop Run
			if ((arpeggiatorOn == HIGH) && (arpeggiatorRunning == HIGH)) {
				currentMillis = millis();
				elapsedTimeInMillis = currentMillis - previousMillis;
				if (elapsedTimeInMillis >= arpeggiatorInterval) {
					mainLeadThreeEngine(3,arpNoteNumberArray[currentArpArrayPosition],
						0,HIGH,1,0);
					if (currentArpArrayPosition < 7) {
						currentArpArrayPosition++;
					}
					else {
						currentArpArrayPosition = 0;
					}
					while (arpNoteNumberArray[currentArpArrayPosition] == 0) {
						if (currentArpArrayPosition < 7) {
							currentArpArrayPosition++;
						}
						else {
							currentArpArrayPosition = 0;
						}
					}
					mainLeadThreeEngine(2,arpNoteNumberArray[currentArpArrayPosition],
						0,HIGH,1,0);
					previousMillis = millis();
				}
			}
		break;
    
		case 2: // Note On
			if (arpeggiatorOn == HIGH) {
				placeNoteInFirstEmptyArrayPosition(arpNoteNumberArray,
					arpNoteDurationArray,noteNumber);
				arpeggiatorRunning = testArrayContentsForAtLeastTwoNotes(
					arpNoteNumberArray) ? HIGH : LOW;
				if (arpeggiatorRunning == LOW) {
					mainLeadThreeEngine(2,noteNumber,0,HIGH,0,0);
				}
			}
		break;
    
		case 3: // Note Off
			if (arpeggiatorOn == HIGH) {
				eraseThisNoteInArray(arpNoteNumberArray,
					arpNoteDurationArray,noteNumber);
				arpeggiatorRunning = testArrayContentsForAtLeastTwoNotes(
					arpNoteNumberArray) ? HIGH : LOW;
				if (arpeggiatorRunning == LOW) {
					mainLeadThreeEngine(3,noteNumber,0,HIGH,0,0);
					if (testArrayContentsForNoNotes(arpNoteNumberArray) == LOW) {
						mainLeadThreeEngine(2,findSingleNoteInArray(
							arpNoteNumberArray),0,HIGH,0,0);
					}
				}
			}
		break;
    
		case 4: // CC4: Arpeggiator On/Off
			if ((noteNumber > 0) && (arpeggiatorOn == LOW)) {
				arpeggiatorOn = HIGH;
				mainLeadThreeEngine(8,0,0,HIGH,0,0);
				arpeggiatorRunning = testArrayContentsForAtLeastTwoNotes(
					arpNoteNumberArray) ? HIGH : LOW;
			}
			if (noteNumber == 0) {
				arpeggiatorOn = LOW;
				mainLeadThreeEngine(3,arpNoteNumberArray[currentArpArrayPosition],
					0,HIGH,0,0);
				transitionNote = mostRecentNoteInArray(arpNoteNumberArray,
					arpNoteDurationArray);
				if (transitionNote > 0) {
					mainLeadThreeEngine(2,transitionNote,0,HIGH,0,0);
				}
				for (int i=0;i<8;i++) {
					mainLeadThreeEngine(8,i,0,LOW,arpNoteNumberArray[i],
						arpNoteDurationArray[i]);
				}
			}
		break;
    
		case 5: // Changes Arpeggiator Speed
			arpeggiatorInterval = map(noteNumber,0,127,20,200);
		break;
    
		case 6: // Accepts noteNumberArray and noteDurationArray
			arpNoteNumberArray[noteNumber] = noteNumberFromArray;
			arpNoteDurationArray[noteNumber] = noteDurationFromArray;
		break;
	}
}




void mainLeadThreeEngine(byte dataType, byte noteNumber, float floatData, 
	boolean noteComingFromArpeggiator, byte arpNoteNumberFromArray, 
	unsigned long arpNoteDurationFromArray) {
  	  
	static boolean insideRun = LOW;
	static byte noteNumberArray[8];
	static unsigned long noteDurationArray[8];
	static boolean portamentoOn = HIGH;
	static boolean portamentoRunning = LOW;
	static boolean portamentoDirection;
	unsigned long currentMicros;
	static unsigned long previousPortamentoChange;
	static unsigned long portamentoSpeedInMicros = 50;
	float portamentoStepInHertz;
	static float insidePitchInHertz;
	static float insideTargetPitchInHertz;
	static float insidePitchInHertzAfterFactors;
	static float insideBendFactor = 1.0;
	static boolean arpeggiatorOn = LOW;
	static const byte leadThreeChannel = 3;
	static const byte leadThreeChip = 0;   
      
	static float insideVibFactor = 1.0;
	static byte insideCC1 = 0;
	static float vibRateDivisor = 23.0;
	unsigned long currentVibCountMicros;
	static unsigned long sineVibSpeedStepsInMicros = 2000;
	static int vibAmount = 0;
	static float vibAmount2 = 0.0;
	static float vibFactor = 1.0;
	static int modWheelFactor = 0;
	static unsigned long previousVibCountMicros = 0;
	static unsigned long currentMillis;
      
	static byte attackAmount = 0;
	static byte decayAmount = 0;
	static byte sustainAmount = 15;
	static byte releaseAmount = 0;
	static unsigned int adsr = 0xF0;

	static byte basicWaveform = B1000000;
	byte basicWaveformNumber = 0;
	static byte waveformModifier = B0;
	byte waveformModifierNumber = 0;
	static byte waveformCombo = B1000000;

	static unsigned int pwmAmount = 2048;

	switch (dataType) {
		case 1: // Main Loop Run
			if ((insideRun == HIGH) && (portamentoOn == HIGH) && 
				(portamentoRunning == HIGH)) {

				currentMicros = micros();
				if ((portamentoDirection == LOW) && (currentMicros - 
					previousPortamentoChange >= portamentoSpeedInMicros)) {

					portamentoStepInHertz = 2.0 * (insidePitchInHertz / 1000.0);
					insidePitchInHertz = insidePitchInHertz - portamentoStepInHertz;
					previousPortamentoChange = micros();
            
					if (insidePitchInHertz <= insideTargetPitchInHertz) {
						portamentoRunning = LOW;
						insidePitchInHertz = insideTargetPitchInHertz;
					}
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,
						leadThreeChannel,leadThreeChip);
				}
				if ((portamentoDirection == HIGH) && (currentMicros - 
					previousPortamentoChange >= portamentoSpeedInMicros)) {

					portamentoStepInHertz = 2.0 * (insidePitchInHertz / 1000.0);
					insidePitchInHertz = insidePitchInHertz + portamentoStepInHertz;
					previousPortamentoChange = micros();
					if (insidePitchInHertz >= insideTargetPitchInHertz) {
						portamentoRunning = LOW;
						insidePitchInHertz = insideTargetPitchInHertz;
					}
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,
						leadThreeChannel,leadThreeChip);
				}
			}
			if (insideCC1 > 0) {  
				currentVibCountMicros = micros();

				// Limits how often the chip can calculate the sine function.
				if ((currentVibCountMicros - previousVibCountMicros) >= 
					sineVibSpeedStepsInMicros) { 
                  
					currentMillis = millis();
					
					//range of  1 to -1, sclaed to time, factored to 100 to -100
					vibAmount = sin(((float)currentMillis * 
						(float)vibRateFactor)/(float)vibRateDivisor) * 100.0; 
					previousVibCountMicros = micros();
                  
					//Scales vibAmount to very small positive or negative amounts
					vibAmount2 = (float)vibAmount * vibDepth; 
					modWheelFactor = map(insideCC1, 0, 127, 0, 2000);
					insideVibFactor = 1.0 + (vibAmount2 * 
						((float)modWheelFactor/1000));
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,
						leadThreeChannel,leadThreeChip);
				}
			}
			else { 
				insideVibFactor = 1.0;
			}   
		break;
    
		case 2: // Note On
			if ((arpeggiatorOn == LOW) || (noteComingFromArpeggiator == HIGH)) {
				if (insideRun == LOW) {
					insidePitchInHertz = 440.00 * 
						pow(2, ((noteNumber - 69) / 12.00));
					insidePitchInHertzAfterFactors = insidePitchInHertz * 
						insideBendFactor * insideVibFactor;
					writeFrequency(insidePitchInHertzAfterFactors,
						leadThreeChannel,leadThreeChip);
					if (arpeggiatorOn == LOW) {
						placeNoteInFirstEmptyArrayPosition(noteNumberArray,
							noteDurationArray,noteNumber);
					}
					insideRun = HIGH;
					writeWaveform((waveformCombo | 0x1),
						leadThreeChannel,leadThreeChip);
					portamentoRunning = LOW;
				}
				else if ((insideRun == HIGH) && (arpeggiatorOn == LOW)) {
					insideTargetPitchInHertz = 440.00 * 
						pow(2, ((noteNumber - 69) / 12.00));
					portamentoRunning = HIGH;
					placeNoteInFirstEmptyArrayPosition(noteNumberArray,
						noteDurationArray,noteNumber);
					if (insideTargetPitchInHertz > insidePitchInHertz) {
						portamentoDirection = HIGH;
					}
					else {
						portamentoDirection = LOW;
					}
				}
			}
		break;
      
		case 3: // Note Off
			if ((arpeggiatorOn == LOW) || (noteComingFromArpeggiator == HIGH)) {
				if (arpeggiatorOn == LOW) {
					eraseThisNoteInArray(noteNumberArray,
						noteDurationArray,noteNumber);
					if (mostRecentNoteInArray(noteNumberArray,
						noteDurationArray) == 0) {
						
						insideRun = LOW;
						writeWaveform((waveformCombo & B11111110),
							leadThreeChannel,leadThreeChip);
					}
					else {
						mainLeadThreeEngine(2,mostRecentNoteInArray(noteNumberArray,
							noteDurationArray),0,LOW,0,0);
					}
				}
				if ((arpeggiatorOn == HIGH) && (arpNoteNumberFromArray == 0)) {
					insideRun = LOW;
					writeWaveform((waveformCombo & B11111110),
						leadThreeChannel,leadThreeChip);
				}
			
				// If notes are coming from main arp sequencer.
				if ((arpeggiatorOn == HIGH) && (arpNoteNumberFromArray == 1)) { 
					insideRun = LOW;
					writeWaveform((waveformCombo & B11111110),
						leadThreeChannel,leadThreeChip);
				}
			}
		break;
    
		case 4: // Pitch Bend
			insideBendFactor = floatData;
			insidePitchInHertzAfterFactors = insidePitchInHertz * 
				insideBendFactor * insideVibFactor;
			writeFrequency(insidePitchInHertzAfterFactors,
				leadThreeChannel,leadThreeChip);
		break;
    
		case 5: // ModWheel
      	insideCC1 = noteNumber;
		break;
    
		case 6: // 
      break;
    
		case 7: // CC5 Arp Speed Change
		break;
    
		case 8: // Arpeggiator Has Turned On Or Off.  Receive or transmit array.
      	if (noteComingFromArpeggiator == HIGH) {
				arpeggiatorOn = HIGH;
				for (byte i=0;i<8;i++) {
					leadThreeArpeggiator(6,i,0,noteNumberArray[i],
						noteDurationArray[i]);
				}
			}
			else {
				arpeggiatorOn = LOW;
				noteNumberArray[noteNumber] = arpNoteNumberFromArray;
				noteDurationArray[noteNumber] = arpNoteDurationFromArray;
			}
		break;

		case 9: // Basic Waveform Selection
			basicWaveformNumber = map(noteNumber,0,127,0,7);
			switch (basicWaveformNumber) {
				case 0: // Pulse Only
					basicWaveform = B1000000;
				break;
				
				case 1: // Saw Only
					basicWaveform = B100000;
				break;
				
				case 2: // Triangle Only
					basicWaveform = B10000;
				break;
				
				case 3: // Pulse/Saw
					basicWaveform = B1100000;
				break;
				
				case 4: // Pulse/Triangle
					basicWaveform = B1010000;
				break;
				
				case 5: // Saw/Triangle
					basicWaveform = B110000;
				break;
				
				case 6: // Pulse/Saw/Triangle
					basicWaveform = B1110000;
				break;
				
				case 7: // Noise
					basicWaveform = B10000000;
				break;
			}
			waveformCombo = basicWaveform | waveformModifier;
			if (insideRun == HIGH) {
				writeWaveform((waveformCombo | 0x1),
					leadThreeChannel,leadThreeChip);
			}
			else {
				writeWaveform((waveformCombo & B11111110),
					leadThreeChannel,leadThreeChip);
			}
		break;

		case 10: // Waveform Modifier Selection
			waveformModifierNumber = map(noteNumber,0,127,0,3);
			switch (waveformModifierNumber) {
				case 0: // Normal
					waveformModifier = B0;
				break;
				
				case 1: // Ring Mod
					waveformModifier = B100;
				break;
				
				case 2: // Sync
					waveformModifier = B10;
				break;
				
				case 3: // Ring Mod and Sync
					waveformModifier = B110;
				break;
			}
			waveformCombo = basicWaveform | waveformModifier;
			if (insideRun == HIGH) {
				writeWaveform((waveformCombo | 0x1),
					leadThreeChannel,leadThreeChip);
			}
			else {
				writeWaveform((waveformCombo & B11111110),
					leadThreeChannel,leadThreeChip);
			}
		break;

		case 11: // Attack Amount
			attackAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadThreeChannel,leadThreeChip);
		break;

		case 12: // Decay Amount
			decayAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadThreeChannel,leadThreeChip);
		break;

		case 13: // Sustain Amount
			sustainAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadThreeChannel,leadThreeChip);
		break;

		case 14: // Release Amount
			releaseAmount = map(noteNumber,0,127,0,15);
			adsr = (attackAmount << 12) | (decayAmount << 8) | 
				(sustainAmount << 4) | releaseAmount;
			writeADSR(adsr,leadThreeChannel,leadThreeChip);
		break;

		case 15: // PWM Amount
			pwmAmount = map(noteNumber,0,127,2048,0);
			writePWM(pwmAmount,leadThreeChannel,leadThreeChip);
		break;

		case 16: // Portamento Speed
			portamentoSpeedInMicros = map(noteNumber,0,127,1,2000);
		break;
	}
}





//////////////////////////////////////////
//  MISC FUNCTIONS
//////////////////////////////////////////


void ledFlash(byte dataType) {

	static unsigned long currentMillis; 
	static boolean ledOn = LOW;
	static unsigned long ledTurnOffTime; 
	static unsigned int ledOnLength = 10;

	switch (dataType) {
		case 1: // If the LED is scheduled to turn off, turn it off.
			if (ledOn == HIGH) {
				currentMillis = millis();
				if (currentMillis >= ledTurnOffTime) {
					digitalWrite(11, LOW);
				}
			}
		break;

		case 2: // Turn the LED on and set the time it is scheduled to turn off.
			digitalWrite(11,HIGH);
			ledOn = HIGH;
			currentMillis = millis();
			ledTurnOffTime = currentMillis + ledOnLength;
		break;
	}
}






//////////////////////////////////////////
//  SID FUNCTIONS
//////////////////////////////////////////


void writeFrequency(float pitchInHertz, byte channel, byte chip) {  
	// 0-65535 in Hz

	byte addressLowByte[3] = {0x0,0x7,0xE};
	byte addressHighByte[3] = {0x1,0x8,0xF};
	byte lowByteOfFrequency;
	byte highByteOfFrequency;
	unsigned int frequencyValue;
	
	channel = channel - 1;
	
	frequencyValue = pitchInHertz / 0.0596;
	
	lowByteOfFrequency = frequencyValue & 0xFF;
	PORTD = addressLowByte[channel];
	PORTB = lowByteOfFrequency;
	pulseCS(chip);

	highByteOfFrequency = frequencyValue >> 0x8;	
	PORTD = addressHighByte[channel];
	PORTB = highByteOfFrequency;
	pulseCS(chip);
}



void writePWM(unsigned int width, byte channel, byte chip) {  
	// 0-4095
	
	byte addressLowByte[3] = {0x2,0x9,0x10};
	byte addressHighByte[3] = {0x3,0xA,0x11};
	byte lowByteOfWidth;
	byte highByteOfWidth;
	
	channel = channel - 1;

	lowByteOfWidth = width & 0xFF;
	PORTD = addressLowByte[channel];
	PORTB = lowByteOfWidth;
	pulseCS(chip);

	highByteOfWidth = width >> 0x8;
	PORTD = addressHighByte[channel];
	PORTB = highByteOfWidth;
	pulseCS(chip);
}





void writeWaveform(byte waveform, byte channel, byte chip) { 

	// (MSB -> LSB) Noise, Pulse, Saw, Triangle, ZERO, Ring Mod, Sync, Gate

	byte controlAddress[3] = {0x4,0xB,0x12};
	
	channel = channel - 1;

	PORTD = controlAddress[channel];
	PORTB = waveform & B11110111;
	pulseCS(chip);
}




void writeADSR(unsigned int adsr, byte channel, byte chip) { 
	// A Nibble For Each Component (A is MSB, R is LSB)
	
	byte addressLowByte[3] = {0x6, 0xD, 0x14};
	byte addressHighByte[3] = {0x5, 0xC, 0x13};
	byte lowByteOfADSR;
	byte highByteOfADSR;
	
	channel = channel - 1;

	lowByteOfADSR = adsr & 0xFF;
	PORTD = addressLowByte[channel];
	PORTB = lowByteOfADSR;
	pulseCS(chip);

	highByteOfADSR = adsr >> 0x8;
	PORTD = addressHighByte[channel];
	PORTB = highByteOfADSR;
	pulseCS(chip);
}




void writeFilter(byte dataType, unsigned int uIntData, byte chip) {
	static byte lowByteOfCutoff = B00000000;
	static byte highByteOfCutoff = B00000000;
	static boolean filterOnOne = 0;
	static boolean filterOnTwo = 0;
	static boolean filterOnThree = 0;
	static byte resonanceNibble = 0;
	static byte volumeNibble = 15;
	static byte filterType = B00010000;
	static byte threeMute = B00000000;
	static byte filterExternal = 0;

	switch (dataType) {
		case 0: // Initialization
			PORTD = 0x15;
			PORTB = B00000000;
			pulseCS(chip);

			PORTD = 0x16;
			PORTB = B01100100;
			pulseCS(chip);

			PORTD = 0x17;
			PORTB = B00000000;
			pulseCS(chip);

			PORTD = 0x18;
			PORTB = B00011111;
			pulseCS(chip);
		break;
		
		case 1: // Running
		break;
		
		case 2: // Turn Filter On For A Channel (Make second argument 1-3 to turn on, 4-6 to turn off.)
			switch (uIntData) {
				case 1: 
					filterOnOne = 1;
				break;
				case 2: 
					filterOnTwo = 1;
				break;
				case 3: 
					filterOnThree = 1;
				break;
				case 4: 
					filterOnOne = 0;
				break;
				case 5: 
					filterOnTwo = 0;
				break;
				case 6: 
					filterOnThree = 0;
				break;
			}
			PORTD = 0x17;
			PORTB = filterOnOne | (filterOnTwo << 1) | (filterOnThree << 2) | 
				(filterExternal << 3) | (resonanceNibble << 4);
			pulseCS(chip);
		break;

		case 3: // Set Cutoff Frequency (0-2047)
			if (uIntData > 2047){
				uIntData = 2047;
			}
			lowByteOfCutoff = uIntData & 0x7;
			PORTD = 0x15;
			PORTB = lowByteOfCutoff;
			pulseCS(chip);

			highByteOfCutoff = uIntData >> 0x03;
			PORTD = 0x16;
			PORTB = highByteOfCutoff;
			pulseCS(chip);
		break;

		case 4: // Set Resonance (0-15)
			if (uIntData > 15){
				uIntData = 15;
			}
			resonanceNibble = uIntData & B00001111;
			
			PORTD = 0x17;
			PORTB = filterOnOne | (filterOnTwo << 1) | (filterOnThree << 2) | 
				(filterExternal << 3) | (resonanceNibble << 4);
			pulseCS(chip);
		break;

		case 5: // Set Volume (0-15)
			if (uIntData > 15){
				uIntData = 15;
			}
			volumeNibble = uIntData & B00001111;
			
			PORTD = 0x18;
			PORTB = volumeNibble | filterType | threeMute;
			pulseCS(chip);
		break;

		case 6: // Set Filter Type (1-3)
			if (uIntData == 1){ // LP
				filterType = B00010000;
			}
			if (uIntData == 2){ // BP
				filterType = B00100000;
			}
			if (uIntData == 3){ // HP
				filterType = B01000000;
			}
			
			PORTD = 0x18;
			PORTB = volumeNibble | filterType | threeMute;
			pulseCS(chip);
		break;

		case 7: // Mute Three
			if (uIntData == 1){
				threeMute = B10000000;
			}
			else {
				threeMute = B00000000;
			}

			PORTD = 0x18;
			PORTB = volumeNibble | filterType | threeMute;
			pulseCS(chip);
		break;

		case 8: // Filter External Audio Pin
			if (uIntData == 1){
				filterExternal = 1;
			}
			else {
				filterExternal = 0;
			}
			
			PORTD = 0x17;
			PORTB = filterOnOne | (filterOnTwo << 1) | (filterOnThree << 2) | (filterExternal << 3) | (resonanceNibble << 4);
			pulseCS(chip);
		break;
	}
}





void pulseCS(byte chipNumber) {
	digitalWrite(CSchip0pin,LOW);
	delayMicroseconds(transferHoldTime);
	digitalWrite(CSchip0pin,HIGH);
}






//////////////////////////////////////////
//  MIDI CALLBACK FUNCTIONS
//////////////////////////////////////////


void OnNoteOn (byte channel, byte note, byte velocity) {
	if (noVelocity == HIGH) {
		velocity = 127;
	}
	switch (channel) {
		case 1: //sample engine
			leadOneArpeggiator(2,note,velocity,0,0);
			mainLeadOneEngine(2,note,0,LOW,0,0);  
		break;
    
		case 2:
			leadTwoArpeggiator(2,note,velocity,0,0);
			mainLeadTwoEngine(2,note,0,LOW,0,0); 
		break;
    
		case 3:
			leadThreeArpeggiator(2,note,velocity,0,0);
			mainLeadThreeEngine(2,note,0,LOW,0,0); 
		break;
    
		case 4:
			mainSampleEngine(2,note,velocity,0);	
		break;
    
		case 5:
			mainPolyEngine(2,note,velocity);
		break;
	}
	ledFlash(2);
}




void OnNoteOff(byte channel, byte note, byte velocity) {
	switch (channel) {
		case 1: 
			leadOneArpeggiator(3,note,velocity,0,0);
			mainLeadOneEngine(3,note,0,LOW,0,0);
		break;
    
		case 2:
			leadTwoArpeggiator(3,note,velocity,0,0);
			mainLeadTwoEngine(3,note,0,LOW,0,0);
		break;
    
		case 3:
			leadThreeArpeggiator(3,note,velocity,0,0);
			mainLeadThreeEngine(3,note,0,LOW,0,0);
		break;
    
		case 4:
			mainSampleEngine(3,note,velocity,0);
		break;
    
		case 5:
			mainPolyEngine(3,note,velocity);
      break;
	}
}




void OnControlChange(byte channel, byte control, byte value) {
	switch(control) {

		// Mod wheel, which controls different things for different engines.
		case 1: 
			switch (channel) {
				case 1: 
					mainLeadOneEngine(5, value, 0, LOW, 0, 0);
				break;          
				
				case 2: 
					mainLeadTwoEngine(5, value, 0, LOW, 0, 0);
				break;
				
				case 3: 
					mainLeadThreeEngine(5, value, 0, LOW, 0, 0);
				break;
				
				case 4: 
					mainSampleEngine(5, value, 0, 0);
				break;
				
				case 5: 
					mainPolyEngine(5, value, 0);
				break;
			}
		break;
    
		case 2: // Global pitch bend range change.
			bendRange = map(value,0,127,1,12);
		break;
    
		case 3: 
		break;
    
		case 4: // Arpeggiator On/Off
			switch (channel) {
				case 1: 
					leadOneArpeggiator(4,value,0,0,0);
				break;
				
				case 2: 
					leadTwoArpeggiator(4,value,0,0,0);
				break;
				
				case 3: 
					leadThreeArpeggiator(4,value,0,0,0);
				break;
			}
		break;
    
		case 5: // Arpeggiator Speed
			switch (channel) {
				case 1: 
					leadOneArpeggiator(5,value,0,0,0);
				break;

				case 2: 
					leadTwoArpeggiator(5,value,0,0,0);
				break;

				case 3: 
					leadThreeArpeggiator(5,value,0,0,0);
				break;

				case 4:
				break;

				case 5:
				break;
        
				case 6:
				break;
			}
		break;

		case 6: // Basic Waveform Selection
			switch (channel) {
				case 1: 
					mainLeadOneEngine(9, value, 0, LOW, 0, 0);
				break;
				
				case 2: 
					mainLeadTwoEngine(9, value, 0, LOW, 0, 0);
				break;
				
				case 3: 
					mainLeadThreeEngine(9, value, 0, LOW, 0, 0);
				break;
				
				case 5: 
					mainPolyEngine(9, value, 0);
				break;
			}
		break;

		case 7: // Waveform Modifier
			switch (channel) {
				case 1: 
					mainLeadOneEngine(10, value, 0, LOW, 0, 0);
				break;
				
				case 2: 
					mainLeadTwoEngine(10, value, 0, LOW, 0, 0);
				break;
				
				case 3: 
					mainLeadThreeEngine(10, value, 0, LOW, 0, 0);
				break;
				
				case 5: 
					mainPolyEngine(10, value, 0);
				break;
			}
		break;

		case 8: // Attack Amount
			switch (channel) {
				case 1: 
					mainLeadOneEngine(11, value, 0, LOW, 0, 0);
				break;
				
				case 2: 
					mainLeadTwoEngine(11, value, 0, LOW, 0, 0);
				break;
				
				case 3: 
					mainLeadThreeEngine(11, value, 0, LOW, 0, 0);
				break;
				
				case 5: 
					mainPolyEngine(11, value, 0);
				break;
			}
		break;

		case 9: // Decay Amount
			switch (channel) {
				case 1: 
					mainLeadOneEngine(12, value, 0, LOW, 0, 0);
				break;
				
				case 2: 
					mainLeadTwoEngine(12, value, 0, LOW, 0, 0);
				break;
				
				case 3: 
					mainLeadThreeEngine(12, value, 0, LOW, 0, 0);
				break;
				
				case 5: 
					mainPolyEngine(12, value, 0);
				break;
			}
		break;

		case 10: // Sustain Amount
			switch (channel) {
				case 1: 
					mainLeadOneEngine(13, value, 0, LOW, 0, 0);
				break;
				
				case 2: 
					mainLeadTwoEngine(13, value, 0, LOW, 0, 0);
				break;
				
				case 3: 
					mainLeadThreeEngine(13, value, 0, LOW, 0, 0);
				break;
				
				case 5: 
					mainPolyEngine(13, value, 0);
				break;
			}
		break;

		case 11: // Release Amount
			switch (channel) {
				case 1: 
					mainLeadOneEngine(14, value, 0, LOW, 0, 0);
				break;
				
				case 2: 
					mainLeadTwoEngine(14, value, 0, LOW, 0, 0);
				break;
				
				case 3: 
					mainLeadThreeEngine(14, value, 0, LOW, 0, 0);
				break;
				
				case 5: 
					mainPolyEngine(14, value, 0);
				break;
			}
		break;

		case 12: // PWM Amount
			switch (channel) {
				case 1: 
					mainLeadOneEngine(15, value, 0, LOW, 0, 0);
				break;
				
				case 2: 
					mainLeadTwoEngine(15, value, 0, LOW, 0, 0);
				break;
				
				case 3: 
					mainLeadThreeEngine(15, value, 0, LOW, 0, 0);
				break;
				
				case 5: 
					mainPolyEngine(15, value, 0);
				break;
			}
		break;

		case 13: // Filter Inclusion
			switch (channel) {
				case 1:
					if (value > 0){
						writeFilter(2,1,0);
					}
					else {
						writeFilter(2,4,0);
					}
				break;

				case 2:
					if (value > 0){
						writeFilter(2,2,0);
					}
					else {
						writeFilter(2,5,0);
					}
				break;

				case 3:
					if (value > 0){
						writeFilter(2,3,0);
					}
					else {
						writeFilter(2,6,0);
					}
				break;

				case 5:
					if (value > 0){
						writeFilter(2,1,0);
						writeFilter(2,2,0);
						writeFilter(2,3,0);
					}
					else {
						writeFilter(2,4,0);
						writeFilter(2,5,0);
						writeFilter(2,6,0);
					}
				break;
			}
		break;

		case 14: // Set Cutoff Frequency
			writeFilter(3,map(value,0,127,0,2047),0);
		break;

		case 15: // Set Resonance
			writeFilter(4,map(value,0,127,0,15),0);
		break;

		case 16: // Set Volume
			writeFilter(5,map(value,0,127,0,15),0);
		break;

		case 17: // Set Filter Type
			writeFilter(6,map(value,0,127,1,3),0);
		break;

		case 18: // Mute Three
			if (value > 0){
				writeFilter(7,1,0);
			}
			else {
				writeFilter(7,0,0);
			}
		break;
		
		/*
		case 19: // Turn on sample mode
			if (value > 0){

				// Include ext audio DC offset for sample playback
				writeFilter(8,1,0); 
			}
			else {
				writeFilter(8,0,0);	
			}
		break;
		*/

		case 20: // Porta Speed
			switch (channel) {
				case 1: 
					mainLeadOneEngine(16,value,0,LOW,0,0);
				break;
				
				case 2: 
					mainLeadTwoEngine(16,value,0,LOW,0,0);
				break;
				
				case 3: 
					mainLeadThreeEngine(16,value,0,LOW,0,0);
				break;
			}
		break;
	}
	ledFlash(2);
}




void OnPitchChange(byte channel, int pitch) {
  
	float bendRangeInCoefficient;
	float bendFactor;
	
	bendRangeInCoefficient = (pow(2,((float)bendRange/12)));
	bendFactor = pow(bendRangeInCoefficient,(((float)pitch/8192.0)-1.0));
  
	switch (channel) {
		case 1: 
			mainLeadOneEngine(4, 0, bendFactor, LOW, 0, 0);
		break;
		
		case 2: 
			mainLeadTwoEngine(4, 0, bendFactor, LOW, 0, 0); 
		break;
		
		case 3: 
			mainLeadThreeEngine(4, 0, bendFactor, LOW, 0, 0);
		break;
		
		case 4: 
			mainSampleEngine(4,0,0,pitch);
		break;
		
		case 5: 
		break;
	}
	ledFlash(2);
}




//////////////////////////////////////////
//  END
//////////////////////////////////////
`tippe oder füge den Code hier ein`

Geh nochmal in das Post mit dem Bleistift unten rechts.
grafik

Dann markiere den gesamten Codeteil.
dann klicke auf den Button <CODE> oben imEditor und dann speichern.
grafik

Vorher ist da nix zu machen.

Mache ich.Danke