CODE 6.9 | 적외선 리모컨으로 자동차 조종하기
| 01 | #include <IRremote.h> // 적외선 리모컨 라이브러리 포함 |
| 02 | |
| 03 | // 모터 제어 핀 설정 |
| 04 | int rightMotor1 = 5; |
| 05 | int rightMotor2 = 6; |
| 06 | int leftMotor1 = 9; |
| 07 | int leftMotor2 = 10; |
| 08 | |
| 09 | // 적외선 수신 센서 핀 설정 |
| 10 | int irPin = 2; |
| 11 | |
| 12 | // 리모컨 버튼별 신호 값 |
| 13 | // 가상의 리모컨 값 예시이며, 내 리모컨에 맞게 수정 필요 |
| 14 | int btnForward = 0x18; // 전진 버튼 |
| 15 | int btnBackward = 0x52; // 후진 버튼 |
| 16 | int btnLeft = 0x08; // 좌회전 버튼 |
| 17 | int btnRight = 0x5A; // 우회전 버튼 |
| 18 | int btnStop = 0x1C; // 정지 버튼 |
| 19 | |
| 20 | void setup() { |
| 21 | // 모터 핀을 출력 모드로 설정 |
| 22 | pinMode(rightMotor1, OUTPUT); |
| 23 | pinMode(rightMotor2, OUTPUT); |
| 24 | pinMode(leftMotor1, OUTPUT); |
| 25 | pinMode(leftMotor2, OUTPUT); |
| 26 | |
| 27 | // 적외선 수신기 시작 |
| 28 | IrReceiver.begin(irPin, DISABLE_LED_FEEDBACK); |
| 29 | } |
| 30 | |
| 31 | void loop() { |
| 32 | // 적외선 신호가 들어왔는지 확인 |
| 33 | if (IrReceiver.decode()) { |
| 34 | int command = IrReceiver.decodedIRData.command; // 수신된 명령 값 저장 |
| 35 | |
| 36 | // 전진 버튼을 눌렀을 때 |
| 37 | if (command == btnForward) { |
| 38 | digitalWrite(rightMotor1, HIGH); |
| 39 | digitalWrite(rightMotor2, LOW); |
| 40 | digitalWrite(leftMotor1, HIGH); |
| 41 | digitalWrite(leftMotor2, LOW); |
| 42 | } |
| 43 | // 후진 버튼을 눌렀을 때 |
| 44 | else if (command == btnBackward) { |
| 45 | digitalWrite(rightMotor1, LOW); |
| 46 | digitalWrite(rightMotor2, HIGH); |
| 47 | digitalWrite(leftMotor1, LOW); |
| 48 | digitalWrite(leftMotor2, HIGH); |
| 49 | } |
| 50 | // 좌회전 버튼을 눌렀을 때 |
| 51 | else if (command == btnLeft) { |
| 52 | digitalWrite(rightMotor1, HIGH); |
| 53 | digitalWrite(rightMotor2, LOW); |
| 54 | digitalWrite(leftMotor1, LOW); |
| 55 | digitalWrite(leftMotor2, LOW); |
| 56 | } |
| 57 | // 우회전 버튼을 눌렀을 때 |
| 58 | else if (command == btnRight) { |
| 59 | digitalWrite(rightMotor1, LOW); |
| 60 | digitalWrite(rightMotor2, LOW); |
| 61 | digitalWrite(leftMotor1, HIGH); |
| 62 | digitalWrite(leftMotor2, LOW); |
| 63 | } |
| 64 | // 정지 버튼을 눌렀을 때 |
| 65 | else if (command == btnStop) { |
| 66 | digitalWrite(rightMotor1, LOW); |
| 67 | digitalWrite(rightMotor2, LOW); |
| 68 | digitalWrite(leftMotor1, LOW); |
| 69 | digitalWrite(leftMotor2, LOW); |
| 70 | } |
| 71 | |
| 72 | // 다음 신호를 받을 수 있도록 수신기 초기화 |
| 73 | IrReceiver.resume(); |
| 74 | } |
| 75 | } |
댓글 없음:
댓글 쓰기