
//**************
//IR sent demo v1.0
//Connect the IR send pins to D3 for this demo
//******************************
#include <IRSendRev.h>

int collisionPin=2;
int switchPin = 5;

int xValue = 0;
int yValue = 0;

int switchState = 0;  
int collisionState = 0;
int joyState = 0; 
//unsigned char d[] = {9, 90, 91, 11, 31, 4, 1, 2, 3, 4};
unsigned char LED[] = {15, 70, 70, 20, 60, 5, 1, 0, 0, 0,0};
unsigned char BUZZER[] = {15, 70, 70, 20, 60, 5, 2, 0, 0, 0,0};
unsigned char LED_OFF[] = {15, 70, 70, 20, 60, 5, 3, 0, 0, 0,0};
unsigned char BUZZER_OFF[] = {15, 70, 70, 20, 60, 5, 4, 0, 0, 0,0};
unsigned char forward[] = {15, 70, 70, 20, 60, 5, 5, 0, 0, 0,0};
unsigned char cleanMotor[] = {15, 70, 70, 20, 60, 5, 6, 0, 0, 0,0};
unsigned char backward[] = {15, 70, 70, 20, 60, 5, 7, 0, 0, 0,0};

//Very Important:
//the first parameter(15): the data that needs to be sent;
//the next 2 parameter(70,70): the logic high and low duration of "Start";
//the next 2 parameter(20,60): the logic "short" and "long"duration in the communication
//                             that to say:  if "0", the high duration is 20ms and  low is 20 ms; while logic "1",
//                              the high duration is 20 ms and low is 60 ms;
//the next 2 parameter(5): number of data you will sent;
//the next parameter(1, 0, 0, 0,0): data you will sent;
void setup()
{
    pinMode(collisionPin,INPUT);
    pinMode(switchPin,INPUT);
    Serial.begin(9600);
}

void loop()
{
   joyState = joystick();
   switchState = switchscan();
   collisionState = collisionscan();
   switch(switchState)
        {
         case 0:IR.Send(LED_OFF, 38);break;//sent the data via 38Kz IR  
         case 1:IR.Send(LED, 38);break;//sent the data via 38Kz IR 
        }
     delay(20);
    switch(collisionState)
        {
          case 0: IR.Send(BUZZER, 38);break;
          case 1: IR.Send(BUZZER_OFF, 38);break;
          }
          delay(20);
    switch(joyState)
        {
          case 0: IR.Send(forward, 38);break;
          case 1: IR.Send(cleanMotor, 38);break;
          case 2: IR.Send(backward, 38);break;
          } 
          delay(20);
}


int switchscan()
{
   if(digitalRead(switchPin)==HIGH)
   {
    switchState = 1;    
   }
   else{switchState = 0;}
   return switchState;
}

int collisionscan()
{
  if(digitalRead(collisionPin)==HIGH)
  {
    collisionState = 1;
    }
    else{collisionState = 0;}
    return collisionState;
  
  }

int joystick()
{
   xValue = analogRead(A5);
   yValue = analogRead(A4);
  if(yValue<300){joyState = 0;}
   else if(yValue>700){joyState = 2;}
   else {joyState =1;}
   Serial.println(joyState);
   return joyState;
  }
