Lee ye bingledel
Published © GPL3+

RC car

This is my second time of Fab Lab academy.

IntermediateFull instructions provided3 hours1,070
RC car

Things used in this project

Story

Read more

Code

rc car

Arduino
#include <SoftwareSerial.h>

SoftwareSerial BTSerial(11, 12);

#define EN1 3 
#define EN2 4 

#define EN3 6  
#define EN4 7 

int carSpeed = 200; 

void setup() {
  BTSerial.begin(9600);   

  pinMode(EN1, OUTPUT);
  pinMode(EN2, OUTPUT);

  pinMode(EN3, OUTPUT);
  pinMode(EN4, OUTPUT);
}

void loop() {

  if (BTSerial.available()) {

    char command = BTSerial.read(); // 블루투스로 수신된 명령어를 command에 저장

    if (command == 'F' || command == 'f') {
      // F 또는 f를 입력으로 받는 경우 RC카를 전진시키는 함수 호출
      moving_forward();
    }
    else if (command == 'B' || command == 'b') {
      // B 또는 b를 입력으로 받는 경우 RC카를 후진시키는 함수 호출
      moving_backward();
    }
    else if (command == 'L' || command == 'l') {
      // L 또는 l을 입력으로 받는 경우 RC카를 왼쪽으로 회전시키는 함수 호출
      moving_left();
    }
    else if (command == 'R' || command == 'r') {
      // R 또는 r을 입력으로 받는 경우 RC카를 오른쪽으로 회전시키는 함수 호출
      moving_right();
    }
    else if (command == 'S' || command == 's') {
      // S 또는 s를 입력으로 받는 경우 RC카를 정지시키는 함수 호출
      stop_moving();
    }
    else {
      // 인식하지 못하는 명령어 수신시 RC카를 정지시키는 함수 호출
      stop_moving();
    }

  }

  // 일정시간 지연
  delay(100);
}

void moving_forward() {
  // 모터A를 정방향으로 회전
  digitalWrite(EN1, HIGH);
  digitalWrite(EN2, LOW);

  // 모터B를 정방향으로 회전
  digitalWrite(EN3, HIGH);
  digitalWrite(EN4, LOW);
}

void moving_backward() {
  // 모터A를 역방향으로 회전
  digitalWrite(EN1, LOW);
  digitalWrite(EN2, HIGH);

  // 모터B를 역방향으로 회전
  digitalWrite(EN3, LOW);
  digitalWrite(EN4, HIGH);
}

void moving_left() {
  // 모터A를 정방향으로 회전
  digitalWrite(EN1, HIGH);
  digitalWrite(EN2, LOW);

  // 모터B를 역방향으로 회전
  digitalWrite(EN3, LOW);
  digitalWrite(EN4, HIGH);
}

void moving_right() {
  // 모터A를 역방향으로 회전
  digitalWrite(EN1, LOW);
  digitalWrite(EN2, HIGH);

  // 모터B를 정방향으로 회전
  digitalWrite(EN3, HIGH);
  digitalWrite(EN4, LOW);
}

void stop_moving() {
  digitalWrite(EN1, LOW);
  digitalWrite(EN2, LOW);
  digitalWrite(EN3, LOW);
  digitalWrite(EN4, LOW);
}   

Credits

Lee ye bin

Lee ye bin

4 projects • 1 follower
gledel

gledel

100 projects • 115 followers
Looking back on my childhood, I was happy when I was making something and I was proud of myself. "Making is instinct!"

Comments