Components and supplies
4WD Robot Car Chassis Kit
WINGONEER New Style Infrared IR Wireless Remote Control Sensor Module Kits for Arduino
Arduino UNO
SainSmart L298N Dual H Bridge Stepper Motor Driver
Tools and machines
Soldering iron (generic)
Project description
Code
IR Remote control car
csharp
1#include <IRremote.h> 2 3char command; 4int receiver_pin = 4; //Connect the output pin of IR receiver at pin 4 5int vcc = 5; //VCC for IR sensor 6int gnd = 6; //GND for IR sensor 7 8int statusled = 13; 9 10IRrecv irrecv(receiver_pin); 11 12decode_results results; 13 14 15// connect motor controller pins to Arduino digital pins 16// motor A 17int enA = 8; 18int in1 = 12; 19int in2 = 11; 20// motor B 21int enB = 7; 22int in3 = 10; 23int in4 = 9; 24 25 26void setup() 27 28{ 29Serial.begin(9600); 30 31irrecv.enableIRIn(); 32 33pinMode(statusled,OUTPUT); 34 35digitalWrite(statusled,LOW); 36 37// set all the motor control pins to outputs 38 pinMode(enA, OUTPUT); 39 pinMode(enB, OUTPUT); 40 pinMode(in1, OUTPUT); 41 pinMode(in2, OUTPUT); 42 pinMode(in3, OUTPUT); 43 pinMode(in4, OUTPUT); 44 pinMode(vcc, OUTPUT); 45 pinMode(gnd, OUTPUT); 46 47// Initializing vcc pin high 48 digitalWrite(vcc, HIGH); 49 50 51 52} 53 54void loop() { 55 56if (irrecv.decode(&results)) { 57 58digitalWrite(statusled,LOW); 59 60irrecv.resume(); 61 62if (results.value == 0xFF18E7){ // type button 2 forward robot control 63 64 // this function will run the motors in both directions at a fixed speed 65 Serial.println("Button 2"); 66 // turn on motor A 67 digitalWrite(in1, HIGH); 68 digitalWrite(in2, LOW); 69 // set speed to 200 out of possible range 0~255 70 analogWrite(enA, 100); 71 // turn on motor B 72 digitalWrite(in3, HIGH); 73 digitalWrite(in4, LOW); 74 // set speed to 200 out of possible range 0~255 75 analogWrite(enB, 100); 76 77 78 79}else if(results.value == 0xFF10EF){ // type button 4 turn left robot control 80 81 // this function will run motor A in forward directions motor B stop 82 Serial.println("Button 4"); 83 // turn on motor A 84 digitalWrite(in1, HIGH); 85 digitalWrite(in2, LOW); 86 // set speed to 200 out of possible range 0~255 87 analogWrite(enA, 100); 88 // turn on motor B 89 digitalWrite(in3, LOW); 90 digitalWrite(in4, LOW); 91 // set speed to 200 out of possible range 0~255 92 analogWrite(enB, 100); 93 94 95}else if(results.value == 0xFF30CF){ // type button 1 rotate left robot control 96 97 // this function will run motor A in forward directions motor B in backward directions 98 Serial.println("Button Turn Right"); 99 // turn on motor A 100 digitalWrite(in1, HIGH); 101 digitalWrite(in2, LOW); 102 // set speed to 200 out of possible range 0~255 103 analogWrite(enA, 100); 104 // turn on motor B 105 digitalWrite(in3, LOW); 106 digitalWrite(in4, HIGH); 107 // set speed to 200 out of possible range 0~255 108 analogWrite(enB, 100); 109 110 111}else if(results.value == 0xFF5AA5){ // type button 6 turn right robot control 112 113// this function will stop motor A run motor B in forward directions 114 Serial.println("Button Turn Left"); 115 // turn on motor A 116 digitalWrite(in1, LOW); 117 digitalWrite(in2, LOW); 118 // set speed to 200 out of possible range 0~255 119 analogWrite(enA, 100); 120 // turn on motor B 121 digitalWrite(in3, HIGH); 122 digitalWrite(in4, LOW); 123 // set speed to 200 out of possible range 0~255 124 analogWrite(enB, 100); 125 126}else if(results.value == 0xFF7A85){ // type button 3 rotate right robot control 127 128// this function will run motor A in backward directions motor B in forward directions 129 // turn on motor A 130 digitalWrite(in1, LOW); 131 digitalWrite(in2, HIGH); 132 // set speed to 200 out of possible range 0~255 133 analogWrite(enA, 100); 134 // turn on motor B 135 digitalWrite(in3, HIGH); 136 digitalWrite(in4, LOW); 137 // set speed to 200 out of possible range 0~255 138 analogWrite(enB, 100); 139 140} else if(results.value == 0xFF4AB5){ // type button 8 backward robot control 141 142// this function will run motor A and motor B in backward directions 143 // turn on motor A 144 digitalWrite(in1, LOW); 145 digitalWrite(in2, HIGH); 146 // set speed to 200 out of possible range 0~255 147 analogWrite(enA, 100); 148 // turn on motor B 149 digitalWrite(in3, LOW); 150 digitalWrite(in4, HIGH); 151 // set speed to 200 out of possible range 0~255 152 analogWrite(enB, 100); 153 154 155 156}else if(results.value == 0xFF38C7){ // type button 5 stop robot control 157 158 159// this function will stop both motor A and motor B 160 // turn on motor A 161 digitalWrite(in1, LOW); 162 digitalWrite(in2, LOW); 163 // set speed to 200 out of possible range 0~255 164 analogWrite(enA, 100); 165 // turn on motor B 166 digitalWrite(in3, LOW); 167 digitalWrite(in4, LOW); 168 // set speed to 200 out of possible range 0~255 169 analogWrite(enB, 100); 170 171 172 173} 174 175} 176 177} 178
Downloadable files
Wire Diagram
Wire Diagram
Wire Diagram
Wire Diagram
Comments
Only logged in users can leave comments