1 Star 0 Fork 0

timetzhang/INTERACTIVE.rplidar-piano

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
tt_interaction_rplidar_piano.ino 1.98 KB
一键复制 编辑 原始数据 按行查看 历史
timetzhang 提交于 2018-11-05 18:12 +08:00 . first
#include <Keyboard.h>
// This sketch code is based on the RPLIDAR driver library provided by RoboPeak
#include <RPLidar.h>
// You need to create an driver instance
RPLidar lidar;
uint8_t keys[] = {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
int lastKey;
#define RPLIDAR_MOTOR 3 // The PWM pin for ontrol the speed of RPLIDAR's motor.
// This pin should connected with the RPLIDAR's MOTOCTRL signal
void setup() {
// bind the RPLIDAR driver to the arduino hardware serial
Serial.begin(115200);
Serial1.begin(115200);
Keyboard.begin();
lidar.begin(Serial1);
// set pin modes
pinMode(RPLIDAR_MOTOR, OUTPUT);
}
void loop() {
if (IS_OK(lidar.waitPoint())) {
float distance = lidar.getCurrentPoint().distance; //distance value in mm unit
float angle = lidar.getCurrentPoint().angle; //anglue value in degree
bool startBit = lidar.getCurrentPoint().startBit; //whether this point is belong to a new scan
byte quality = lidar.getCurrentPoint().quality; //quality of the current measurement
//perform data processing here...
if (distance > 0 && distance < 6000 && ((angle >= 0 && angle <= 1) || (angle >= 359 && angle <= 360))) {
Serial.print("distance:");
Serial.print(distance);
Serial.print("/");
Serial.print("angle:");
Serial.print(angle);
Serial.print("/");
Serial.print("startBit:");
Serial.print(startBit);
Serial.print("/");
Serial.print("quality:");
Serial.println(quality);
int i = map(distance, 150, 5800, 0, 8);
if (i != lastKey) {
Keyboard.write(keys[i]);
delay(100);
lastKey = i;
}
}
} else {
analogWrite(RPLIDAR_MOTOR, 0); //stop the rplidar motor
// try to detect RPLIDAR...
rplidar_response_device_info_t info;
if (IS_OK(lidar.getDeviceInfo(info, 100))) {
// detected...
lidar.startScan();
// start motor rotating at max allowed speed
analogWrite(RPLIDAR_MOTOR, 190);
delay(1000);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/timetzhang/INTERACTIVE.rplidar-piano.git
git@gitee.com:timetzhang/INTERACTIVE.rplidar-piano.git
timetzhang
INTERACTIVE.rplidar-piano
INTERACTIVE.rplidar-piano
master

搜索帮助