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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
/* **********************************************************
* *
* *
* Remote Controlled Robot Car (TV - Infrared Remote (IR)) *
* *
* *
************************************************************/
/* include library */
#include <IRremote.h>
/* define IR sensor pin */
int IRsensorPin = 12;
/* define the some functions used by the library */
IRrecv irrecv(IRsensorPin);
decode_results results;
/* define L298N motor drive control pins */
int RightMotorForward = 2; // IN1
int RightMotorBackward = 4; // IN2
int LeftMotorForward = 5; // IN3
int LeftMotorBackward = 6; // IN4
void setup(){
/* initialize motor control pins as output */
pinMode(LeftMotorForward,OUTPUT);
pinMode(RightMotorForward,OUTPUT);
pinMode(LeftMotorBackward,OUTPUT);
pinMode(RightMotorBackward,OUTPUT);
/* start serial communication to see hex codes */
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop(){
/* if the sensor is receive any signal */
if (irrecv.decode(&results)){
/* print the hex code value on the serial monitor */
Serial.println(results.value);
delay(5);
/* resume function according to hex code */
irrecv.resume();
}
/* if the incoming data is "defined hex code" then run the motors functions */
/* instead of zeros "0000", write the hex codes of your remote control */
if (results.value !=0)
{
if(results.value == 322||results.value == 2370) MotorForward();
if(results.value == 325||results.value == 2373) MotorBackward();
if(results.value == 323||results.value == 2371) TurnRight();
if(results.value == 321||results.value == 2369) TurnLeft();
if(results.value == 320||results.value == 2368) MotorStop();
results.value = 0;
}
}
/* FORWARD */
void MotorForward(){
digitalWrite(LeftMotorForward,HIGH);
digitalWrite(RightMotorForward,HIGH);
digitalWrite(LeftMotorBackward,LOW);
digitalWrite(RightMotorBackward,LOW);
Serial.println("MotorForward");
}
/* BACKWARD */
void MotorBackward(){
digitalWrite(LeftMotorBackward,HIGH);
digitalWrite(RightMotorBackward,HIGH);
digitalWrite(LeftMotorForward,LOW);
digitalWrite(RightMotorForward,LOW);
Serial.println("MotorBackward");
}
/* TURN RIGHT */
void TurnRight(){
digitalWrite(LeftMotorForward,HIGH);
digitalWrite(RightMotorForward,LOW);
digitalWrite(LeftMotorBackward,LOW);
digitalWrite(RightMotorBackward,HIGH);
Serial.println("TurnRight");
}
/* TURN LEFT */
void TurnLeft(){
digitalWrite(RightMotorForward,HIGH);
digitalWrite(LeftMotorForward,LOW);
digitalWrite(LeftMotorBackward,HIGH);
digitalWrite(RightMotorBackward,LOW);
Serial.println("TurnLeft");
}
/* STOP */
void MotorStop(){
digitalWrite(LeftMotorBackward,LOW);
digitalWrite(RightMotorBackward,LOW);
digitalWrite(LeftMotorForward,LOW);
digitalWrite(RightMotorForward,LOW);
Serial.println("MotorStop");
} |
Partager