Pages: [1]   Go Down
Author Topic: Noob question for "Clapper" circuit  (Read 596 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok.. So I'm pretty new to programming Arduino and have done several beginer projects. I've made a very simple "Clapper" project for my bedroom but it by far had it's issues (real annoying when the lights turn on in the middle of the night for no reason smiley-eek). I really want the clapper circuit as a permenant fixture and researching this I found another's code that he says has worked pretty flawless for him. Problem is it's way over my head to adapt it to the hardware I'm using so any help would be greatly appriciated. So

I'm using an Arduino Uno (will switch over to nano once I get it working),
a basic single relay shield
and this sound sensor shield (electret microphone)
http://www.ebay.com/itm/Sound-Sensor-Module-for-Sensor-Shield-For-ATmega328-2560-LM393-3v-24v-/300768346281?pt=LH_DefaultDomain_0&hash=item460730c0a9.

Note that the mic has a pot and both digital and analog outputs and a sensor LED that lights when I clap (ignoring the tv background noise) when the pots been adjusted to values of about 420 when quiet. Guessing this is the ideal adjustment for it and need to change the code for these threshold..??

And heres the Code:
Code:
// Arduino Clapper - by WilliamK @ Wusik.com & Beat707.com - November 06 2011 - V 1.4.0 //
// On V1.4.0, a volume checker is placed which checks for background noise x sound "pops".

#define inPin A1               // Pin input for the audio sensor
#define outPin 0               // Output pin to active external device (use relay for 110/220V)
#define touchPin PINB4         // Must be in PINB# format (digital pin 8 ~ 13 = PINB0 ~ PINB5)

// ------------------------------------------------------------------------------------------------------------------------ //
uint8_t isOn = false;
uint8_t prevIsOn = false;
uint8_t prevButton = 0;
uint8_t curButton = 0;
uint8_t wasButtonLow = 1;

unsigned long sensorValue = 38;
unsigned char counter = 0;
unsigned long backgroundNoise = 0;
unsigned char claps = 2;
unsigned long milliS = millis();

// ------------------------------------------------------------------------------------------------------------------------ //
void setup() 
{
  pinMode(outPin, OUTPUT);
  pinMode(inPin, INPUT);
 
  prevButton = getcap(1<<PINB0);
}

// ------------------------------------------------------------------------------------------------------------------------ //
void loop() 

  if (isOn)
  {
    sensorValue += analogRead(inPin);
    counter++;
    if (counter > 125)
    {
      if (backgroundNoise > 0 && sensorValue > (backgroundNoise+1400))
      {
        claps++;
        if (claps == 2) milliS = millis() + 1000; else milliS = millis() + 4000;
      }
      if (sensorValue > backgroundNoise) backgroundNoise = sensorValue;
        else if (backgroundNoise > sensorValue && backgroundNoise >= 400) backgroundNoise -= 400;
      counter = 0;
      sensorValue = 0;
    }
    delayMicroseconds(250);
 
    if (milliS > 0 && millis() > milliS)
    {
      if (claps == 2)
      {
        isOn = !isOn;
        checkNewOn();
      }
     
      claps = milliS = 0;
    }
  }
 
  checkButton();
}

// ------------------------------------------------------------------------------------------------------------------------ //
void checkNewOn()
{
  if (isOn != prevIsOn)
  {
    digitalWrite(outPin, isOn);
    prevIsOn = isOn;
    sensorValue = backgroundNoise = claps = counter = milliS = 0;
    delay(2000);
  }
}

// ------------------------------------------------------------------------------------------------------------------------ //
void checkButton()
{
  curButton = getcap(1<<touchPin);
 
  if (curButton != prevButton)
  {
    prevButton = curButton;
    if (wasButtonLow == 1 && curButton > 1)
    {
      isOn = !isOn;
      wasButtonLow = 0;
      checkNewOn();
      delay(500);
    }
    if (curButton <= 1) wasButtonLow = 1;
  }
 
  checkNewOn();
}

// ------------------------------------------------------------------------------------------------------------------------ //
char getcap(char pin)
{
  char i = 0;
  DDRB &= ~pin;          // input
  PORTB |= pin;          // pullup on
  for(i = 0; i < 2; i++)
  {
    if( (PINB & pin) ) break;
  }
  PORTB &= ~pin;         // low level
  DDRB |= pin;           // discharge
  return i;
}
Good Luck!! smiley-grin
Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2207
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
real annoying when the lights turn on in the middle of the night for no reason
Snoring  smiley-wink



Logged

Handle every stressful situation like a dog. If you can't eat it or hump it. Piss on it and walk away.

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So nobody knows much about this code either, huh? I've spent quite a bit of time (days) researching this.. The touch sensor I can get to toggle the LED but can't get it to register any sound at all.. Please please help me understand some of this..
Logged

Pages: [1]   Go Up
Jump to: