// Team 108 to program the Glove in C, we updated the open source nunchunk library, please see our update listed below
//#include <Wire.h>
/*
 * NunchuckPrint
 *
 * 2007 Tod E. Kurt, http://todbot.com/blog/
 *
 * The Wii Nunchuck reading code is taken from Windmeadow Labs
 *   http://www.windmeadow.com/node/42
*/ 
#include <Wire.h>
// variables declaration for our addition
int ledpin = 13;  // Vibrator
int ledpin1 = 12;  // LED
char myChar = 9;
int fsrPin = A0;     // the FSR and 10K pulldown are connected to a0
int fsrPin1 = A1;    // new game
int fsrReading;
int fsrReading1;
int cnt = 1;
int fsrsave;     // the analog reading from the FSR resistor divider
int myvalue[30];
//int myChar ;
void setup()
{
  Serial.begin(9600);
//  pinMode(fsrPin,INPUT);
  pinMode(ledpin, OUTPUT);
  pinMode(ledpin1, OUTPUT);
  nunchuck_setpowerpins(); // use analog pins 2&3 as fake gnd & pwr
  nunchuck_init(); // send the initilization handshake
 // Serial.print ("Finished setup\n");
}
void loop()
{
 // we start updating the code to program the two force sensors and check the data read by the nunchuck_get_data() to decide the direction based on the values
    fsrReading=digitalRead(fsrPin);
    fsrReading1=digitalRead(fsrPin1);
 delay(100);
char acc='s',joy='3';
 nunchuck_get_data();
 delay(50);
 // Check the serial input from the windows application
   if (Serial.read()=='2'){
        digitalWrite(ledpin1, HIGH);
      delay(5);
     digitalWrite(ledpin1, LOW);
     delay(5);
   }  
  
  else if(Serial.read()=='1'){
        digitalWrite(ledpin, HIGH);  // turn the ledPin on
       delay(100);
       digitalWrite(ledpin, LOW);

       
   }
if((nunchuck_accelx()>80 && nunchuck_accelx()<180) && (nunchuck_accely()>80 && nunchuck_accely()<159))
  
 {
     acc='s';//Serial.write('s');
 }
 //FORWORD    
    else if((nunchuck_accelx()>80 && nunchuck_accelx()<180) && (nunchuck_accely()>160)){
 acc='f';//Serial.write ('f');
   
  } 
   
  //RIGHT
  else if((nunchuck_accelx()>160  ) && (nunchuck_accely()>80 &&nunchuck_accely()<180 ))
 {  
  
    acc='r';//Serial.write('r');
 
  }
  
        else if ((nunchuck_accelx()<80) && (nunchuck_accely()>80 && nunchuck_accely()<180))
    {
      acc='l';//Serial.write('l');
  
      }
       
    //BACKWORD
     else if((nunchuck_accelx()>80 && nunchuck_accelx()<180) && (nunchuck_accely()<90)){
     acc='b';//Serial.write('b');
    }  
  if (nunchuck_zbutton()==1 || fsrReading > 0)
     { joy='2';//Serial.write('2');
  acc='2';  
  }
  
     //LEFT
    else if ((nunchuck_joyx()<80) && (nunchuck_joyy()>80 && nunchuck_joyy()<180))
    {
           joy='0';//Serial.write('0');
  }
  
  // Buttom-left
 else if((nunchuck_joyx()<80) && (nunchuck_joyy()<80 ) )
 {
     joy='9';//Serial.write('9');
 }
    
    
    //BACKWORD
    else if((nunchuck_joyx()>80 && nunchuck_joyx()<180) && (nunchuck_joyy()<80)){
 
     joy='7';//Serial.write('7');
    }
    
}

Serial.flush();
Serial.write(',');
Serial.write(joy);
Serial.write(',');
Serial.write(acc);
}
    }
  
// End our update to the code

//
// Nunchuck functions
//

static uint8_t nunchuck_buf[6];   // array to store nunchuck data,

// Uses port C (analog in) pins as power & ground for Nunchuck
static void nunchuck_setpowerpins()
{
#define pwrpin PORTC3
#define gndpin PORTC2
    DDRC |= _BV(pwrpin) | _BV(gndpin);
    PORTC &=~ _BV(gndpin);
    PORTC |=  _BV(pwrpin);
    delay(100);  // wait for things to stabilize    
  
}

// initialize the I2C system, join the I2C bus,
// and tell the nunchuck we're talking to it
void nunchuck_init()
{ 
  Wire.begin();	                // join i2c bus as master
  Wire.beginTransmission(0x52);	// transmit to device 0x52
  Wire.write(0x40);		// sends memory address
  Wire.write(0x00);		// sends sent a zero.  
  Wire.endTransmission();	// stop transmitting
}

// Send a request for data to the nunchuck
// was "send_zero()"
void nunchuck_send_request()
{
  Wire.beginTransmission(0x52);	// transmit to device 0x52
  Wire.write(0x00);		// sends one byte
  Wire.endTransmission();	// stop transmitting
}

// Receive data back from the nunchuck, 
int nunchuck_get_data()
{
    int cnt=0;
    Wire.requestFrom (0x52, 6);	// request data from nunchuck
    while (Wire.available ()) {
      // receive byte as an integer
      nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.read());
      cnt++;
    }
    nunchuck_send_request();  // send request for next data payload
    // If we recieved the 6 bytes, then go print them
    if (cnt >= 5) {
     return 1;   // success
    }
    return 0; //failure
}

// Print the input data we have recieved
// accel data is 10 bits long
// so we read 8 bits, then we have to add
// on the last 2 bits.  That is why I
// multiply them by 2 * 2
void nunchuck_print_data()
{ 
  static int i=0;
  int joy_x_axis = nunchuck_buf[0];
  int joy_y_axis = nunchuck_buf[1];
  int accel_x_axis = nunchuck_buf[2]; // * 2 * 2; 
  int accel_y_axis = nunchuck_buf[3]; // * 2 * 2;
  int accel_z_axis = nunchuck_buf[4]; // * 2 * 2;

  int z_button = 0;
  int c_button = 0;

  // byte nunchuck_buf[5] contains bits for z and c buttons
  // it also contains the least significant bits for the accelerometer data
  // so we have to check each bit of byte outbuf[5]
  if ((nunchuck_buf[5] >> 0) & 1) 
    z_button = 1;
  if ((nunchuck_buf[5] >> 1) & 1)
    c_button = 1;

  if ((nunchuck_buf[5] >> 2) & 1) 
    accel_x_axis += 2;
  if ((nunchuck_buf[5] >> 3) & 1)
    accel_x_axis += 1;

  if ((nunchuck_buf[5] >> 4) & 1)
    accel_y_axis += 2;
  if ((nunchuck_buf[5] >> 5) & 1)
    accel_y_axis += 1;

  if ((nunchuck_buf[5] >> 6) & 1)
    accel_z_axis += 2;
  if ((nunchuck_buf[5] >> 7) & 1)
    accel_z_axis += 1;

  Serial.print(i,DEC);
  Serial.print("\t");
  
  Serial.print("joy:");
  Serial.print(joy_x_axis,DEC);
  Serial.print(",");
  Serial.print(joy_y_axis, DEC);
  Serial.print("  \t");

  Serial.print("acc:");
  Serial.print(accel_x_axis, DEC);
  Serial.print(",");
  Serial.print(accel_y_axis, DEC);
  Serial.print(",");
  Serial.print(accel_z_axis, DEC);
  Serial.print("\t");

  Serial.print("but:");
  Serial.print(z_button, DEC);
  Serial.print(",");
  Serial.print(c_button, DEC);

  Serial.print("\r\n");  // newline
  i++;

}

// Encode data to format that most wiimote drivers except
// only needed if you use one of the regular wiimote drivers
char nunchuk_decode_byte (char x)
{
  x = (x ^ 0x17) + 0x17;
  return x;
}
// returns zbutton state: 1=pressed, 0=notpressed
int nunchuck_zbutton()
{
   return ((nunchuck_buf[5] >> 0) & 1) ? 0 : 1;  // voodoo
}
// returns zbutton state: 1=pressed, 0=notpressed
int nunchuck_cbutton()
{
   return ((nunchuck_buf[5] >> 1) & 1) ? 0 : 1;  // voodoo
}

// returns value of x-axis joystick
int nunchuck_joyx()
{
  return nunchuck_buf[0]; 
}

// returns value of y-axis joystick
int nunchuck_joyy()
{
  return nunchuck_buf[1];
}

// returns value of x-axis accelerometer
int nunchuck_accelx()
{
  return nunchuck_buf[2];   // FIXME: this leaves out 2-bits of the data
}

// returns value of y-axis accelerometer
int nunchuck_accely()
{
  return nunchuck_buf[3];   // FIXME: this leaves out 2-bits of the data
}

// returns value of z-axis accelerometer
int nunchuck_accelz()
{
  return nunchuck_buf[4];   // FIXME: this leaves out 2-bits of the data
}

