/*
*Pat's IR Robot- Keep finger on button for movement, off to stop.
*19/9/2018
 */

#include <IRremote.h>

// connect motor controller pins to Arduino digital pins
// motor one`
const int in1 = 7;
const int in2 = 8;
// motor two
const int in3 = 9;
const int in4 = 10;
// LEDs
const int ledBlue = 4;
const int ledWhite = 5;
const int delayTime=1000;

const int RECV_PIN = A4;
//int delayTime=1000
IRrecv irrecv(RECV_PIN);

void setup()
{
  // set all the motor control pins to outputs
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);
  pinMode(ledBlue, OUTPUT);
  pinMode(ledWhite, OUTPUT);

  irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
  decode_results results;

  if (irrecv.decode(&results)) // if there is an IR reading
  {
    Serial.println(results.value, HEX);

    switch (results.value)
    {
      case 0x2F0:
       Serial.println("Forwards!");
       forwards();
        break;
      case 0xAF0:
        Serial.println("Backwards!");
        backwards();
        break;
      case 0x2D0:
        Serial.println("Left!");
        left();
        break;
      case 0xCD0:
        Serial.println("Right!");
       right();
        break;
      case 0xA70:
        Serial.println("Stop!");
        halt();
        break;
      case 0x290:
        Serial.println("Crazy!");
        Crazy();
        break;
    }
    
    irrecv.resume(); // Receive the next value
  }
} 
 
void forwards()
{
  // turn on motor A
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(ledBlue, HIGH);
// turn on motor B
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  digitalWrite(ledBlue, HIGH);
  delay(delayTime);
   // stop both motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); 
}

void backwards()
{
// turn on motor A
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(ledWhite, HIGH);
   // turn on motor B
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  digitalWrite(ledWhite, HIGH);
  delay(delayTime);
   // stop both motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); 
}

void right()
{

  // turn on motor A
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
   // turn on motor B
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  delay(delayTime);
   // stop both motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); 
}

void left()
{
 // turn on motor A
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  // turn on motor B
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
   delay(delayTime);
    // stop both motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); 
}
 
void halt()
{
  // stop both motors
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);  
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW); 
}
//{
 // forwards();
 // backwards();
 // right();
  //left();
//} 
 
void Crazy()
{
  // turn on motor A
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(ledBlue, HIGH);
// turn on motor B
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
  digitalWrite(ledBlue, HIGH);
  delay(delayTime);
}

//void backwards()
//{
// turn on motor A
  digitalWrite(in1, LOW);
  digitalWrite(in2, HIGH);
  digitalWrite(ledWhite, HIGH);
   // turn on motor B
  digitalWrite(in3, LOW);
  digitalWrite(in4, HIGH);
  digitalWrite(ledWhite, HIGH);
  delay(delayTime);
}

//void right()
//{

  // turn on motor A
  digitalWrite(in1, HIGH);
  digitalWrite(in2, LOW);
   // turn on motor B
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  delay(delayTime);
}

//void left()
//{
 // turn on motor A
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  // turn on motor B
  digitalWrite(in3, HIGH);
  digitalWrite(in4, LOW);
   delay(delayTime);
}