乐高遥控小车
Language | English |
---|
目录原理通过手机端的mTank软件发送指令,叠加在mCookie的蓝牙模块接收并解析指令 通过IIC通讯,mCookie控制LEGOShield,完成对NXT接口电机和传感器的控制 设备
程序说明#include <mCookieLEGO.h>
#include <Microduino_Protocol_HardSer.h>
#include "Wire.h"
uint16_t Data[8];
int speedLeft = 0;
int speedRight = 0;
Protocol ProtocolB(&Serial1, TYPE_NUM);
mCookieLEGO m;
void setup(){
Wire.begin();
Serial.begin(9600);
ProtocolB.begin(9600); //9600/19200/38400
m.configSensorType(mCookieLEGO::S1, mCookieLEGO::SENSOR_ULTRA);
m.configConnection(0xFFFF);
}
uint8_t dis;
void loop(){
int sta = ProtocolB.parse(Data, MODE_WHILE);
if (sta != P_NONE) {
switch (sta) {
case P_FINE:
speedLeft = map(Data[1], 1000, 2000, -255, 255);
speedRight = speedLeft;
int speedTurn = map(Data[4], 1000, 2000, -255, 255);
if(speedTurn < 0){
speedLeft += speedTurn;
}
else{
speedRight -= speedTurn;
}
break;
// default:
// break;
}
}
refreshSpeed();
delay(10);
}
void refreshSpeed{
m.motor_setFixedDrive(mCookieLEGO::M3, speedLeft);
m.motor_setFixedDrive(mCookieLEGO::M4, speedRight);
}
|