사용되는 부품

아두이노 보드 1

버튼 스위치 2

초음파 센서 3

서브모터 1

DC모터 1

모터 드라이버

블루투스 모듈


목표로 한 코드 동작이었습니다.


1. 버튼 1을 누르면 모터를 200RPM으로 작동시킨다

버튼 2를 누르면 모터의 작동을 멈춘다. 

2. 각 초음파 센서에서 물체에 대한 거리를 측정한다.

3. 측정한 정보 중 가장 먼 거리에 있는 물체를 측정한 센서를 기준으로

초음파 1 센서일 경우 - 

    서브 모터의 각도를 45도로 움직인다

    측정된 거리가 100cm 이하일 경우-

        모터의 RPM은 400으로 설정

    측정된 거리가 100cm 이상 200cm 이하일 경우-

        모터의 RPM은 800으로 설정

    측정된 거리가 200cm 이상 400cm 이하일 경우-

        모터의 RPM은 1200으로 설정

초음파 2 센서일 경우 - 

    서브 모터의 각도를 90도로 움직인다

    측정된 거리가 100cm 이하일 경우-

        모터의 RPM은 400으로 설정

    측정된 거리가 100cm 이상 200cm 이하일 경우-

        모터의 RPM은 800으로 설정

    측정된 거리가 200cm 이상 400cm 이하일 경우-

        모터의 RPM은 1200으로 설정

초음파 3 센서일 경우 - 

    서브 모터의 각도를 135도로 움직인다

    측정된 거리가 100cm 이하일 경우-

        모터의 RPM은 400으로 설정

    측정된 거리가 100cm 이상 200cm 이하일 경우-

        모터의 RPM은 800으로 설정

    측정된 거리가 200cm 이상 400cm 이하일 경우-

        모터의 RPM은 1200으로 설정

    

블루투스 모듈에서 연결 신호를 받을 경우,

     받은 신호가 L일 경우

        모터의 RPM을 400으로 설정

     받은 신호가 M일 경우

        모터의 RPM을 800으로 설정

     받은 신호가 H일 경우

        모터의 RPM을 1200으로 설정

        

    받은 신호가 Fix일 경우

        서브 모터의 움직임을 90도로 설정


    받은 신호가 Rot 일 경우

        서브 모터가 0도에서 180도까지 , Fix 신호를 받을 때 까지 반복하여 움직일 것

        서브 모터의 움직임은 초당 36도씩 움직이도록 조절할 것



그리고 제작한 코드입니다.

#include <Servo.h>

#include <NewPing.h>

#include <SoftwareSerial.h>


// 핀 설정

const int button1Pin = 2;

const int button2Pin = 3;

const int trigPin1 = 4;

const int echoPin1 = 5;

const int trigPin2 = 6;

const int echoPin2 = 7;

const int trigPin3 = 8;

const int echoPin3 = 9;

const int motorPin1 = 10;  // 모터 드라이버 IN1

const int motorPin2 = 11;  // 모터 드라이버 IN2

const int enablePin = 12;  // 모터 드라이버 ENA

const int servoPin = 13;

const int bluetoothRx = 14; // 블루투스 모듈 RX

const int bluetoothTx = 15; // 블루투스 모듈 TX


// 서브모터 및 초음파 센서 설정

Servo myservo;

NewPing sonar1(trigPin1, echoPin1, 400);

NewPing sonar2(trigPin2, echoPin2, 400);

NewPing sonar3(trigPin3, echoPin3, 400);

SoftwareSerial bluetooth(bluetoothRx, bluetoothTx);


// 변수 설정

int rpm = 0;

long distance1, distance2, distance3;

char command;

bool rotating = false;


void setup() {

  pinMode(button1Pin, INPUT_PULLUP);

  pinMode(button2Pin, INPUT_PULLUP);

  pinMode(motorPin1, OUTPUT);

  pinMode(motorPin2, OUTPUT);

  pinMode(enablePin, OUTPUT);


  myservo.attach(servoPin);

  bluetooth.begin(9600);


  Serial.begin(9600);  // 디버깅용 시리얼 통신

}


void loop() {

  // 버튼 처리

  if (digitalRead(button1Pin) == LOW) {

    setMotorRPM(200);

  } else if (digitalRead(button2Pin) == LOW) {

    setMotorRPM(0);

  }


  // 초음파 센서 거리 측정

  distance1 = sonar1.ping_cm();

  distance2 = sonar2.ping_cm();

  distance3 = sonar3.ping_cm();

  long maxDistance = max(distance1, max(distance2, distance3));


  // 가장 먼 거리의 센서에 따라 동작 결정

  if (maxDistance == distance1) {

    myservo.write(45);

    adjustMotorRPM(distance1);

  } else if (maxDistance == distance2) {

    myservo.write(90);

    adjustMotorRPM(distance2);

  } else if (maxDistance == distance3) {

    myservo.write(135);

    adjustMotorRPM(distance3);

  }


  // 블루투스 명령 처리

  if (bluetooth.available()) {

    command = bluetooth.read();

    if (command == 'L') {

      setMotorRPM(400);

    } else if (command == 'M') {

      setMotorRPM(800);

    } else if (command == 'H') {

      setMotorRPM(1200);

    } else if (command == "Fix") {

      rotating = false;

      myservo.write(90);

    } else if (command == "Rot") {

      rotating = true;

    }

  }


  // 서브모터 회전

  if (rotating) {

    for (int pos = 0; pos <= 180; pos += 36) {

      myservo.write(pos);

      delay(1000);  // 초당 36도 회전

    }

    for (int pos = 180; pos >= 0; pos -= 36) {

      myservo.write(pos);

      delay(1000);

    }

  }

}


void setMotorRPM(int rpmValue) {

  rpm = rpmValue;

  int motorSpeed = map(rpm, 0, 1200, 0, 255);

  analogWrite(enablePin, motorSpeed);

  if (rpm > 0) {

    digitalWrite(motorPin1, HIGH);

    digitalWrite(motorPin2, LOW);

  } else {

    digitalWrite(motorPin1, LOW);

    digitalWrite(motorPin2, LOW);

  }

}


void adjustMotorRPM(long distance) {

  if (distance <= 100) {

    setMotorRPM(400);

  } else if (distance > 100 && distance <= 200) {

    setMotorRPM(800);

  } else if (distance > 200 && distance <= 400) {

    setMotorRPM(1200);

  }

}


하지만 코드를 업로딩 시킨 후에 모터들이 작동을 안 합니다. 어느 부분이 문제인지 모르겠어서 질문 올려봅니다.