#include <IRremote.h>
int IRpin=12;
IRrecv IR(IRpin);
decode_results cmd;
int ENA=5;
int ENB=6;
int IN1=7;
int IN2=8;
int IN3=9;
int IN4=11;
float d=1;
int degRot=90;
int left;
int right;
float v=1.2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
IR.enableIRIn();
IR.blink13(true);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
}
void loop() {
int wv;
//v=1.2;
//d=1;
wv=(v-.35)/.0075;
left=wv;
right=wv;
setSpeed(left,right);
while (IR.decode(&cmd)==0){
}
Serial.println(cmd.value,HEX);
if (cmd.value==0xFF629D){
Serial.println("Forward");
forward(d,v);
}
if (cmd.value==0xFFA857){
Serial.println("Backward");
backward(d,v);
}
if (cmd.value==0xFFC23D){
Serial.println("Right");
turnRight(degRot,wv);
}
if (cmd.value==0xFF22DD){
Serial.println("Left");
turnLeft(degRot,wv);
}
//If Command Was Pound, Set Distance
if (cmd.value==0xFF52AD){
Serial.println("Set distance");
delay(500);
IR.resume();
while (IR.decode(&cmd)==0){
}
delay(100);
if (cmd.value==0xFF4AB5){
Serial.println("Distance=0");
d=0;
}
if (cmd.value==0xFF6897){
Serial.println("Distance=1");
d=1;
}
if (cmd.value==0xFF9867){
Serial.println("Distance=2");
d=2;
}
if (cmd.value==0xFFB04F){
Serial.println("Distance=3");
d=3;
}
if (cmd.value==0xFF30CF){
Serial.println("Distance=4");
d=4;
}
if (cmd.value==0xFF18E7){
Serial.println("Distance=5");
d=5;
}
if (cmd.value==0xFF7A85){
Serial.println("Distance=6");
d=6;
}
if (cmd.value==0xFF10EF){
Serial.println("Distance=7");
d=7;
}
if (cmd.value==0xFF38C7){
Serial.println("Distance=8");
d=8;
}
if (cmd.value==0xFF5AA5){
Serial.println("Distance=9");
d=9;
}
}
//If Command Was star, Set Speed
if (cmd.value==0xFF42BD){
Serial.println("Set speed");
delay(500);
IR.resume();
while (IR.decode(&cmd)==0){
}
delay(100);
Serial.println(cmd.value,HEX);
}
IR.resume();
//calR(wv);
//forward(8,v);
//v=1.5;
//wv=(v-.35)/.0075;
//left=wv;
//right=wv;
//setSpeed(left,right);
//backward(8,v);
}
void setSpeed(int leftVal,int rightVal){
analogWrite(ENA,leftVal);
analogWrite(ENB,rightVal);
}
void forward(float d, float v){
float t;
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
t=d/v*1000;
delay(t);
stopCar();
}
void backward(float d, float v){
float t;
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
t=d/v*1000;
delay(t);
stopCar();
}
void turnRight(int deg, int wv){
float t;
stopCar();
delay(100);
analogWrite(ENA,125);
analogWrite(ENB,125);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
t=(deg+6)/136.29*1000.;
delay(t);
stopCar();
analogWrite(ENA,wv);
analogWrite(ENB,wv);
}
void turnLeft(int deg, int wv){
float t;
analogWrite(ENA,125);
analogWrite(ENB,125);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
t=(deg+6)/136.29*1000.;
delay(t);
stopCar();
analogWrite(ENA,wv);
analogWrite(ENB,wv);
}
void stopCar(){
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}
void calF(){
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
delay(5000);
stopCar();
}
void calB(){
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
delay(5000);
stopCar();
}
void calR(int wv){
stopCar();
analogWrite(ENA,125);
analogWrite(ENB,125);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
delay(3000);
analogWrite(ENA,wv);
analogWrite(ENB,wv);
stopCar();
}
void calL(int wv){
analogWrite(ENA,125);
analogWrite(ENB,125);
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
delay(5000);
analogWrite(ENA,wv);
analogWrite(ENB,wv);
stopCar();
}