求助各位大神 Arduino+直流电机 (语音控制+触摸按键控制)

各为大神们好,刚加入CSND就有一个小项目的问题想要咨询大家:

本次的项目是 用非特定人语音传感器LD3320 来控制 12V的直流电机(连接了LN298的电机驱动模块),又加了额外的一个触摸传感器想要增加一个控制模块。

想要实现的功能:语音控制电机的正反转。每次说上升之后,电机正转8秒停。每次说下降之后,电机反转8秒停。

触摸传感器也是实现一样的功能。

逻辑:触摸传感器和语音传感器可以单独触发,但是当语音控制上升之后,触摸传感器被触摸之后只能触发下降的指令,语音传感控制下降之后,如果这时候触发触摸按键的时候,只能触发上升的命令。

问题是:怎么把两个整合在一起?现在语音控制没有问题,但是没有办法像我设想的运行逻辑和触摸不起作用。。

恳请大神们帮我解决一下问题,跪谢!!!

#include 
#include
#include

VoiceRecognition Voice;                         //声明一个语音识别对象

//int i=0;
int input1 = 3; // 定义uno的pin 5 向 input1 输出
int input2 = 5; // 定义uno的pin 6 向 input2 输出
int input3 = 6; // 定义uno的pin 9 向 input3 输出
int input4 = 10;// 定义uno的pin 10向 input4 输出

#define S1 7//触摸开关口为7

unsigned char mode;
unsigned char temp;

void key_scan();

void setup()
{
  pinMode(S1, INPUT); //输入

  // 初始化各IO, 模式为OUTPUT 输出模式
  pinMode(input1, OUTPUT);
  pinMode(input2, OUTPUT);
  pinMode(input3, OUTPUT);
  pinMode(input4, OUTPUT);

  //初始状态电机不转
  digitalWrite(input1, LOW); //给低电平
  digitalWrite(input2, LOW); //给低电平
  digitalWrite(input3, LOW); //给低电平
  digitalWrite(input4, LOW); //给低电平

  Serial.begin(9600);                        //配置9600
  Serial.print("Motor start!\n");            //Motor启动
  Serial.print("Voiceread start!\n");        //声音识别启动
  Voice.init();                               //初始化VoiceRecognition模块
  Voice.addCommand("shang sheng", 0);         //上升到30度
  Voice.addCommand("xia jiang", 1);           //下降到180度初始状态

  Voice.addCommand("shang", 2);            //杂音
  Voice.addCommand("sheng", 3);            //杂音
  Voice.addCommand("xia", 4);              //杂音
  Voice.addCommand("jiang", 5);            //杂音
  Voice.addCommand("zhuan", 6);            //杂音
  Voice.addCommand("san shi zhuan", 7);
  Voice.addCommand("ke jin zhuan", 8);
  Voice.addCommand("zhuan bu zhuan", 9);
  Voice.addCommand("pa pi jiang", 10);
  Voice.addCommand("ping guo jiang", 11);
  Voice.addCommand("xiang jiang", 12);
  Voice.addCommand("xiao zhi", 13);
  Voice.addCommand("zhi ma jiang", 14);
  Voice.addCommand("xiang qian", 15);
  Voice.addCommand("xiang hou", 16);
  Voice.addCommand("xia jiang ba", 17);
  Voice.addCommand("shang sheng ba", 18);
  Voice.addCommand("zhuan xin", 19);
  Voice.addCommand("shang sheng ya", 20);
  Voice.addCommand("xia jiang ya", 21);
  Voice.addCommand("xuan zhuan", 22);
  Voice.addCommand("zou yi zou", 23);
  Voice.addCommand("wang xia zou", 24);
  Voice.addCommand("xia sheng", 25);
  Voice.addCommand("shang xia", 26);
  Voice.addCommand("zhuan dong", 27);
  Voice.addCommand("cha zhang", 28);
  Voice.addCommand("zhu yi", 29);
  Voice.addCommand("en en", 30);
  Voice.addCommand("en", 31);
  Voice.addCommand("bai bai", 32);
  Voice.addCommand("chao song", 33);
  Voice.addCommand("shang qu", 34);
  Voice.addCommand("xia lai", 35);
  Voice.addCommand("shang liang", 36);
  Voice.addCommand("shang gang", 37);
  Voice.addCommand("shang xing", 38);
  Voice.addCommand("xia qu", 39);
  Voice.addCommand("xia gang", 40);
  Voice.addCommand("jiang yu", 41);
  Voice.addCommand("fan kui", 42);
  Voice.addCommand("jiang yao", 43);
  Voice.addCommand("xia jian", 44);
  Voice.addCommand("hua chuan", 45);
  Voice.addCommand("xia bu xia jiang", 46);
  Voice.addCommand("shang bu shang sheng ", 47);
  Voice.addCommand("xia lou", 48);
  Voice.addCommand("xia gao", 49);
  Voice.addCommand("jiang tou", 50);

  Voice.start();//开始识别
}
void loop()
{
    key_scan();
    if (mode == 1)
    {
     Serial.print("Motor forward\n");//上升
     digitalWrite(input1, HIGH); //给高电平
     digitalWrite(input2, LOW); //给低电平
     digitalWrite(input3, HIGH); //给高电平
     digitalWrite(input4, LOW); //给低电平
     delay(8000);   //运行8秒
     //电机停
     digitalWrite(input1, LOW); //给高电平
     digitalWrite(input2, LOW); //给低电平
     digitalWrite(input3, LOW); //给高电平
     digitalWrite(input4, LOW); //给低电平
     delay(30);
    }
    else
    {
     Serial.print("Motor backward\n");//上升
     digitalWrite(input1, LOW); //给高电平
     digitalWrite(input2, HIGH); //给低电平
     digitalWrite(input3, LOW); //给高电平
     digitalWrite(input4, HIGH); //给低电平
     delay(8000);   //运行8秒
     //电机停
     digitalWrite(input1, LOW); //给低电平
     digitalWrite(input2, LOW); //给低电平
     digitalWrite(input3, LOW); //给低电平
     digitalWrite(input4, LOW); //给低电平
     delay(30);
    }
 
  switch (Voice.read())                //判断识别
  {
    case 0:  //30度
      //mode = 0;
      Serial.print("Motor forward\n");//上升
      digitalWrite(input1, LOW); //给高电平
      digitalWrite(input2, HIGH); //给低电平
      digitalWrite(input3, LOW); //给高电平
      digitalWrite(input4, HIGH); //给低电平
      delay(8000);   //运行1秒
      digitalWrite(input1, LOW); //给高电平
      digitalWrite(input2, LOW); //给低电平
      digitalWrite(input3, LOW); //给高电平
      digitalWrite(input4, LOW); //给低电平
      break;
    case 1: //180度
      //mode = 1;
      Serial.print("Motor backward\n");//下降
      digitalWrite(input1, HIGH);
      digitalWrite(input2, LOW);
      digitalWrite(input3, HIGH);
      digitalWrite(input4, LOW);
      delay(8000);   //运行一秒
      digitalWrite(input1, LOW); //给高电平
      digitalWrite(input2, LOW); //给低电平
      digitalWrite(input3, LOW); //给高电平
      digitalWrite(input4, LOW); //给低电平
      break;
    case 2:
      //analogWrite(Led, 30);//30
      Serial.print("Unknown\n");
      break;
    case 3:
      //analogWrite(Led, 80);//80
      Serial.print("Unknown\n");
      break;
    case 4:
      //analogWrite(Led, 120);//120
      Serial.print("Unknown\n");
      break;
    case 5:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 6:
      //analogWrite(Led, 200);//200
      Serial.print("Unknown\n");
      break;
    case 7:
      //analogWrite(Led, 30);//30
      Serial.print("Unknown\n");
      break;
    case 8:
      //analogWrite(Led, 80);//80
      Serial.print("Unknown\n");
      break;
    case 9:
      //analogWrite(Led, 120);//120
      Serial.print("Unknown\n");
      break;
    case 10:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 11:
      //analogWrite(Led, 200);//200
      Serial.print("Unknown\n");
      break;
    case 12:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 13:
      //analogWrite(Led, 200);//200
      Serial.print("Unknown\n");
      break;
    case 14:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 15:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 16:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 17:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 18:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 19:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 20:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 21:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 22:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 23:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 24:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 25:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 26:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 27:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 28:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 29:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 30:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 31:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 32:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 33:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 34:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 35:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 36:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 37:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 38:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 39:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 40:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 41:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 42:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 43:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 44:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 45:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 46:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 47:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 48:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 49:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
    case 50:
      //analogWrite(Led, 150);//150
      Serial.print("Unknown\n");
      break;
  }
}
void key_scan()
{
  temp = digitalRead(7);
  if (temp == HIGH)
  {
    delay(20);
    temp = digitalRead(7);
    if (temp == HIGH)
    {
      mode = !mode;
    }
    while (temp == HIGH) {
      temp = digitalRead(7);
    }
  }
}

你可能感兴趣的:(求助各位大神 Arduino+直流电机 (语音控制+触摸按键控制))