Обнаружение нескольких хлопков с помощью датчика хлопка (Arduino Nano) - PullRequest
0 голосов
/ 07 октября 2018

Для школы я делаю датчик хлопков с моим Arduino Nano.Я нашел некоторый код, чтобы определить, есть ли 2 хлопка ( ссылка ).Но теперь я хочу изменить код, чтобы он мог различить, если я хлопнул 1,2 или 3 раза.Теперь я изменил источник, чтобы обнаружить 1 или 2 хлопка.Но теперь, если я хлопаю дважды, всегда обнаруживается один хлопок, прежде чем он увидит 2 хлопка.И я совершенно не знаю, как обнаружить 3 хлопка.Может кто-нибудь помочь мне с этой проблемой?

Код:

    #define signalToRelayPin              12
    #define sensorPin                      7

    int lastSoundValue;
    int soundValue;
    long lastNoiseTime = 0;
    long currentNoiseTime = 0;
    long lastLightChange = 0;
    int relayStatus = HIGH;

    void setup() {
      pinMode(sensorPin, INPUT);
      pinMode(signalToRelayPin, OUTPUT);
      Serial.begin(115200);
    }

    struct DataBlockStruct  meting1,meting2;

    void loop() {

      soundValue = digitalRead(sensorPin);
      currentNoiseTime = millis();

      if (soundValue == 1) { // if there is currently a noise
        if (
          (currentNoiseTime > lastNoiseTime + 200) && // to debounce a sound occurring in more than a loop cycle as a single noise
          (lastSoundValue == 0) &&  // if it was silent before
          (currentNoiseTime < lastNoiseTime + 800) && // if current clap is less than 0.8 seconds after the first clap
          (currentNoiseTime > lastLightChange + 1000) // to avoid taking a third clap as part of a pattern
        ) {

          relayStatus = !relayStatus;
          Serial.println("2 X CLAP");

         } else {    
          Serial.println("1 X CLAP");
          }

         lastNoiseTime = currentNoiseTime;
      }

      lastSoundValue = soundValue;


    }

1 Ответ

0 голосов
/ 07 октября 2018

Попробуйте этот фрагмент:

    #define signalToRelayPin              12
    #define sensorPin                      7

    int lastSoundValue;
    int soundValue;
    long lastNoiseTime = 0;
    long currentNoiseTime = 0;
    long lastLightChange = 0;
    int relayStatus = HIGH;
    int clap_interval = 500;
    int claps = 0;

    void setup() {
       pinMode(sensorPin, INPUT);
       pinMode(signalToRelayPin, OUTPUT);
       Serial.begin(115200);
    }

    struct DataBlockStruct  meting1,meting2;



void loop() {

    soundValue = digitalRead(sensorPin);
    currentNoiseTime = millis();

    if (soundValue == 1 && lastSoundValue == 0) 
    { 
        if (claps == 0) // allow first to register without much condition
        {
            claps = 1;
            lastNoiseTime = currentNoiseTime;
        }
        else
        {
            if (currentNoiseTime > lastNoiseTime + clap_interval)
            {
                claps++;
                lastNoiseTime = currentNoiseTime;
                            relayStatus = !relayStatus;
            }
        }           
    }
    else
    {
        if (currentNoiseTime > lastNoiseTime + 2 * clap_interval) // no claps for a longer duration time to print and/or reset clap
        {
            if (claps > 0)
            {
                Serial.print(claps);
                Serial.println(" CLAPS");
                claps = 0; ///reset
            }
        }
    }

    //lastSoundValue = soundValue;
    delay(50); // delay polling
}
...