用户
 找回密码
 立即注册

QQ登录

只需一步,快速开始

点击进入授权页面

只需一步,快速开始

  • QQ空间
  • 回复
  • 收藏

该用户从未签到

串口与esp8266_01的串口相连,esp8266用的电源也是板上的电源
求大神指教!!!
以下是代码
[C++] 纯文本查看 复制代码
#include <Servo.h>

Servo myservo;

const int L_motor=10;

const int R_motor=11;

const int servo=9;

const int N_speed=150;

const int Degree_S=90;

char val;

void Stop(){

    analogWrite(L_motor,0);

    analogWrite(R_motor,0); 

}

void TurnLeft(){

  for(int i=150,j=150;i<=250;i+=5,j-=5){

    analogWrite(R_motor,i);

    analogWrite(L_motor,j);

    delay(30);

  }

  Serial.println("Left");

  delay(300);

  Stop();

}

void TurnRight(){

  for(int i=150,j=150;i<=250;i+=5,j-=5){

    analogWrite(L_motor,i);

    analogWrite(R_motor,j);

    delay(30);

  }

  Serial.println("Right");

  delay(300);

  Stop();

}

void SpeedUp(){

  analogWrite(L_motor,N_speed);

  analogWrite(R_motor,N_speed);

  Serial.println("UP");

  delay(800);

  Stop();

}



void STurnLeft(){

  analogWrite(L_motor,N_speed);

  analogWrite(R_motor,N_speed);

  for(int i=Degree_S;i>=60;i-=3){

    myservo.write(i);  

    delay(100);

  }

  Stop();

}

void STurnRight(){

  analogWrite(L_motor,N_speed);

  analogWrite(R_motor,N_speed);

  for(int i=Degree_S;i<=120;i+=3){

    myservo.write(i);

    delay(100);

  }

  Stop();

}

void setup(){

  pinMode(L_motor,OUTPUT);

  pinMode(R_motor,OUTPUT);

  pinMode(servo,OUTPUT);

  Serial.begin(115200);

  myservo.attach(servo);

}

void loop(){

  val='_';

  if(Serial.available()){

    val=Serial.read();

    //Serial.write(val);

    while(Serial.read()>=0);

  }

  switch(val){

    case 'a' :TurnLeft();break;

    case 'd' :TurnRight();break;

    case 'w' :SpeedUp();break;

    case 's' :Stop();break;

    case 'A' :STurnLeft();break;

    case 'D' :STurnRight();break;

  }

  

}



在线等,望大神扶我一把
发新帖
发表评论
高级模式  
您需要登录后才可以回帖 登录 | 立即注册  
关闭

推荐主题 上一条 /2 下一条