Code only works correctly the first time?

Edited this post to get the code properly posted. It compiles for me and loads to the board. It runs, but not the right way every time.

Taking on a pretty extensive Halloween project and I'm getting close to the proper coding, however, it only seems to work properly the first time. If I power off the board entirely and re-energize it, the audio loops over and over again until the light sensor reads darkness. Not sure why this is happening...

The program should run where if the light sensor senses darkness AND the proximity sensor senses movement, the audio file plays and the lights come on. Any help here on this would be greatly appreciated!

//Assign pins
     int sensorPin = A0;     //light sensor
     int pirSENSOR = 2;      //SENSOR to detect motion, set to maximum sensitivity and range
     int previousBUTTON = 4; //button on MP3 module
     int playBUTTON = 3;     //button on MP3 module
    int legLED = 5;         //leg red lights

    int ldrStatus;


    void setup() {
    pinMode(sensorPin,INPUT);
    pinMode(pirSENSOR,INPUT);
    pinMode(previousBUTTON,OUTPUT);
    pinMode(playBUTTON,OUTPUT);
    pinMode(legLED,OUTPUT);

    digitalWrite(previousBUTTON,HIGH); //set initial state, in my case Relay HIGH is OFF/OPEN
    digitalWrite(playBUTTON,HIGH);     //set initial state, in my case Relay HIGH is OFF/OPEN
    digitalWrite(legLED,HIGH);       //set initial state, in my case Relay HIGH is OFF/OPEN

    }

     void loop() {
     ldrStatus = analogRead(sensorPin);
    
    if ((ldrStatus <= 400) && (digitalRead(pirSENSOR) == HIGH))  {   //If its dark outside and Motion is detected
 
       //Play Sound
        digitalWrite(previousBUTTON,LOW);      //PRESS previous button which plays sound from begining
        delay(100);
        digitalWrite(previousBUTTON,HIGH);     //release previous button
        delay(100);
 
    //Leg lighting sequence
       digitalWrite(legLED,LOW);
       delay(8000);
       digitalWrite(legLED,HIGH);
       delay(100);
    
    //Stop Sound   
      digitalWrite(playBUTTON,LOW);          //pause/stop the sound playback
      delay(100);
      digitalWrite(playBUTTON,HIGH);         //release play button
      delay(60000);                          //wait 1minute before allowing to reactivate
  
    }
     else{
  
    }
    }

Hi, @xqizt
Welcome to the forum.

You have some very long delays in your code.

delay(60000)  // 60 second delay

and others of 8 seconds.

You should put some Serial.print statements in to show the value of your variables.

A delay statement, STOPS the code for that time, it does nothing, so if you are expecting buttons or outputs to change in the delay time, IT WON'T.

Look at the "Blink without delay" example in the IDE.

Have you written code to prove each of your inputs is working correctly?

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Doesn't compile for me.

Thanks for the welcome. I'm really enjoying all the info here and the challenge of making cooler stuff!

The 8 second delay in the leg lighting sequence is so they stay on while the audio file finished playing. Is there a better way to code this? Honestly, I "borrowed" code from other projects I found here and made modifications to fit my set up.

The one minute delay is so that there is some "reset" time before the decoration can be activated again. Again, I borrowed code from other projects I found here.

The code for the lighting sequence (led strip lights) and the audio should both only be allowed to process if the photosensor "sees" darkness, but it doesnt quite work that way.

Sorry, there were some errors with the way I posted the code. It should compile now.

Also, yes, I first wrote code that only ran the audio and lighting effects when the PIR sensor was tripped. That worked flawlessly. I started to experience problems when I attempted to add in the photosensor to the mix.

Basically, I only want this decoration to run at night when someone trips the PIR sensor.

Hi,
You now need to add Serial.println statements in your code to pin point problem areas.
At the moment lowering your delay figures will help you expedite the process.

Have you checked how your LDR input works. It may be working in the opposite direction to what you think.

Can you please post a circuit diagram of your project?
Just an image of a hand drawn schematic will be fine.

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

Hi,
I have added Serial.prints to your code and edited some lines.
Open the IDE monitor with 9600baud.

The code does work just I think your LDR configuration in the circuit is back to front.

//Assign pins
int sensorPin = A0;     //light sensor
int pirSENSORPin = 2;      //SENSOR to detect motion, set to maximum sensitivity and range
int previousBUTTON = 4; //button on MP3 module
int playBUTTON = 3;     //button on MP3 module
int legLED = 5;         //leg red lights
bool PIRsensorStatus;
int ldrLevel;


void setup()
{
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  pinMode(pirSENSORPin, INPUT);
  pinMode(previousBUTTON, OUTPUT);
  pinMode(playBUTTON, OUTPUT);
  pinMode(legLED, OUTPUT);
  digitalWrite(previousBUTTON, HIGH); //set initial state, in my case Relay HIGH is OFF/OPEN
  digitalWrite(playBUTTON, HIGH);    //set initial state, in my case Relay HIGH is OFF/OPEN
  digitalWrite(legLED, HIGH);      //set initial state, in my case Relay HIGH is OFF/OPEN
}

void loop()
{
  ldrLevel = analogRead(sensorPin);
  PIRsensorStatus = digitalRead(pirSENSORPin);
  Serial.print("PIR Status = ");
  Serial.print(PIRsensorStatus);
  Serial.print("  LDR Level = ");
  Serial.println(ldrLevel);
  if ((ldrLevel <= 400) && (PIRsensorStatus == HIGH))     //If its dark outside and Motion is detected
  {
    //Play Sound
    Serial.println("Playing Sound");
    digitalWrite(previousBUTTON, LOW);     //PRESS previous button which plays sound from begining
    delay(100);
    digitalWrite(previousBUTTON, HIGH);    //release previous button
    delay(100);
    //Leg lighting sequence
    Serial.println("Start LED Sequence");
    digitalWrite(legLED, LOW);
    delay(8000);
    digitalWrite(legLED, HIGH);
    delay(100);
    //Stop Sound
    digitalWrite(playBUTTON, LOW);         //pause/stop the sound playback
    delay(100);
    digitalWrite(playBUTTON, HIGH);        //release play button
    Serial.println("Playing Released");
    //    delay(500);
    delay(60000);                          //wait 1minute before allowing to reactivate
  }
}

You do not need the "else" part of the if statement if you have nothing in it.

Tom... :smiley: :+1: :coffee: :australia:

Tom, I REALLY appreciate your input here. Your code corrections helped!

I am powering the Adruino board through a 12v power supply because this will be an outdoor decoration. The issue I run into now is if I unplug power and then restore it (simulating a power outage or accidental unplugging), the audio file plays constantly until the LDR senses darkness and the motion sensor is tripped. Then it falls back into proper operation.

I assumed that if power was lost, upon power being restored, the board would go back to the very beginning and run everything from the start of the code.

I'll also get you a schematic. Stand by!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.