OS/Arduino
Arduino Uno - IR Remote
파란크리스마스
2017. 12. 30. 10:58
728x90
출처 : IR Remote Control Car | How To Make Remote Control Car Using Arduino - MaxPhi
z3t0/Arduino-IRremote: Infrared remote library for Arduino: send and receive infrared signals with multiple protocols
IR Remote 라이브러리
Arduion IDE가 설치된 폴더의 libraries 폴더에서 소스 다운로드
C:\arduino-1.6.5-r5\libraries>git clone https://github.com/z3t0/Arduino-IRremote.git
Cloning into 'Arduino-IRremote'...
remote: Counting objects: 1664, done.
remote: Total 1664 (delta 0), reused 0 (delta 0), pack-reused 1664 eceiving objects: 100% (1664/1664), 972.01 KiB | 419.Receiving objects: 100% (1664/1664), 1.08 MiB | 445.00 KiB/s, done.
Resolving deltas: 100% (916/916), done.
적외선 센서 배선
적외선 센서 소스
/* IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv An IR detector/demodulator must be connected to the input RECV_PIN. Version 0.1 July, 2009 Copyright 2009 Ken Shirriff * http://arcfn.com */ #include <IRremote.h> int RECV_PIN = 12; IRrecv irrecv(RECV_PIN); decode_results results; void setup() { Serial.begin(9600); irrecv.enableIRIn(); // Start the receiver } void loop() { if (irrecv.decode(&results)) { Serial.println(results.value, HEX); irrecv.resume(); // Receive the next value } }
적외선 센서 연동 RC CAR 배선
적외선 센서 연동 RC CAR 소스
#include <IRremote.h> #include <Ultrasonic.h> #include <SoftwareSerial.h> int IR_RECV_PIN = 12; int rc_car_mode = 2; IRrecv irrecv(IR_RECV_PIN); SoftwareSerial btSerial(9, 8); //Connect HC-06. Use your (TX, RX) settings Ultrasonic ultrasonic(10,11); // (Trig PIN,Echo PIN) //모터A 컨트롤 int enA = 6; int IN1 = 7; int IN2 = 5; //모터B 컨트롤 int enB = 3; int IN3 = 4; int IN4 = 2; decode_results ir_results; String receiveStr=""; //받는 문자열 String olfReceiveStr=""; //받는 문자열 #define MOTER_SPEED 255 // MAX 255 void setup() { irrecv.enableIRIn(); // Start the receiver pinMode(enA, OUTPUT); pinMode(enB, OUTPUT); pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(IN3, OUTPUT); pinMode(IN4, OUTPUT); Serial.begin(9600); Serial.println("Hello!"); btSerial.begin(9600); // set the data rate for the BT port } void loop() { analogWrite(enA, MOTER_SPEED); analogWrite(enB, MOTER_SPEED); int value = ultrasonic.Ranging(CM); Serial.print(value); // CM or INC Serial.print(" cm / " ); boolean is_forward = true; if (value <= 15) { is_forward = false; } if (rc_car_mode > 2) { rc_car_mode = 1; } switch (rc_car_mode) { case 1: loop_bluetooth(is_forward); break; case 2: loop_irrecv(is_forward); break; } Serial.print("\n"); } void loop_irrecv(boolean is_forward) { receiveStr = ""; if (irrecv.decode(&ir_results)) { Serial.print(ir_results.value, HEX); switch(ir_results.value) { case 0xFF629D: Serial.print(" / ch"); // ch receiveStr = "C"; break; case 0xFF18E7: Serial.print(" / Forward"); // Button 2 receiveStr = "F"; break; case 0xFF10EF: Serial.print(" / Left"); // Button 4 receiveStr = "L"; break; case 0xFF38C7: Serial.print(" / Stop"); // Button 5 receiveStr = "S"; break; case 0xFF5AA5: Serial.print(" / Right"); // Button 6 receiveStr = "R"; break; case 0xFF4AB5: Serial.print(" / Back"); // Button 8 receiveStr = "B"; break; } irrecv.resume(); // Receive the next value loop_moter_command(is_forward, receiveStr); } } void loop_bluetooth(boolean is_forward) { // put your main code here, to run repeatedly: receiveStr=""; while(btSerial.available()) //mySerial에 전송된 값이 있으면 { char myChar = (char)btSerial.read(); //mySerial int 값을 char 형식으로 변환 receiveStr+=myChar; //수신되는 문자를 receiveStr에 모두 붙임 (1바이트씩 전송되는 것을 연결) delay(5); //수신 문자열 끊김 방지 } loop_moter_command(is_forward, receiveStr); } void loop_moter_command(boolean is_forward, String receiveStr) { if (receiveStr.length() > 0) { Serial.print(receiveStr); if (receiveStr == "C") { rc_car_mode++; } else if (receiveStr == "F") { if (is_forward) { bothMotorStart(); // 전진 } else { stopAllMotor(); } } else if (receiveStr == "S") { stopAllMotor(); } else if (receiveStr == "R") { turnRight(); // 오른쪽으로 턴 } else if (receiveStr == "L") { turnLeft(); // 왼쪽으로 턴 } else if (receiveStr == "B") { bothMotorBack(); // 후진 } olfReceiveStr = receiveStr; } else if (!is_forward && olfReceiveStr == "F") { stopAllMotor(); } } //모터A,B 정회전 void bothMotorStart() { digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); } //모터A,B 역회전 void bothMotorBack() { digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); } //모터A,B Stop void stopAllMotor() { digitalWrite(IN1,LOW); digitalWrite(IN2,LOW); digitalWrite(IN3,LOW); digitalWrite(IN4,LOW); } //모터A 역회전, 모터B 정회전 void turnLeft() { digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); } //모터A 정회전, 모터B 역회전 void turnRight() { digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); } //모터A 정회전, 모터B Stop void motorA_Rotation() { digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,LOW); digitalWrite(IN4,LOW); } //모터A Stop, 모터B 정회전 void motorB_Rotation() { digitalWrite(IN1,LOW); digitalWrite(IN2,LOW); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); } //모터A 역회전, 모터B Stop void motorA_Reverse() { digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,LOW); digitalWrite(IN4,LOW); } //모터A Stop, 모터B 역회전 void motorB_Reverse() { digitalWrite(IN1,LOW); digitalWrite(IN2,LOW); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); }