我是靠谱客的博主 斯文奇迹,最近开发中收集的这篇文章主要介绍基于arduino 蓝牙麦克拉姆轮小车,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include <SoftwareSerial.h> 
// Pin10接HC05的TXD
// Pin1接HC05的RXD
SoftwareSerial BT(0, 1); 
char val;
int L1_IN1 = 7;int L1_IN2 = 5;int L1_ENA = 6;//左前轮
int R1_IN1 = 4;int R1_IN2 = 2;int R1_ENA = 3;//右前轮
int L2_IN1 = 12;int L2_IN2 = 13;int L2_ENA = 11;//左后轮
int R2_IN1 = 8;int R2_IN2 = 9;int R2_ENA = 10;//右后轮
int sp=200;
void setup() {
  // put your setup code here, to run once:
  pinMode(L1_IN1, OUTPUT);pinMode(L1_IN2, OUTPUT);pinMode(L1_ENA, OUTPUT);
  pinMode(R1_IN1, OUTPUT);pinMode(R1_IN2, OUTPUT);pinMode(R1_ENA, OUTPUT);
  pinMode(L2_IN1, OUTPUT);pinMode(L2_IN2, OUTPUT);pinMode(L2_ENA, OUTPUT);
  pinMode(R2_IN1, OUTPUT);pinMode(R2_IN2, OUTPUT);pinMode(R2_ENA, OUTPUT);

  
  Serial.begin(9600);
  Serial.println("bluetooth is ready!");
  BT.begin(9600);
}
 
void loop() 
{
 
  if (BT.available()) {
    val = BT.read();
    Serial.print(val);
  }
  switch(val){
    case '0':allstop();break;
    case '1':go_ahead(sp);break;
    case '2':go_back(sp);break;
     case '3':turn_left(sp);;break;
    case '4':turn_right(sp);break;
     case '5':turn_around(sp);break;
    case '6':left_move(sp);break;
    case '7':right_move(sp);break;
    case '+':sp=sp+10;delay(200);break;
    case '-':sp=sp-10;delay(200);break;
    case '=':sp=sp;break;
    default :allstop();
      break;
  }
  if(sp<0)
  sp=0;
  else if(sp>255)
  sp=255;
}

void go_ahead(int sp)
{
      L1_forward(sp);
      R1_forward(sp);
      L2_forward(sp);
      R2_forward(sp);
}

void go_back(int sp)
{
  L1_backward(sp);
  R2_backward(sp);
  L2_backward(sp);
  R1_backward(sp);
}
void turn_right(int sp)
{
      L1_forward(sp);//左边前进
      L2_forward(sp);
      digitalWrite(R2_IN2,LOW);//右边不动
      digitalWrite(R1_IN2,LOW);
      digitalWrite(R2_IN1,LOW);
      digitalWrite(R1_IN1,LOW);
}
void turn_left(int sp)
{
      R2_forward(sp);//右边前进
      R1_forward(sp);
      digitalWrite(L2_IN2,LOW);//左边不动
      digitalWrite(L1_IN2,LOW);
      digitalWrite(L2_IN1,LOW);
      digitalWrite(L1_IN1,LOW);
}
void left_move(int sp)
{
  L1_backward(sp);
   L2_forward(sp);
   R1_forward(sp);
  R2_backward(sp);
}
void right_move(int sp)
{
  L2_backward(sp);
   L1_forward(sp);
   R2_forward(sp);
  R1_backward(sp);
}
void L1_forward(int sp)//左前轮前进
{
  digitalWrite(L1_IN2,HIGH);
  digitalWrite(L1_IN1,LOW);
  analogWrite(L1_ENA,sp);
}
void R1_forward(int sp)//右前轮前进
{
  digitalWrite(R1_IN2,HIGH);
  digitalWrite(R1_IN1,LOW);
  analogWrite(R1_ENA,sp);
}
void L2_forward(int sp)//左后轮前进
{
  digitalWrite(L2_IN2,HIGH);
  digitalWrite(L2_IN1,LOW);
  analogWrite(L2_ENA,sp);
}
void R2_forward(int sp)//右后轮前进
{
  digitalWrite(R2_IN2,HIGH);
  digitalWrite(R2_IN1,LOW);
  analogWrite(R2_ENA,sp);
}

void turn_around(int sp)
{
   L1_backward(sp);
   L2_backward(sp);
   R2_forward(sp);
   R1_forward(sp);
}
void allstop()
{
  digitalWrite(L1_IN1,LOW);
  digitalWrite(L1_IN2,LOW);  
  digitalWrite(R1_IN1,LOW);
  digitalWrite(R1_IN2,LOW);
  digitalWrite(L2_IN1,LOW);
  digitalWrite(L2_IN2,LOW);
  digitalWrite(R2_IN1,LOW);
  digitalWrite(R2_IN2,LOW);
}
void L1_backward(int sp)//左前轮后退
{
  digitalWrite(L1_IN2,LOW);
  digitalWrite(L1_IN1,HIGH);
  analogWrite(L1_ENA,sp);
}
void R1_backward(int sp)//右前轮后退
{
  digitalWrite(R1_IN2,LOW);
  digitalWrite(R1_IN1,HIGH);
  analogWrite(R1_ENA,sp);
}
void L2_backward(int sp)//左后轮后退
{
  digitalWrite(L2_IN2,LOW);
  digitalWrite(L2_IN1,HIGH);
  analogWrite(L2_ENA,sp);
}
void R2_backward(int sp)//右后轮后退
{
  digitalWrite(R2_IN2,LOW);
  digitalWrite(R2_IN1,HIGH);
  analogWrite(R2_ENA,sp);
}

最后

以上就是斯文奇迹为你收集整理的基于arduino 蓝牙麦克拉姆轮小车的全部内容,希望文章能够帮你解决基于arduino 蓝牙麦克拉姆轮小车所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部