蓝牙小车和循迹
(42)

蓝牙小车

材料准备

1、arduino开发板
2、HC-05蓝牙模块
3、L298N驱动板*2
4、18650电池*3
5、电池座
6、小车底盘
7、mg955舵机
che.jpg

源码

#include"Servo.h"
Servo myservo;
#define STOP      0
#define FORWARD      1
#define BACKWARD     2
#define TURNLEFT     3
#define TURNRIGHT    4
#define TURNLEFTLOW  5
#define TURNRIGHTLOW 6
#define FORWARDLOW   9
int leftMotor1 = 4;
int leftMotor2 = 5;
int rightMotor1 = 6;
int rightMotor2 = 7;
int leftMotor3 = 8;
int leftMotor4 = 9;
int rightMotor3 = 10;
int rightMotor4 = 11;

void setup() {
  Serial.begin(9600);
  pinMode(leftMotor1, OUTPUT);
  pinMode(leftMotor2, OUTPUT);
  pinMode(rightMotor1, OUTPUT);
  pinMode(rightMotor2, OUTPUT);
  pinMode(leftMotor3, OUTPUT);
  pinMode(leftMotor4, OUTPUT);
  pinMode(rightMotor3, OUTPUT);
  pinMode(rightMotor4, OUTPUT);
  pinMode(3,OUTPUT);
  myservo.attach(3);
  myservo.write(180);
}
void loop() {
  if(Serial.available()>0)
  {
    char cmd = Serial.read();
    Serial.println(cmd);
    motorRun(cmd);     
  }
}
void motorRun(char cmd)
{
  switch(cmd){
    case FORWARD:
      Serial.println("FORWARD");
      digitalWrite(leftMotor1, HIGH);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, HIGH);
      digitalWrite(rightMotor2, LOW);
      digitalWrite(leftMotor3, HIGH);
      digitalWrite(leftMotor4, LOW);
      digitalWrite(rightMotor3, HIGH);
      digitalWrite(rightMotor4, LOW);
      break;
     case BACKWARD:
      Serial.println("BACKWARD");
      digitalWrite(leftMotor1, LOW);
      digitalWrite(leftMotor2, HIGH);
      digitalWrite(rightMotor1, LOW);
      digitalWrite(rightMotor2, HIGH);
      digitalWrite(leftMotor3, LOW);
      digitalWrite(leftMotor4, HIGH);
      digitalWrite(rightMotor3, LOW);
      digitalWrite(rightMotor4, HIGH);
      break;
     case TURNLEFT:
      Serial.println("TURN  LEFT");
      
      digitalWrite(leftMotor1, HIGH);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, LOW);
      digitalWrite(rightMotor2, HIGH);
      digitalWrite(leftMotor3, HIGH);
      digitalWrite(leftMotor4, LOW);
      digitalWrite(rightMotor3, LOW);
      digitalWrite(rightMotor4, HIGH);
      break;
     case TURNRIGHT:
      Serial.println("TURN  RIGHT");
      digitalWrite(leftMotor1, LOW);
      digitalWrite(leftMotor2, HIGH);
      digitalWrite(rightMotor1, HIGH);
      digitalWrite(rightMotor2, LOW);
      digitalWrite(leftMotor3, LOW);
      digitalWrite(leftMotor4, HIGH);
      digitalWrite(rightMotor3, HIGH);
      digitalWrite(rightMotor4, LOW);
      break;
      case FORWARDLOW:
      Serial.println("FORWARD LOW");
      digitalWrite(leftMotor1, HIGH);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, HIGH);
      digitalWrite(rightMotor2, LOW);
      digitalWrite(leftMotor3, LOW);
      digitalWrite(leftMotor4, LOW);
      digitalWrite(rightMotor3, LOW);
      digitalWrite(rightMotor4, LOW);
      break;
     case TURNLEFTLOW:
      Serial.println("TURN  LEFT LOW");
      
      digitalWrite(leftMotor1, HIGH);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, LOW);
      digitalWrite(rightMotor2, LOW);
      digitalWrite(leftMotor3, HIGH);
      digitalWrite(leftMotor4, LOW);
      digitalWrite(rightMotor3, LOW);
      digitalWrite(rightMotor4, HIGH);
      break;
     case TURNRIGHTLOW:
      Serial.println("TURN  RIGHT LOW");
      digitalWrite(leftMotor1, LOW);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, HIGH);
      digitalWrite(rightMotor2, LOW);
      digitalWrite(leftMotor3, LOW);
      digitalWrite(leftMotor4, HIGH);
      digitalWrite(rightMotor3, HIGH);
      digitalWrite(rightMotor4, LOW);
      break;
     case 7: 
      Serial.println("7");
      myservo.write(10);
      delay(1000);
      break; 
    case 8:
      Serial.println("8");
      myservo.write(170);
      delay(1000);
      break;
     case STOP:
      Serial.println("STOP");
      digitalWrite(leftMotor1, LOW);
      digitalWrite(leftMotor2, LOW);
      digitalWrite(rightMotor1, LOW);
      digitalWrite(rightMotor2, LOW);
      digitalWrite(leftMotor3, LOW);
      digitalWrite(leftMotor4, LOW);
      digitalWrite(rightMotor3, LOW);
      digitalWrite(rightMotor4, LOW);
      break;
  }
}

循迹小车

本文为作者admin发布,未经允许禁止转载!
上一篇 下一篇
评论
暂无评论 >_<
加入评论