 #include "Nunchuck.h"
 #include <Wire.h> 
 #include <MozziGuts.h>
+#include <LowPassFilter.h>
+#include <ADSR.h>
 
-#define CONTROL_RATE 64 // powers of 2 please
-
-#define LEFT 0
-#define RIGHT 1
-#define UP 2
+#define CONTROL_RATE 128 // powers of 2 please
 
 int bumpy_input = 0;
 int constrain_input = 0;

int xStick;
int yStick;

unsigned char volumeLeft;
unsigned char volumeRight;
unsigned char volumePitch;

int play = 0;

void setup(){
  uint8_t error;
  
  error = Nunchuck.begin ();
  
  //Serial.begin(19200);
  
  /*if ( error )
  {

  
  // set the frequency
  aSin.setFreq(0);
  aRight.setFreq(0);
  aLeft.setFreq(0);
  
  attack = 50;
  decay = 50;
  sustain = 100;
  release_ms = 500;
  envelope.setADLevels(255, 100);
  envelope.setTimes(attack,decay,sustain,release_ms);

  envelope2.setADLevels(255, 100);
  envelope2.setTimes(attack,decay,sustain,release_ms);

  lpf.setResonance(1);
  
  lpf.setCutoffFreq(128);
}

void updateControl(){
   Nunchuck.update(); 

   if( Nunchuck.getZButton() )
   {
     pressed++;
     if( pressed >= 1 )
     {
       envelope.noteOn();
       envelope2.noteOn();
       pressed = 0;
     }
   }

     constrain_input = constrain(Nunchuck . getRoll(), -180, -1);
     bumpy_input = map(-constrain_input, 0, 180, 11, 0);
     rollLeft = rollLAverage.next(bumpy_input);

     constrain_input = constrain(Nunchuck . getRoll(), 0, 180);
     bumpy_input = map(constrain_input, 0, 180, 11, 0);
     rollRight = rollRAverage.next(bumpy_input);

     constrain_input = constrain(Nunchuck . getPitch(), 18, 174);
     bumpy_input = map(constrain_input, 100, 150, 440, 880);
     pitch = pitchAverage.next(bumpy_input);
     
     constrain_input = constrain(Nunchuck . getRaw()->yStick, 31, 129);
     bumpy_input = map(-constrain_input, -129, -31, 200, 1000);
     yStick = yAverage.next(bumpy_input);     

     aSin.setFreq((int)pitch);
     if (rollRight == 0 )
     {
       aRight.setFreq(0);
     }
     else
     {
       aRight.setFreq((int)((2*pitch) + rollRight*73));
       //aRight.setFreq((int)((2*pitch) - rollRight*73));
     }
     if (rollLeft == 0 )
     {
       aLeft.setFreq(0);
     }
     else{
       aLeft.setFreq( (int)((pitch/2)+ 18*rollLeft));
       //aLeft.setFreq( (int)((pitch/2)- 18*rollLeft));
     }
   envelope.update();
   envelope2.update();  
 
   gain = envelope.next();
   gain2 = envelope2.next();
}


int updateAudio(){
    int synth;
    
    if( rollRight == 0 )
    {
      synth = lpf.next(((long)gain * aSin.next() + (int)gain2 * aLeft.next()) >> 9 );
    }
    else
    {
      synth = lpf.next(((long)gain * aSin.next() + (int)gain2 * aRight.next()) >> 9);
    }
    return synth;
}

void loop(){
