本文主要是介绍中原工 摘星车 蓝牙控制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
好久前 比赛完成 超级给力
https://g.alicdn.com/idleFish-F2e/app-basic/item.html?itemid=573263026793&ut_sk=1.WxEIBJ%2FiSUoDADyw2O%2FQUZqV_21407387_1538638809582.Copy.detail.573263026793.1977947810&forceFlush=1
现在 记录下 代码
#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // RX, TX
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos=0;
int base=2; //定义从2号端口开始
int num=4; //定义4个端口
int p1=6;
int p2=7;
int p3=8;
int p4=9;
int p5=10;// variable to store the servo position
char val=’/’;
void setup()
{
for(int i=base;i<base+num;i++)
{
pinMode(i,OUTPUT); //端口为输出端
digitalWrite(i,HIGH); //相应端口初始化为高电平
Serial.begin(9600);
myservo.attach(p1); // attaches the servo on pin 2 to the servo object
myservo.write(pos); //初始化所有舵机位置,防止通电抖动。
delay(100);
myservo.detach();
}
Serial.begin(9600);
}
void loop() // run over and over 上升
{
val=Serial.read();
if(val==‘1’)
{
pos=pos+5; //anti-clock motion
myservo.attach(p1);
myservo.write(pos);
delay(50);
myservo.detach();
val=‘w’;
}
if(val==‘2’)
{
pos=pos-5; //clock motion
myservo.attach(p1);
myservo.write(pos);
delay(100);
myservo.detach();
val=‘r’;
}
if(val==‘3’)
{
pos=60; //initialization
myservo.attach(p1);
myservo.write(pos);
delay(50);
myservo.detach();
val=‘r’;
}
val=Serial.read();
if(val==‘11’)
{
pos=pos+5; //anti-clock motion
myservo.attach(p2);
myservo.write(pos);
delay(50);
myservo.detach();
val=‘w’;
}
if(val==‘22’)
{
pos=pos-5; //clock motion
myservo.attach(p2);
myservo.write(pos);
delay(100);
myservo.detach();
val=‘r’;
}
if(val==‘33’)
{
pos=60; //initialization
myservo.attach(p2);
myservo.write(pos);
delay(50);
myservo.detach();
val=‘r’;
}
val=Serial.read();
if(val==‘111’)
{
pos=pos+5; //anti-clock motion
myservo.attach(p3);
myservo.write(pos);
delay(50);
myservo.detach();
val=‘w’;
}
if(val==‘222’)
{
pos=pos-5; //clock motion
myservo.attach(p3);
myservo.write(pos);
delay(100);
myservo.detach();
val=‘r’;
}
if(val==‘333’)
{
pos=60; //initialization
myservo.attach(p3);
myservo.write(pos);
delay(50);
myservo.detach();
val=‘r’;
}
val=Serial.read();
if(val==‘1111’)
{
pos=pos+5; //anti-clock motion
myservo.attach(p4);
myservo.write(pos);
delay(50);
myservo.detach();
val=‘w’;
}
if(val==‘2222’)
{
pos=pos-5; //clock motion
myservo.attach(p4);
myservo.write(pos);
delay(100);
myservo.detach();
val=‘r’;
}
if(val==‘3333’)
{
pos=60; //initialization
myservo.attach(p4);
myservo.write(pos);
delay(50);
myservo.detach();
val=‘r’;
}
val=Serial.read(); //接收数据
if(val==‘q’) //停止
{
digitalWrite(2,HIGH);
digitalWrite(3,HIGH);
}
if(val==‘w’) //前进
{
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
}
if(val==‘e’) // 后退
{
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
}
val=Serial.read(); //接收数据
if(val==‘8’) //停止
{
digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
}
if(val==‘9’) //前进
{
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
}
if(val==‘10’) // 后退
{
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
}
}
其实就是 机械臂小车
这篇关于中原工 摘星车 蓝牙控制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!