// CANDY-HO Argentina www.candy-ho.com
#include <IRremote.h>
int motorIP = 4;
int motorIN = 6;
int motorDP = 3;
int motorDN = 5;
int enableA = 7;
int enableB = 2;
#define carSpeed 1
#define F 16736925  // FORWARD
#define B 16754775  // BACK
#define L 16720605  // LEFT
#define R 16761405  // RIGHT
#define S 16712445  // STOP
#define UNKNOWN_F 5316027     // FORWARD
#define UNKNOWN_B 2747854299  // BACK
#define UNKNOWN_L 1386468383  // LEFT
#define UNKNOWN_R 553536955   // RIGHT
#define UNKNOWN_S 3622325019  // STOP
#define RECV_PIN 12
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long val;
unsigned long preMillis;


void setup() {
 pinMode(motorIP, OUTPUT);
 pinMode(motorIN, OUTPUT);
 pinMode(motorDP, OUTPUT);
 pinMode(motorDN, OUTPUT);
 Serial.begin(9600);
 irrecv.enableIRIn();
}

void loop() {
  if (irrecv.decode(&results)){ 
    preMillis = millis();
    val = results.value;
    Serial.println(val);
    irrecv.resume();
    switch(val){
      case F: 
      case UNKNOWN_F: forward(); break;
      case B: 
      case UNKNOWN_B: backward(); break;
      case L: 
      case UNKNOWN_L: left(); break;
      case R: 
      case UNKNOWN_R: right();break;
      case S: 
      case UNKNOWN_S: stop(); break;
      default: break;
    }
  }
  else{
    if(millis() - preMillis > 500){
      stop();
      preMillis = millis();
    }
  }
//  Serial.println("HOLA");
} 

//Defining functions so it's more easy
//motor functions
void motorAforward() {
  digitalWrite (motorIP, HIGH);
  digitalWrite (motorIN, LOW);
}
void motorBforward() {
  digitalWrite (motorDP, HIGH);
  digitalWrite (motorDN, LOW);
}
void motorAbackward() {
  digitalWrite (motorIP, LOW);
  digitalWrite (motorIN, HIGH);
}
void motorBbackward() {
  digitalWrite (motorDP, LOW);
  digitalWrite (motorDN, HIGH);
}
void motorAstop() {
 digitalWrite (motorIP, HIGH);
 digitalWrite (motorIN, HIGH);
}
void motorBstop() {
 digitalWrite (motorDP, HIGH);
 digitalWrite (motorDN, HIGH);
}
void motorAcoast() {
 digitalWrite (motorIP, LOW);
 digitalWrite (motorIN, LOW);
}
void motorBcoast() {
 digitalWrite (motorDP, LOW);
 digitalWrite (motorDN, LOW);
}
void motorAon() {
 digitalWrite (enableA, HIGH);
}
void motorBon() {
 digitalWrite (enableB, HIGH);
}
void motorAoff() {
 digitalWrite (enableA, LOW);
}
void motorBoff() {
 digitalWrite (enableB, LOW);
}
// Movement functions

void forward()
{
motorAforward();
motorBforward();
//delay(DELAY);
Serial.println("foward");
//Serial.println("Moving forward");
}


void backward()
{

//Serial.println("bk");
Serial.println("Moving back");
motorAbackward();
motorBbackward();
//delay(DELAY);
}


void left()
{
Serial.println("lf");
//Serial.println("Turning LEFT");
motorAbackward();
motorBforward();

}


void right()
{
Serial.println("rg");
//Serial.println("Turning Right");
motorAforward();
motorBbackward();
}

void stop()
{
Serial.println("stop");
//Serial.println("Stopping");
digitalWrite(motorIP,LOW);
digitalWrite(motorIN,LOW);
digitalWrite(motorDP,LOW);
digitalWrite(motorDN,LOW);
}


void coast (int duration) {
 motorAcoast();
 motorBcoast();
 delay (duration);
}
void breakRobot (int duration) {
 motorAstop();
 motorBstop();
 delay (duration);
}
void disableMotors() {
 motorAoff();
 motorBoff();
}
void enableMotors() {
 motorAon();
 motorBon();
}


