multiple relays not triggering correctly

quick description of the sketch: its a clap on/off relay that turns on only when it's dark.

Cannot seem to get the relays to stay off. What happens is they are off by default then when I do the double clap to turn them on that works, after this I double clap to turn them off and then go off then immediately go on again. I can get a single relay to work perfectly but when I uncomment the rest there seems to be a problem.

I am assuming it has something to do with the delay for the clap sensor, but I am not entirely sure-- I have tried quite a few things to try to resolve it but am really lost as to why this might be happening. ideas?

int soundSensor = A2;
int relay = A0;
int relay2 = A3;
int relay3 = A4;
int relay4 = A5;
int claps = 0;
long detectionSpanInitial = 0;
long detectionSpan = 0;
boolean clapState = false;
int doubleClap = 0;


//Light sensor 
int photocellPin = A1;     // the cell and 10K pulldown are connected to a1
int photocellReading;     // the analog reading from the sensor divider
int LEDbrightness;       

//int delayValue = 100;
int photoSensor = 0;

void setup() {
  pinMode(soundSensor, INPUT);
  pinMode(relay, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  Serial.begin(9600);
}
 
void loop() {
  
  /////////////
  // photo sensor
  //////////////////// 
  photocellReading = analogRead(photocellPin);  
  photocellReading = 1023 - photocellReading;
  LEDbrightness = map(photocellReading, 0, 1023, 0, 255);
  
  if(photocellReading > 500){
      photoSensor = 0;
  }
  if(photocellReading < 500){
    photoSensor = 1;
  }
  
  /////////////
  // clap sensor
  /////////////////////
  int sensorState = digitalRead(soundSensor);
  //erial.println(sensorState);
  if (sensorState == 1)
  {
    if (claps == 0)
    {
      detectionSpanInitial = detectionSpan = millis();
      claps++;
    }
    else if (claps > 0 && millis()-detectionSpan >= 50)
    {
      detectionSpan = millis();
      claps++;
    }
  }
 
  if (millis()-detectionSpanInitial >= 500)
  {
    if (claps == 2)
    {
      if (doubleClap == 0)
        {
          doubleClap = 1;
        }
        else if (doubleClap == 1)
        {
          doubleClap = 0;
        }
    }
    claps = 0;
  }

  if(doubleClap == 1 && photoSensor == 0){ 
    digitalWrite(relay, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);
    Serial.println("off");
    
  }
  else{ 
    digitalWrite(relay, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    Serial.println("on");
  };

  
}
  if(photocellReading > 500){
      photoSensor = 0;
  }
  if(photocellReading < 500){
    photoSensor = 1;
  }

What if photocellReading is exactly 500?

  LEDbrightness = map(photocellReading, 0, 1023, 0, 255);

map() is a pretty expensive way to divide by 4.

  int sensorState = digitalRead(soundSensor);

What kind of sound sensor outputs a HIGH or LOW value?

What do your Serial.print() statements tell you is happening?

PaulS:

  LEDbrightness = map(photocellReading, 0, 1023, 0, 255);

map() is a pretty expensive way to divide by 4.

Also, shouldn't it be:
  LEDbrightness = map(photocellReading, 0, 1024, 0, 256);? :wink:
Because 1023/255 != 4

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post a picture of your project?

How are you powering the relays?

Thanks.. Tom... :slight_smile:

enum {relay=A0, relay2=A3, relay3=A4, relay4=A5};

Those are pin numbers aren't they? The names should reflect what the variable contains/is used for.

  Serial.println(analogRead(photocellPin));
  photoSensor = analogRead(photocellPin) > 185;

Print one value. Use another one. Pointless printing unused data...

  int sensorState = digitalRead(soundSensor);

Still with the mysterious digital sound sensor.

I'm done until you explain just WTF this sensor is.