Basic Remote control car
Basic Remote control car with IRremote
Prototype 01
YouTube: video
Circuit:
The LEDs connected in the circuit indicates that they are connected to 2 wheels of the car respectively with the help of motor driver.
YouTube: Video
Code 1:
(Basic remote control car without password)
#include <IRremote.h>
int RECV_PIN = 2;
IRrecv sensor(RECV_PIN);
decode_results value1;
#define front 1904
#define back 3952
#define right 752
#define left 2800
void forward();
void backward();
void turnleft();
void turnright();
void Stop();
int m1p1 = 6;
int m1p2 = 7;
int m2p1 = 8;
int m2p2 = 9;
int led = 13;
int number = 0, value = 0;
void setup()
{
Serial.begin(9600);
sensor.enableIRIn();
pinMode(led, OUTPUT);
pinMode(m1p1, OUTPUT);
pinMode(m1p2, OUTPUT);
pinMode(m2p1, OUTPUT);
pinMode(m2p2, OUTPUT);
}
void loop()
{
if (sensor.decode(&value1)) {
unsigned int state = value1.value;
if (state == 2704 ) {
digitalWrite(led, HIGH);
value = 1;
}
sensor.resume();
while (value == 1)
{
if (sensor.decode(&value1)) {
unsigned int state1 = value1.value;
Serial.println(state1);
if (state1 == 1904) {
forward();
Serial.println("Go Forward!");
delay(100);
Stop();
Serial.println("STOP!");
}
else if (state1 == 752) {
turnright();
Serial.println("Turn RIGHT");
delay(100);
Stop();
Serial.println("STOP!");
}
else if (state1 == 3952) {
backward();
Serial.println("Reverse!");
delay(100);
Stop();
Serial.println("STOP!");
}
else if (state1 == 2800) {
turnleft();
Serial.println("Turn LEFT");
delay(100);
Stop();
Serial.println("STOP!");
}
else if (state1 == 2704)
{
digitalWrite(led, LOW);
value = 0;
}
sensor.resume();
}
}
}
}
void forward()
{
digitalWrite(m1p1, 1);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 1);
digitalWrite(m2p2, 0);
delay(100);
}
void backward()
{
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 1);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 1);
delay(100);
}
void turnright()
{
digitalWrite(m1p1, 1);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 1);
delay(100);
}
void turnleft()
{
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 1);
digitalWrite(m2p1, 1);
digitalWrite(m2p2, 0);
delay(100);
}
void Stop()
{
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 0);
delay(100);
}
Code 2:
(Basic remote control car with password)
#include <IRremote.h>
int RECV_PIN = 2;
IRrecv sensor(RECV_PIN);
decode_results value1;
decode_results value2;
decode_results value3;
#define front 1904
#define back 3952
#define right 752
#define left 2800
void forward();
void backward();
void turnleft();
void turnright();
void Stop();
int password();
int m1p1 = 6;
int m1p2 = 7;
int m2p1 = 8;
int m2p2 = 9;
int led = 13;
int number = 0, value = 0;
void setup()
{
Serial.begin(9600);
sensor.enableIRIn();
pinMode(led, OUTPUT);
pinMode(m1p1, OUTPUT);
pinMode(m1p2, OUTPUT);
pinMode(m2p1, OUTPUT);
pinMode(m2p2, OUTPUT);
Serial.println("Switch On!");
}
void loop()
{
if (sensor.decode(&value1)) {
unsigned int state = value1.value;
Serial.println(state);
delay(10);
if (state == 2704 ) {
if (number % 2 == 0) {
digitalWrite(led, HIGH);
state = 0;
value = password();
delay(100);
}
else
{
value = NULL;
}
}
number++;
sensor.resume();
}
if (value == 4)
{
Serial.println("Work");
while (value == 4)
{
if (sensor.decode(&value3)) {
unsigned int state1 = value3.value;
Serial.println(state1);
if (state1 == 1904) {
forward();
Serial.println("Go Forward!");
delay(100);
Stop();
Serial.println("STOP!");
}
else if (state1 == 752) {
turnright();
Serial.println("Turn RIGHT");
delay(100);
Stop();
Serial.println("STOP!");
}
else if (state1 == 3952) {
backward();
Serial.println("Reverse!");
delay(100);
Stop();
Serial.println("STOP!");
}
else if (state1 == 2800) {
turnleft();
Serial.println("Turn LEFT");
delay(100);
Stop();
Serial.println("STOP!");
}
else if (state1 == 2704)
{
value = NULL;
}
sensor.resume();
}
}
}
if (value == NULL)
{
digitalWrite(led, 0);
}
delay(500);
}
int password()
{
delay(500);
Serial.println("Enter Password:");
int num = 0, val = 1;
while (val == 1)
{
sensor.resume();
if (sensor.decode(&value2)) {
delay(150);
unsigned int state2 = value2.value;
Serial.print("_");
Serial.print(state2);
Serial.print("_");
switch (state2) {
case 16: /*1*/ num = ++num; break;
case 2064: /*2*/ num = --num; break;
case 1040: /*3*/ num = num * num; break;
case 3088:/*4*/ num = ++num; break;
case 528:/*5*/ num = ++num; break;
case 2576: /*6*/ num = --num; break;
case 1552: /*7*/ num = num * 2; val = 0; break;
case 3600: /*8*/ num = --num; break;
case 272: /*9*/ num = num * 5; break;
case 2320: /*0*/ num = ++num; break;
case 2704: /*PowerButton*/num = NULL; digitalWrite(led, 0); val = 0; break;
default: num = 0; break;
}
sensor.resume();
}
}
Serial.print("\n");
Serial.print("Password:");
Serial.println(num);
return num;
}
void forward()
{
digitalWrite(m1p1, 1);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 1);
digitalWrite(m2p2, 0);
delay(100);
}
void backward()
{
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 1);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 1);
delay(100);
}
void turnright()
{
digitalWrite(m1p1, 1);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 1);
delay(100);
}
void turnleft()
{
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 1);
digitalWrite(m2p1, 1);
digitalWrite(m2p2, 0);
delay(100);
}
void Stop()
{
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 0);
delay(100);
}
Prototype 02
YouTube: Simulation
Circuit:
Code:
Without Password
#include <IRremote.h>
void power();
void forward();
void backward();
void turnleft();
void turnright();
void Stop();
int button = 0;
int m1p1 = 8, m1p2 = 9, m2p1 = 6, m2p2 = 7;
int onOff = 13, state = 0, work = 0;
int motion = 500;//Delay
int mapCodeToButton(unsigned long code) {
if ((code & 0x0000FFFF) == 0x0000BF00) {
code >>= 16;
if (((code >> 8) ^ (code & 0x00FF)) == 0x00FF) {
return code & 0xFF;
}
}
return -1;
}
int readInfrared() {
int result = -1;
if (IrReceiver.decode()) {
unsigned long code = IrReceiver.decodedIRData.decodedRawData;
result = mapCodeToButton(code);
IrReceiver.resume();
}
return result;
}
void setup()
{
IrReceiver.begin(2);
Serial.begin(9600);
pinMode(m1p1, OUTPUT);
pinMode(m1p2, OUTPUT);
pinMode(m2p1, OUTPUT);
pinMode(m2p2, OUTPUT);
pinMode(onOff, OUTPUT);
}
void loop()
{
button = readInfrared();
if (button >= 0) {
Serial.println(button);
switch (button)
{
case 0: power(); break;
case 5: if (work) Stop(); break;
case 1: if (work)forward(); Stop(); break;
case 9: if (work)backward(); Stop(); break;
case 6: if (work)turnright(); Stop(); break;
case 4: if (work)turnleft(); Stop(); break;
}
}
delay(10);
}
void power()
{
if (state % 2 == 0)
{
digitalWrite(onOff, 1);
work = 1;
state++;
loop();
}
else
{
digitalWrite(onOff, 0);
work = 0;
Stop();
state++;
loop();
}
}
void forward()
{
digitalWrite(m1p1, 1);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 1);
digitalWrite(m2p2, 0);
delay(motion);
}
void backward()
{
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 1);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 1);
delay(motion);
}
void turnright()
{
digitalWrite(m1p1, 1);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 1);
delay(motion);
}
void turnleft()
{
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 1);
digitalWrite(m2p1, 1);
digitalWrite(m2p2, 0);
delay(motion);
}
void Stop()
{
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 0);
delay(100);
}
Code:
With Password
#include <IRremote.h>
void power();
int password();
void forward();
void backward();
void turnleft();
void turnright();
void Stop();
int button = 0;
int m1p1 = 8, m1p2 = 9, m2p1 = 6, m2p2 = 7;
int onOff = 13, state = 0, work = 0, pin = 0;
int motion = 500;//Delay
int mapCodeToButton(unsigned long code) {
if ((code & 0x0000FFFF) == 0x0000BF00) {
code >>= 16;
if (((code >> 8) ^ (code & 0x00FF)) == 0x00FF) {
return code & 0xFF;
}
}
return -1;
}
int readInfrared() {
int result = -1;
if (IrReceiver.decode()) {
unsigned long code = IrReceiver.decodedIRData.decodedRawData;
result = mapCodeToButton(code);
IrReceiver.resume();
}
return result;
}
void setup()
{
IrReceiver.begin(2);
Serial.begin(9600);
pinMode(m1p1, OUTPUT);
pinMode(m1p2, OUTPUT);
pinMode(m2p1, OUTPUT);
pinMode(m2p2, OUTPUT);
pinMode(onOff, OUTPUT);
Serial.println("Switch On to start");
state = 0, work = 0, pin = 0;
}
void loop()
{
button = readInfrared();
if (button >= 0) {
Serial.println(button);
switch (button)
{
case 0: delay(10); power(); Stop(); break;
case 5: if (work) Stop(); break;
case 1: if (work)forward(); Stop(); break;
case 9: if (work)backward(); Stop(); break;
case 6: if (work)turnright(); Stop(); break;
case 4: if (work)turnleft(); Stop(); break;
default: break;
}
}
delay(10);
}
void power()
{
if (state % 2 == 0)
{
digitalWrite(onOff, 1);
state++;
loop:
pin = 0;
pin = password();
if (pin == 4000)
{
Serial.println("Proceed");
work = 1;
delay(10);
}
else if (pin == 117)
{
digitalWrite(onOff, 0);
work = 0;
Stop();
state++;
Serial.println("Switched Off");
Serial.println("Switch On to start");
}
else
{
Serial.println("Failed! Re-Enter");
work = 0;
goto loop;
}
}
else
{
digitalWrite(onOff, 0);
work = 0;
Stop();
state++;
Serial.println("Switched Off");
Serial.println("Switch On to start");
}
}
int password()
{
Serial.println("Enter Pin");
int rpass = 0, enter = 1;
while (enter)
{
button = readInfrared();
if (button >= 0) {
Serial.println(button);
switch (button)
{
case 12 : rpass = 10; break; //0
case 16: rpass += 200; break; //1
case 17 : rpass = rpass * rpass; break;//2
case 18: rpass = rpass * rpass + 1; break;//3
case 20 : rpass += 432; break;//4
case 21: rpass += 5; break;//5
case 22: rpass = rpass + 100; break;//6
case 24: rpass = rpass * 10; break;//7
case 25: rpass += 20; break;//8
case 26: rpass += 15; break;//9
case 0: return 117; break;
case 14: Serial.println("Pin cleared"); rpass = 0; break;
case 13: enter = 0; break;
default: break;
}
// Serial.println(rpass);
}
delay(10);
}
return rpass;
}
void forward()
{
Serial.println("Forward Motion");
digitalWrite(m1p1, 1);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 1);
digitalWrite(m2p2, 0);
delay(motion);
}
void backward()
{
Serial.println("Backward Motion");
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 1);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 1);
delay(motion);
}
void turnright()
{
Serial.println("Turn Right");
digitalWrite(m1p1, 1);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 1);
delay(motion);
}
void turnleft()
{
Serial.println("Turn Left");
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 1);
digitalWrite(m2p1, 1);
digitalWrite(m2p2, 0);
delay(motion);
}
void Stop()
{
digitalWrite(m1p1, 0);
digitalWrite(m1p2, 0);
digitalWrite(m2p1, 0);
digitalWrite(m2p2, 0);
delay(100);
}
Comments
Post a Comment