永发信息网

求助arduino用超声波手势控制音乐播放

答案:1  悬赏:40  手机版
解决时间 2021-11-28 12:40
  • 提问者网友:山高云阔
  • 2021-11-28 01:46
求助arduino用超声波手势控制音乐播放
最佳答案
  • 五星知识达人网友:一袍清酒付
  • 2021-11-28 01:54
烧写以下代码到UNO:

const int EchoPin = 2; //超声波信号输入

const int TrigPin = 3; //超声波控制信号输出

int currDist = 0;

void setup(){

pinMode(EchoPin, INPUT);

for (int pinindex = 3; pinindex < 8; pinindex++) {

pinMode(pinindex, OUTPUT); // set pins 3 to 10 as outputs

}

pinMode(A0,OUTPUT);

pinMode(A1,OUTPUT);

pinMode(A2,OUTPUT);

pinMode(A3,OUTPUT);

digitalWrite(A0,LOW);

digitalWrite(A1,LOW);

digitalWrite(A2,LOW);

digitalWrite(A3,LOW);

Serial.begin(9600);

}

void loop(){

currDist = MeasuringDistance(); //读取前端距离

//Serial.println(currDist);

if(currDist > 25){

}

//power开关看个人需要,这里没有启用

//else if(currDist < 8){

// mpower();

// }

else if(currDist < 7){

mpause();

}

else if(currDist < 15){

mnext();

}

else if(currDist < 25){

mprev();

}

}

long MeasuringDistance() {

long duration;

digitalWrite(TrigPin, LOW);

delayMicroseconds(2);

digitalWrite(TrigPin, HIGH);

delayMicroseconds(10);

digitalWrite(TrigPin, LOW);

duration = pulseIn(EchoPin, HIGH);

return duration / 29 / 2;

}

//mplay播放

void mplay() {

digitalWrite(A0, HIGH);

delay(100);

digitalWrite(A0, LOW);

Serial.println("mplay");

}

//mprev上一首

void mprev() {

digitalWrite(A1, HIGH);

delay(1000);

digitalWrite(A1, LOW);

Serial.println("mprev");

}

//mnext下一首

void mnext() {

digitalWrite(A2, HIGH);

//return;

delay(1000);

digitalWrite(A2, LOW);

Serial.println("mnext");

}

//mpause暂停

void mpause() {

digitalWrite(A0, HIGH);

delay(1000);

digitalWrite(A0, LOW);

Serial.println("mpause");

}

//mpower

//void mpower() {

// digitalWrite(A3, HIGH);

// delay(500);

//digitalWrite(A3, LOW);

// Serial.println("mpower");

//}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯