BASIC4MCU | 질문게시판 | 아두이노 rc카 리모컨 조종
페이지 정보
작성자 현지zz 작성일2021-06-01 15:46 조회9,764회 댓글1건본문
수동 주행과 자율 주행 나눠서 리모컨을 이용하여 rc카를 움직이려고 합니다 .
리모컨으로 전진 후진 좌회전 우회전은 되는데 자동으로 주행( 라인 따라 가기 )이 되지 않습니다 ㅠㅠ
센서는 작동 하는 것 확인 했습니다
코드
#include <IRremote.h>
int RECV_PIN = A0; // 적외선 수신센서 핀(아날로그 입력 A0)
IRrecv irrecv(RECV_PIN); // 적외선 송수신 통신을 위한 객체
decode_results IR_Signal; // 적외선 수신값 해석을 위한 객체
int RightMotor_E_pin = 5; // 오른쪽 모터의 Enable & PWM
int LeftMotor_E_pin = 6; // 왼쪽 모터의 Enable & PWM
int RightMotor_1_pin = 8; // 오른쪽 모터 제어선 IN1
int RightMotor_2_pin = 9; // 오른쪽 모터 제어선 IN2
int LeftMotor_3_pin = 10; // 왼쪽 모터 제어선 IN3
int LeftMotor_4_pin = 11; // 왼쪽 모터 제어선 IN4
int L_Line = A5;
int C_Line = A4;
int R_Line = A3;
int SL = 1;
int SC = 1;
int SR = 1;
int motor_s = 210;
int R_Motor = 0;
int L_Motor = 0;
int mode = 0;
void setup() {
pinMode(RightMotor_E_pin, OUTPUT); // 출력모드로 설정
pinMode(RightMotor_1_pin, OUTPUT);
pinMode(RightMotor_2_pin, OUTPUT);
pinMode(LeftMotor_3_pin, OUTPUT);
pinMode(LeftMotor_4_pin, OUTPUT);
pinMode(LeftMotor_E_pin, OUTPUT);
Serial.begin(9600); // PC와의 시리얼 통신 9600bps로 설정
Serial.println("Welcome Eduino!");
irrecv.enableIRIn(); // 적외선 통신 수신 시작
}
void loop() {
if(irrecv.decode(&IR_Signal)){ // 적외선(IR) 수신값이 있는지 판단.
Serial.print("Input Signal : ");
Serial.print("HEX[ "); Serial.print(IR_Signal.value, HEX); Serial.print(" ], ");
Serial.print("Int[ "); Serial.print((String)IR_Signal.value); Serial.print(" ] || ");
control_SmartCar((String)IR_Signal.value);
if(mode == 0){
motor_role(R_Motor, L_Motor, motor_s);
}
else if(mode == 1){
Right_role(R_Motor, L_Motor, motor_s);
}
else if(mode == 2){
Left_role(R_Motor, L_Motor, motor_s);
}
else if(mode == 3){
motor_role(R_Motor, L_Motor, 0);
}
irrecv.resume(); // 다음 적외선 값 수신
}
else{
int L = digitalRead(L_Line); //오른쪽
int C = digitalRead(C_Line);
int R = digitalRead(R_Line); //왼쪽
if(L == LOW && C == LOW && R == LOW){ //정지 : 전부 흰색 0 0 0
L = SL; C = SC; R = SR;
}
if(L == LOW && C == HIGH && R == LOW){ //직진 : 중간만 검은색 0 1 0
digitalWrite(RightMotor_1_pin, HIGH);
digitalWrite(RightMotor_2_pin,LOW);
digitalWrite(LeftMotor_3_pin, HIGH);
digitalWrite(LeftMotor_4_pin, LOW);
analogWrite(RightMotor_E_pin, motor_s);
analogWrite(LeftMotor_E_pin, motor_s);
}
else if(L== LOW && C == LOW && R == LOW){ // 3개 다 선 없으면 정지 아니라 속도 줄임
digitalWrite(RightMotor_1_pin, HIGH);
digitalWrite(RightMotor_2_pin, LOW);
digitalWrite(LeftMotor_3_pin,HIGH);
digitalWrite(LeftMotor_4_pin, LOW);
analogWrite(RightMotor_E_pin, 190);
analogWrite(LeftMotor_E_pin, 190);
}
else if(L == LOW && R == HIGH){ //우회전 : 오른쪽 검은색 0 0 1. 0 1 1
digitalWrite(RightMotor_1_pin, LOW);
digitalWrite(RightMotor_2_pin, HIGH);
digitalWrite(LeftMotor_3_pin, HIGH);
digitalWrite(LeftMotor_4_pin, LOW);
analogWrite(RightMotor_E_pin, motor_s);
analogWrite(LeftMotor_E_pin, motor_s);
}
else if(L == HIGH && R == LOW){ //좌회전 : 왼쪽 검은색 1 0 0, 1 1 0
digitalWrite(RightMotor_1_pin, HIGH);
digitalWrite(RightMotor_2_pin, LOW);
digitalWrite(LeftMotor_3_pin, LOW);
digitalWrite(LeftMotor_4_pin, HIGH);
analogWrite(RightMotor_E_pin, motor_s);
analogWrite(LeftMotor_E_pin, motor_s);
}
else if(L == HIGH && R == HIGH){ //정지 : 전부 검은색 1 1 1, 1 0 1
digitalWrite(RightMotor_1_pin, LOW);
digitalWrite(RightMotor_2_pin, LOW);
digitalWrite(LeftMotor_3_pin, LOW);
digitalWrite(LeftMotor_4_pin, LOW);
analogWrite(RightMotor_E_pin, 0);
analogWrite(LeftMotor_E_pin, 0);
}
SL = L; SC = C; SR = R;
}
}
void control_SmartCar(String Remote_Val){
if( Remote_Val == "16754775" ){ // "+" 버튼, 명령 : 속도 증가
motor_s = motor_s + 20;
motor_s = min(motor_s, 255);
Serial.print("Speed Up : ");
}
else if( Remote_Val == "16769055" ){ // "-" 버튼, 명령 : 속도 감소
motor_s = motor_s - 20;
motor_s = max(motor_s, 50);
Serial.print("Speed Down : ");
}
else if(Remote_Val == "16718055" ){ // "2" 버튼, 명령 : 전진
R_Motor = HIGH; L_Motor = HIGH; mode = 0;
Serial.print("Forward : ");
}
else if( Remote_Val == "16716015" ){ // "4" 버튼, 명령 : 좌회전
mode = 2;
Serial.print("Turn Left : ");
}
else if( Remote_Val == "16734885" ){ // "6" 버튼, 명령 : 우회전
mode = 1;
Serial.print("Turn Right : ");
}
else if( Remote_Val == "16730805" ){ // "8" 버튼, 명령 : 후진
R_Motor = LOW; L_Motor = LOW; mode = 0;
Serial.print("Backward : ");
}
else if( Remote_Val == "16726215" ){ // "5" 버튼, 명령 : 정지
mode = 3;
Serial.print("Stop : ");
}
else if( Remote_Val == "16732845"){ // "9"버튼, 명령: 라인트레이서
mode = 4;
}
else{
Serial.print("Not Defined : "); // 지정하지 않은 주소입력.
}
Serial.print("R_Motor[ ");Serial.print(R_Motor);Serial.print(" ], ");
Serial.print("L_Motor[ ");Serial.print(L_Motor);Serial.print(" ], ");
Serial.print("motor_s[ ");Serial.print(motor_s);Serial.print(" ], ");
Serial.print("Mode[ ");Serial.print(mode);Serial.println(" ]");
}
void motor_role(int R_motor, int L_motor, int Speed){ //직진,후진
digitalWrite(RightMotor_1_pin, R_motor);
digitalWrite(RightMotor_2_pin, !R_motor);
digitalWrite(LeftMotor_3_pin, L_motor);
digitalWrite(LeftMotor_4_pin, !L_motor);
analogWrite(RightMotor_E_pin, Speed); // 우측 모터 속도값
analogWrite(LeftMotor_E_pin, Speed); // 좌측 모터 속도값
}
void Right_role(int R_motor, int L_motor, int Speed){ //오른쪽
digitalWrite(RightMotor_1_pin, HIGH);
digitalWrite(RightMotor_2_pin, LOW);
digitalWrite(LeftMotor_3_pin, LOW);
digitalWrite(LeftMotor_4_pin, LOW);
analogWrite(RightMotor_E_pin, Speed); // 우측 모터 속도값
analogWrite(LeftMotor_E_pin, Speed); // 좌측 모터 속도값
}
void Left_role(int R_motor, int L_motor, int Speed){ //왼쪽
digitalWrite(RightMotor_1_pin, LOW);
digitalWrite(RightMotor_2_pin, LOW);
digitalWrite(LeftMotor_3_pin, HIGH);
digitalWrite(LeftMotor_4_pin, LOW);
analogWrite(RightMotor_E_pin, Speed); // 우측 모터 속도값
analogWrite(LeftMotor_E_pin, Speed); // 좌측 모터 속도값
}
댓글 1
조회수 9,764master님의 댓글
master 작성일
자동주행만 코드를 작성해서 동작시킨 후
리모콘 수동주행을 추가해보세요