
/*************************************
 *     TIME:2020.10.29
 *     Development Team: Zhiyi Technology Co., Ltd.
 *     INFRARED REMOTE CONTROL
 * 
 **************************************/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_LEDBackpack.h"

Adafruit_8x16matrix matrix = Adafruit_8x16matrix();

#define FORWARD 1
#define BACKWARD 2
#define TURNLEFT 3
#define TURNRIGHT 4
#define STOP 5
#define Buzzer 6

#define MOTOAR1 6    //
#define MOTOAR2 7

#define MOTOBL1 9    //
#define MOTOBL2 8

int sound = 13;//Blinks

static const uint8_t PROGMEM
smile_bmp[] =
{ B00111100,
  B01000010,
  B10100101,
  B10000001,
  B10100101,
  B10011001,
  B01000010,
  B00111100
},

frown_bmp[] =
{ B00111100,
  B01000010,
  B10100101,
  B10000001,
  B10011001,
  B10100101,
  B01000010,
  B00111100
};
//LED indicator definition
#define LEFT_LED 17   //left LED
#define RIGHT_LED 10  //right LED

void forward()  //Forward function
{ 
  digitalWrite(MOTOAR1,LOW); 
  digitalWrite(MOTOAR2,HIGH);   //7 port high level right wheel forward
  
  digitalWrite(MOTOBL1,LOW);  
  digitalWrite(MOTOBL2,HIGH); //8 port high level left wheel forward
  digitalWrite(LEFT_LED, LOW);
  digitalWrite(RIGHT_LED, LOW);
  Serial.println("Forward");//Sends a message to the serial monitor
  matrix.clear();
  matrix.drawBitmap(0, 0, smile_bmp, 8, 8, LED_ON);//The left dot matrix shows a smile
  matrix.drawBitmap(0, 8, smile_bmp, 8, 8, LED_ON);//The dot bar on the right shows a smile
  matrix.writeDisplay();
  delay(100);
 }
 
 
void back()  //Backward function
{                      
  digitalWrite(MOTOAR1,HIGH);  //6 port high level right wheel back
  digitalWrite(MOTOAR2,LOW); 
  
  digitalWrite(MOTOBL1,HIGH); //9 port high level left wheel back
  digitalWrite(MOTOBL2,LOW); 
  digitalWrite(LEFT_LED, HIGH);
  digitalWrite(RIGHT_LED, HIGH);
  Serial.println("Back");
  matrix.clear();
  matrix.drawBitmap(0, 0, frown_bmp, 8, 8, LED_ON); //The dot matrix on the left shows frowning
  matrix.drawBitmap(0, 8, frown_bmp, 8, 8, LED_ON); //The dot matrix on the right shows frowning
  matrix.writeDisplay();
  delay(100);
}

void left()  //The left function
{
  digitalWrite(MOTOAR1,LOW);  
  digitalWrite(MOTOAR2,HIGH); 
  digitalWrite(MOTOBL1,HIGH); 
  digitalWrite(MOTOBL2,LOW); 
  
  digitalWrite(LEFT_LED, HIGH);
  digitalWrite(RIGHT_LED, LOW);

  Serial.println("Left");
}

void right() //Turn right function
{
  digitalWrite(MOTOAR1,HIGH); 
  digitalWrite(MOTOAR2,LOW);  
  
  digitalWrite(MOTOBL1,LOW); 
  digitalWrite(MOTOBL2,HIGH);  

  digitalWrite(LEFT_LED, LOW);
  digitalWrite(RIGHT_LED, HIGH);

  Serial.println("Right");
}

 void stop()//Stop function
 {
   
  digitalWrite(MOTOAR1,LOW); 
  digitalWrite(MOTOAR2,LOW);  
  
  digitalWrite(MOTOBL1,LOW); 
  digitalWrite(MOTOBL2,LOW);  

  digitalWrite(LEFT_LED, LOW);
  digitalWrite(RIGHT_LED, LOW);
  Serial.println("Stop");

 }

/************BlinkSOUND****/
void Blinks()
{
  for(int i=0;i<5;i++)
  { 
    digitalWrite(sound,HIGH);
    digitalWrite(sound,LOW);
    Serial.println("Blink!");
  }
  delay(1000);
}
//////////////////////////////////
void setup() {
  pinMode(MOTOAR1,OUTPUT);//before useing io pin, pin mode must be set first 
  pinMode(MOTOAR2,OUTPUT);
  pinMode(MOTOBL1,OUTPUT);
  pinMode(MOTOBL2,OUTPUT);
  pinMode(LEFT_LED ,OUTPUT);
  pinMode(RIGHT_LED ,OUTPUT);
  //pinMode(2,OUTPUT);
  pinMode(sound,OUTPUT);
  matrix.begin(0x70);  // pass in the address
  Serial.begin(9600); 
  stop();
  //Serial.flush();//clear cache
}

void loop() {
  Run();
}

/*
* Function name: HC05()
* Function: Receive Bluetooth data, control the car movement direction
* Entry parameters: None
* Return value: None
*/
void Run()
{
    if(Serial.available())      //Determine if the received data is greater than 0
  {
    char serialData = Serial.read(); //Receiving function
    Serial.print("serialData=");
    Serial.println(serialData);
    switch(serialData)
    {
      case 'S':stop();   break;
      case 'D':Blinks(); break;
      case 'B':back();   break;
      case 'F':forward();break;
      case 'R':right();  break;
      case 'L':left();   break;
      default:           break;
    }
  }
}

