A clapper switch! (...once again)

twain28:
Hello there everybody!

I's my first official time here, but I've been lurking around the forums for almost a year, while trying to build up my custom arduino project...why, it's a clapper switch, of course! :slight_smile:

Let's see...I started with a "simple" SINGLE CLAP ON/OFF LED LIGHT using the code I found on this Arduinonano-Clap-Switch instructables page/.
Code read as follows:

int micpin=10;

int ledpin=12;

void setup() {

pinMode (micpin, INPUT);

pinMode (ledpin, OUTPUT);

}

void loop() {

if (digitalRead (micpin)==HIGH) {

if (digitalRead(ledpin)==LOW) {

digitalWrite (ledpin, HIGH);

} else {

digitalWrite(ledpin,LOW);

}

delay(1000);

}

}




This worked fine, so I went for the next step, turning it into a DOUBLE CLAP ON/OFF LED LIGHT, mixing the original code with a bit I found on a few different pages, such as [this HighVoltageCode page](https://www.highvoltagecode.com/post/arduino-clap-switch-controlling-home-appliances-with-a-double-clap) and [this 14Core page](https://www.14core.com/making-your-own-sound-activated-switch-on-arduino/).
This way, I obtained the following code:


int InputSound = 0;
int micpin = 10;
int ledpin = 12;

long DetectSpan = 0;
long SoundDetection = 0;

boolean lightState = false;

void setup() {
  Serial.begin(9600);
  pinMode (micpin, INPUT);
  pinMode (ledpin, OUTPUT);
  Serial.print("Microphone up and running");
}

void loop() {

int sensorState = digitalRead(micpin);
           
    if (sensorState == 0)
    {
      if (InputSound == 0)
      {
        SoundDetection = DetectSpan = millis();
        InputSound++;
      }
      else if (InputSound > 0 && millis()-DetectSpan >= 50)
      {
        DetectSpan = millis();
        InputSound++;
      }
    }

if (millis() - SoundDetection >= 400)
    {
      if (InputSound == 2)
      {
        if (!lightState)
        {
          lightState = true;
          digitalWrite(ledpin, HIGH);
          Serial.print("Let there be light!");
        }
        else if (lightState)
        { /* altrimenti */
          lightState = false;
          digitalWrite(ledpin, LOW);
          Serial.print("Lights of, please!");
        }
      }
      InputSound = 0;
    }
}




This worked as well...so I went for the big jump and changed the code to use a relay switch to POWER ON/OFF an extension cord. I adapted the code above mixing it with what I found on [this Create.Arduino page](https://create.arduino.cc/projecthub/mtashiro/the-clapper-09dc8a) and again on [the HighVoltage page](https://www.highvoltagecode.com/post/arduino-clap-switch-controlling-home-appliances-with-a-double-clap).
Here's the final code:


int InputSound = 0;
int micpin = 10;
int relaypin = 12;
long DetectSpan = 0;
long SoundDetection = 0;

boolean relayState = false;

void setup() {
  Serial.begin(9600);
  pinMode (micpin, INPUT);
  pinMode (relaypin, OUTPUT);
  Serial.print("Microphone up and running");
}

void loop() {

int sensorState = digitalRead(micpin);
           
    if (sensorState == 0)
    {
      if (InputSound == 0)
      {
        SoundDetection = DetectSpan = millis();
        InputSound++;
      }
      else if (InputSound > 0 && millis()-DetectSpan >= 50)
      {
        DetectSpan = millis();
        InputSound++;
      }
    }

if (millis() - SoundDetection >= 400)
    {
      if (InputSound == 2)
      {
        if (!relayState)
        {
          relayState = true;
          digitalWrite(relaypin, HIGH);
          Serial.print("Let there be light!");
        }
        else if (relayState)
        {
          relayState = false;
          digitalWrite(relaypin, LOW);
          Serial.print("Lights off, please!");
        }
      }
      InputSound = 0;
    }
}




Now...this should work...and yet it does not!
I chopped the power extension, then plugged the + cable (blue) to the C (in) and another piece of blue cable from the NO (out) nack to the plug...yet this does not work!
I tried plugging in the arduino nano to its usb cable, and all of a sudden, the board turned on and seemed to work (nano, microphone and even relay turned on and I could CLAP CLAP ON/OFF the relay),but then, when I turn the relay itself off by double clapping, the electricity still flowed to the plug.

What am I boing wrong? Please help! 
Tomorrow I'll provide an image of my setup, to better undersand it.

Many thanks to everyone who can provide a solution.

is it a single relay by itself or a relay module board?