
In this project we will build a remotely controlled car (by infrared) controllable by the Arduino board.
The user will be able to drive the car by a remote control in all four directions (forward, backward, right and left) and stop it.
Here is the program for the Arduino board connected to the car.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <IRremote.h> int RECV_PIN = 4; IRrecv irrecv(RECV_PIN); decode_results results; int GA=11,GB=10,DA=5,DB=6; //pin initialization (GA & GB for left motor/ DA & DB for right motor) void setup() { irrecv.enableIRIn(); //Initializes the infrared receiver pinMode(DA,OUTPUT); pinMode(DB,OUTPUT); pinMode(GA,OUTPUT); pinMode(GB,OUTPUT); } /*******************************/ /***Function***/ void ar() //backward direction { digitalWrite(DA,HIGH); digitalWrite(DB,LOW); digitalWrite(GA,HIGH); digitalWrite(GB,LOW); } void av() //forward direction { digitalWrite(DA,LOW); digitalWrite(DB,HIGH); digitalWrite(GA,LOW); digitalWrite(GB,HIGH); } void d()//right direction { digitalWrite(DA,LOW); digitalWrite(DB,HIGH); digitalWrite(GA,HIGH); digitalWrite(GB,LOW); } void g()//left direction { digitalWrite(DA,HIGH); digitalWrite(DB,LOW); digitalWrite(GA,LOW); digitalWrite(GB,HIGH); } void s()//stop car { digitalWrite(DA,LOW); digitalWrite(DB,LOW); digitalWrite(GA,LOW); digitalWrite(GB,LOW); } /*****************************/ void loop() {if (irrecv.decode(&results)) { if (results.value==0xFF18E7)//press the 2 key av(); // the car is moving forward if (results.value==0xFF5AA5)//press the 6 key d(); // the car is moving right if (results.value==0xFF10EF)//press the 4 key g(); // the car is moving left if (results.value==0xFF4AB5)//press the 8 key ar(); // the car is moving backward if (results.value==0xFF38C7)//press the 5 key s();// car stops irrecv.resume(); //Get the following value } } |