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 (does in seperate setup and code, not in combination with this setup and code). Two 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?
Thanks a lot for your advice!
Cheers,
MarkyMark
Use a listener to detect clap from sound module?
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
-
- Posts: 3
- Joined: Tue Dec 11, 2012 6:47 pm
- Franklin97355
- Posts: 23911
- Joined: Mon Apr 21, 2008 2:33 pm
Re: Use a listener to detect clap from sound module?
It really helps if, when you ask a question about your code you include your code. Insert it between
Code: Select all
tags to maintain formatting.
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Use a listener to detect clap from sound module?
Trying to do two things at the same time can be tricky on an Arduino. If you post the code you are using, we can better see where the problem is.
-
- Posts: 3
- Joined: Tue Dec 11, 2012 6:47 pm
Re: Use a listener to detect clap from sound module?
Code: Select all
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// Filename: AllSensor_V4.ino //
// Date: 2012.12.10 //
// Goal: Adruino file to experiment with connecting different sensors //
// Origin: Own programming, in some cases combined with code available from the internet (via GOOGLE), //
// big thanks to all these unknown contributors! //
// //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Import required libraries ==================================================
#include "U8glib.h"
#include "Wire.h"
#include "RTClib.h"
#include <OneWire.h>
#include <DallasTemperature.h>
// Setup button ===============================================================
//#define BUTTON 1 // Pin to R
// Setup HC-SR04 sensor =======================================================
#define ECHOPIN 2 // Pin to receive echo pulse (wit)
#define TRIGPIN 3 // Pin to send trigger pulse (groen)
// Setup PIR ==================================================================
#define PIR 4 // Pin to PIR
// Setup 18B20 sensor =========================================================
#define ONE_WIRE_BUS 5
// Setup JQC-3FF ==============================================================
#define RELAY 1
// RGB LED ====================================================================
#define RED 10 // Setup the button inpu
#define GREEN 11 // Pin to G
#define BLUE 12 // Pin to B
// Setup laser ================================================================
#define LASER 13 // Pin to laser
// Setup micropohone ==========================================================
#define MIC 6 // Pin to mic
// Setup u8g display object ===================================================
U8GLIB_ST7920_128X64 u8g(9, 8, 7, U8G_PIN_NONE); // SPI Com: SCK = en = 9, MOSI = rw = 8, CS = di = 7
// Setup a oneWire instance to communicate with the OneWire device
OneWire oneWire(ONE_WIRE_BUS);
// Pass the oneWire reference to the 18B20 sensor
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer = {0x28, 0xB2, 0x27, 0x46, 0x04, 0x00, 0x00, 0xA9};
// Screensaver ================================================================
int nSinceStart = 0; // to keep track of cycles since start
byte regel = 0; // to avoid burnt screen
byte kolom = 0; // to avoid burnt screen
float CMax = 0.00; // Maximum measured temperature
float CMin = 100.00; // Minimum measured temperature
// DS1307 Clock ================================================================
RTC_DS1307 RTC;
String weekdays [] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
unsigned long relayAan, relayUitKort, relayUitLang;
// 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;
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) );
}
-
- Posts: 3
- Joined: Tue Dec 11, 2012 6:47 pm
Re: Use a listener to detect clap from sound module?
Hope this helps, thanks in advance!
- adafruit_support_bill
- Posts: 88093
- Joined: Sat Feb 07, 2009 10:11 am
Re: Use a listener to detect clap from sound module?
So you are reading the 'clap' on a digital pin. But with all the other stuff going on in your loop, you are not reading that pin frequently enough to reliably catch the clap.
I think your best bet would be to connect your mic to one of the interrupt pins and use an interrupt to catch the clap: http://arduino.cc/en/Reference/AttachInterrupt
I think your best bet would be to connect your mic to one of the interrupt pins and use an interrupt to catch the clap: http://arduino.cc/en/Reference/AttachInterrupt
Please be positive and constructive with your questions and comments.