//Connect the IR Receiver pin to D2 for this demo


#include <IRSendRev.h>

int IR_Receiver=2;
int ledPin = 3;
int buzzerPin = 4;

const int pinI1=8;     // motor 1 pin1
const int pinI2=11;    //motor 1 pin2
const int speedpinA=9; //EA(PWM)to control the motor_1 speed
const int pinI3=12;    //motor 2 pin 1
const int pinI4=13;    //motor 2 pin 2
const int speedpinB=10;//EB(PWM]) to control the motor_2 speed

unsigned char dta[20];

void setup() {
  // put your setup code here, to run once:
    pinMode(pinI1,OUTPUT); //set to output
    pinMode(pinI2,OUTPUT);
    pinMode(speedpinA,OUTPUT);
    pinMode(pinI3,OUTPUT);
    pinMode(pinI4,OUTPUT);
    pinMode(speedpinB,OUTPUT);
    pinMode(ledPin, OUTPUT);   
    pinMode(buzzerPin, OUTPUT);   
    IR.Init(IR_Receiver);
    Serial.begin(9600);
    Serial.println("init over");
}

void loop() {
  // put your main code here, to run repeatedly:
  if(IR.IsDta())
    {
          IR.Recv(dta);
          Serial.println(dta[6]);
          if(dta[6]==1){
            digitalWrite(ledPin,HIGH);
            }
          if(dta[6]==2){
            digitalWrite(buzzerPin,HIGH);
            }
          if(dta[6]==3){
            digitalWrite(ledPin,LOW);
            }
          if(dta[6]==4){
            digitalWrite(buzzerPin,LOW);
            }
          if(dta[6]==5){
            forward();
            }
          if(dta[6]==6){
            clean_Output();
            }
          if(dta[6]==7){
            backward();
            }
          
      }
    delay(50);
}

void forward()
{
  analogWrite(speedpinA,150);
  analogWrite(speedpinB,150);  
  digitalWrite(pinI1,LOW);
  digitalWrite(pinI2,HIGH);
  digitalWrite(pinI3,LOW);
  digitalWrite(pinI4,HIGH);
}

void backward()
{
  analogWrite(speedpinA,150);
  analogWrite(speedpinB,150);  
  digitalWrite(pinI1,HIGH);
  digitalWrite(pinI2,LOW);
  digitalWrite(pinI3,HIGH);
  digitalWrite(pinI4,LOW);
  }
  
void clean_Output()
{
  digitalWrite(speedpinA,LOW);// full PWM 255
  digitalWrite(speedpinB,LOW);
}
 
