Use a listener to detect clap from sound module?

All,

I have several sensors and an SPI LCD module hooked up to my Arduino Uno Ethernet and a bunch of code to run during the loop. As a result I am getting results every second over my serial, roughly.

It turns out that the clap sensor (sound module if you like) does not work (it does in a seperate setup and code, not in combination with this setup and code). Three questions:
1 - Could this be caused by the fact that it would be a coincidence that the DigitalRead and the clap intersect?
2 - Can I use a 'listener' that keeps listening my digital input for a clap signal from the module?
3 - Or is there another way to work around this problem?

Thanks a lot for your advice!

Cheers,

MarkyMark

1 - Could this be caused by the fact that it would be a coincidence that the DigitalRead and the clap intersect?

Anything's possible. Although, the intersection between hardware and software is hard to visualize.

2 - Can I use a 'listener' that keeps listening my digital input for a clap signal from the module?

Depends on the module. If it outputs a pulse when it detects a clap, then you could connect it to an interrupt pin, and have it trigger an interrupt.

3 - Or is there another way to work around this problem?

Probably.

You'll notice that these answers are about as vague as your questions. What module are you talking about? Where is your code?

*Removed import and define codes*

// Setup ========================================================================================================
void setup(void) {
  
  //Setup serial cummunication
  Serial.begin(115200); 
  
  //Setting the Arduino I/O ports
  //pinMode(BUTTON, INPUT);
  pinMode(ECHOPIN, INPUT);
  pinMode(TRIGPIN, OUTPUT);
  pinMode(PIR, INPUT);
  pinMode(RELAY, OUTPUT);
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(LASER, OUTPUT);
  pinMode(MIC, INPUT);
   
  //Thermometer =================================================================
  sensors.begin();
  sensors.setResolution(insideThermometer, 12);  // Set the resolution to 12 bit
  
  //Clock =======================================================================
  Wire.begin();
  RTC.begin();
  if (! RTC.isrunning())
  {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

}


// Main loop ====================================================================================================
void loop(void) {
  
  // Microphone =============================================================
  int clap = digitalRead(MIC);
  //clap = 0;  // For debugging purposes only
    if(clap == 1)
  {
    digitalWrite(LASER, HIGH);
  }
  if(clap == 0)
  {
    digitalWrite(LASER, LOW);
  }
  
  Serial.print("Microphone: ");
  Serial.println(clap);
  
  // Temperature ===========================================================
  sensors.requestTemperatures();
  float tempC = sensors.getTempC(insideThermometer);
  
  Serial.print("Temperatuur [C]: ");
  Serial.println(tempC);
  
  if(tempC > CMax)
  {
    CMax = tempC;  // for use on LCD
    Serial.print(" CMax          : ");
    Serial.println(CMax);
  }
   
  if(tempC < CMin)
  {
    CMin = tempC;  // for use on LCD
    Serial.print(" CMin          : ");
    Serial.println(CMin);
  }
  
  if(tempC >= 21.0)  // glow red
  {
    digitalWrite(RED, HIGH);
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, LOW);
  }
  if((tempC >= 15.0) && (tempC < 21.0))  // glow blue
  {
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, HIGH);
    digitalWrite(BLUE, LOW);
  }
  if(tempC < 15.0)  // glow green
  {
    digitalWrite(RED, LOW);
    digitalWrite(GREEN, LOW);
    digitalWrite(BLUE, HIGH);
  }

  // Sonar =================================================================
  // Start ranging
  digitalWrite(LASER,LOW);
  digitalWrite(TRIGPIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIGPIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGPIN, LOW);
  
  // Compute distance
  float distance = pulseIn(ECHOPIN, HIGH);
  distance= distance/58;
  
  if (distance > 2500)
  {
    distance = 0;
  }
  
  else
  {
    Serial.print("Afstand    [cm]: ");
    Serial.println(distance);
  }
  
  if(distance > 0.0 && distance < 10.00)
  {
    digitalWrite(LASER, HIGH);
  }
  
  // Clock =================================================================
  DateTime now = RTC.now();
  unsigned long nu = now.unixtime();

  // PIR ====================================================================
  byte moved = digitalRead(PIR);
  if (moved == 1)
  {
    Serial.println("Motion detected!");
    
    relayAan = (now.unixtime());
    relayUitLang = (now.unixtime() + 30 * 60);  // relay on for 30 minutes
    relayUitKort = (now.unixtime() +  1 * 60);  // relay on for 1 minute
  }
  
  if((now.hour()) < 7)  // before 7 o'clock
  {
    if(nu <= relayUitKort)
    {
    Serial.print("Time left relay on (<7h)   [s]: ");
    Serial.println(relayUitKort - nu);
    digitalWrite(RELAY, HIGH);  // turn RELAY ON
    }
    
    if(nu > relayUitKort)
    {
    Serial.println("Relay          : OFF");
    digitalWrite(RELAY, LOW);  // turn RELAY OFF
    } 
  }
  
  if( ((now.hour()) >= 7) && ((now.hour()) < 22) )  // between 7 and 22 o'clock
  {
    if(nu <= relayUitLang)
    {
    Serial.print("Time left relay on (7-22u) [s]: ");
    Serial.println(relayUitLang - nu);
    digitalWrite(RELAY, HIGH);  // turn RELAY ON
    }
    
    if(nu > relayUitLang)
    {
    Serial.println("Relay          : OFF");
    digitalWrite(RELAY, LOW);  // turn RELAY OFF
    }
  }
  
  if( (now.hour()) >= 22)  // after 22 o'clock
  {
    if(nu <= relayUitKort)
    {
    Serial.print("Time left relay on (>22h)  [s]: ");
    Serial.println(relayUitKort - nu);
    digitalWrite(RELAY, HIGH);  // turn RELAY ON
    }
    
    if(nu > relayUitKort)
    {
    Serial.println("Relay          : OFF");
    digitalWrite(RELAY, LOW);  // turn RELAY OFF
    } 
  }
  
  // LCD ====================================================================
  u8g.firstPage();  
  do
  {
    draw(now, weekdays, regel, kolom, distance, tempC, CMin, CMax, moved);
  }
  
  while( u8g.nextPage() );
  
  // rebuild the picture after some delay
  //delay(100);

// Screensaver ================================================================
  nSinceStart ++;
  if(nSinceStart % 100 == 0)
  {
    regel ++;
    kolom ++;
    if(regel>4)
    {
      regel = 1;
    }
    if(kolom>2)
    {
      kolom = 0;
    }
  }

}


// Required Functions ===========================================================================================

// Write to LCD ===============================================================
void draw(DateTime now, String weekdays[], byte regel, byte kolom, float distance, float tempC, float CMin, float CMax, byte moved)
{
 
  //DATUM ====================================
  u8g.setFont(u8g_font_5x7);
  u8g.setPrintPos(kolom, 10+regel);
  u8g.print("=");
  u8g.print(weekdays[now.dayOfWeek()]);
  u8g.print(" ");
  u8g.print(now.day(), DEC);
  u8g.print(".");
  u8g.print(now.month(), DEC);
  u8g.print(".");
  u8g.print(now.year(), DEC);
  
  u8g.print(" ");
  
  u8g.print(now.hour(), DEC);
  u8g.print(":");
  u8g.print(now.minute(), DEC);
  u8g.print(":");
  u8g.print(now.second(), DEC);
  u8g.print("=");
 
  //AFSTAND ===================================
  u8g.setFont(u8g_font_7x14);
  u8g.setPrintPos(kolom, 30+regel);
  u8g.print("Afstand: ");
  if(distance == 0)
  {
    u8g.print("-    ");
  }
  else
  {
  u8g.print(distance);
  }
  if(moved==1)
  {
    u8g.print("  *");
  }
 
  //TEMPERATUUR ===============================
  u8g.setFont(u8g_font_7x14);
  u8g.setPrintPos(kolom, 45+regel);
  u8g.print("Temperatuur: ");
  u8g.print(tempC); 
  
  u8g.setFont(u8g_font_5x7);
  u8g.setPrintPos(kolom, 57+regel);
  u8g.print(" Min: ");
  u8g.print(CMin);
  u8g.print(" - Max: ");
  u8g.print(CMax); 
  
}

// Convert normal decimal numbers to binary coded decimal =====================
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}

// Convert binary coded decimal to normal decimal number ======================
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}

See your point PaulS, thanks. Hope this will help how I can realize that the clap is detected by the sounds module (Recommendations For You - DealeXtreme), even though the loop takes a bit more then a second (as far as I can detect).

Thanks!

  int clap = digitalRead(MIC);
  clap = 0;

Why bother reading the pin?

I don't see that the clapper is doing anything useful. I'm not sure why it is even connected.

If your loop() function takes a second to execute, with the only delays I see being in the ping sensor code, then the u8g stuff must be what is taking so long.

Sharp! Did not take the test 'clap = 0;' out before posting the code...

I'd like to be able to turn on the relay by the PIR as well as by clapping.

Newbie suggestion : use interrupt