using a photo cell

i'm usine a photo cell to turn a relay on for 1 second the value i'm using is 100 so when the value gets to 100 the relay turns on for a second. The problem is that it fluctuates around 100 for a couple minutes so that relay goes on and off how do i prevent that in that sketch

turn it on when it get to 100

turn it off when it drops to a number well below 100, like 90

creating a 'dead band'

The problem is that it fluctuates

It? What is it?

how do i prevent that in that sketch

Add a servo with a big hammer attached. Position the servo so that the hammer whacks the photo cell. Stop any nonsense it's putting out permanently.

Or post your code, damn it.

OK then I like the hammer idea
Im using the relay to control the dead bolt in my front door to lock it at dusk. Using serial monitor I chose the value of 100. When I ran my sketch I could hear my dead bolt trying to lock every few seconds for a few min. The photo cells value goes up and down for a few min like 100 then 99 then 101 then 100 then 98. So every time it gets to 100 the relay #2 operates
There are other things in my code but Im only having trouble with the photocell

//  Push Buttons for Relay Number 1 -----> Pin 10
//  Push Buttons for Relay Number 3 -----> Pin 11
//  Push Buttons for Relay Number 4 -----> Pin 12

//  Photo Cell ----> Pin A0

//Relay Number 1 -----> Pin 6
//Relay Number 2 -----> Pin 7
//Relay Number 3 -----> Pin 8
//Relay Number 4 -----> Pin 9

//RFID Connection:
//ENABLE -----> Pin 2
//RFID TX ---- Pin RX
//VCC ----> 5v
//GND -----> Arduino GND



//Constants
#define NumberOfIDs  20  //This defines the amount of IDs in the access list
#define ON_TIME 1000         //time in milliseconds the relay #1 stays energized
#define ON_TIME_2 1000         //time in milliseconds the relay #2 stays energized
#define ON_TIME_3 1000         //time in milliseconds the relay #3 stays energized
#define ON_TIME_4 1000         //time in milliseconds the relay #4 stays energized
#define Relay2_operateValue 100

//External Equipment
#define ENABLE 2               //Pin connected to the Enable pin of RFID
#define RELAY1 6                  //Pin that is connected to the relay #1
#define RELAY2 7                 //Pin that is connected to the relay #2
#define RELAY3 8                 //Pin that is connected to the relay #3
#define RELAY4 9                  //Pin that is connected to the relay #4
#define RELAY1_PUSH_BUTTON 10
#define RELAY3_PUSH_BUTTON 11
#define RELAY4_PUSH_BUTTON 12

//Analog Input
#define PHOTO_CELL A0


int     val = 0; 
int     bytesread = 0;
String  IDCode;
String  accessList[NumberOfIDs]={"120075A418","12007389DA","690008BB23","6900076031","11003163FE","430051E429","0F032BBBD8","0F032BBA53"};  // !!!!  HERE YOU WILL ADD THE IDs !!!!! 
char    code[10];
boolean IDFound,photoCellstate= false;
int button1State,button3State,button4State =0;         // variable for reading the pushbutton status
int photoCellValue=1023;

void setup() {
  
pinMode(ENABLE,OUTPUT);
pinMode(RELAY1,OUTPUT);
pinMode(RELAY2,OUTPUT);
pinMode(RELAY3,OUTPUT);
pinMode(RELAY4,OUTPUT);
pinMode(RELAY1_PUSH_BUTTON, INPUT);
pinMode(RELAY3_PUSH_BUTTON, INPUT);
pinMode(RELAY4_PUSH_BUTTON, INPUT);

Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps 

digitalWrite(ENABLE, LOW);                  // LOW Activates the RFID reader, HIGH deactivates it
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY2,LOW);
digitalWrite(RELAY3,LOW);
digitalWrite(RELAY4,LOW);

}  


 void loop() { 
  digitalWrite(ENABLE, LOW);                  // LOW Activates the RFID reader, HIGH deactivates it

   // read the state of the pushbutton value:
  button1State = digitalRead(RELAY1_PUSH_BUTTON);
  button3State = digitalRead(RELAY3_PUSH_BUTTON);
  button4State = digitalRead(RELAY4_PUSH_BUTTON);
  photoCellValue = analogRead(PHOTO_CELL);
  
   /**** EXTRACTION OF DATA FROM CARD ****/
  if(Serial.available() > 0) {            // if data available from reader 
    if((val = Serial.read()) == 10) {   // check for header [Header of data = 10]
      bytesread = 0; 
      
      while(bytesread<10) {              // read 10 digit code 
        if( Serial.available() > 0) { 
          val = Serial.read(); 
          if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading 
            break;                             // stop reading 
          } 
          code[bytesread] = val;      // add the digit
          bytesread++;                   // ready to read next digit
        } 
      } 
      if(bytesread == 10) {                  // if 10 digit read is complete
       IDCode = code;
       IDCode = IDCode.substring(0,10);
      }
      
      bytesread = 0; 
      digitalWrite(ENABLE, HIGH);                  // deactivate the RFID reader for a moment so it will not flood

             
       /*** INTERPRETATION OF DATA ***/
       //At this point, data was read, and there is a IDcode in the IDCode variable     
       
          for(int i = 0;i<NumberOfIDs;i++)  //check entered ID against saved IDs
          {
             if(IDCode.equals(accessList[i])){
                IDFound = true;}                  //if the ID is in the access list, IDFound = true;
          }
          if(IDFound)
          {
          // energize Relay 1:    
              digitalWrite(RELAY1,HIGH);    //energize the relay 1
              delay(ON_TIME);            //Relay stays energized for ON_TIME milliseconds
              digitalWrite(RELAY1,LOW); //Immediately after delay, de-energize relay.
              IDFound = false;
              
          }
          else{
            digitalWrite(RELAY1,LOW);
          }
 
    }    //end of data reading
    
      delay(4000);                                 // wait for a bit 

  }


 
   // check if the push buttons are pressed.
  // if it is, the button State is HIGH:
  
   /***Relay Number 1 control ***/
  if (button1State == HIGH) {     
    // energize Relay 1:    
      digitalWrite(RELAY1,HIGH);//energize the relay 1
  delay(ON_TIME);            //Relay stays energized for ON_TIME milliseconds
  digitalWrite(RELAY1,LOW); //Immediately after delay, de-energize relay.
  } 
  else {
    // de-energize Relay 1:
    digitalWrite(RELAY1, LOW); 
  }

   /***Relay Number 2 control ***/
  if ((photoCellValue < Relay2_operateValue) && (photoCellstate==false)) {     
    // energize Relay 2:    
  digitalWrite(RELAY2,HIGH);//energize the relay 2
  delay(ON_TIME_2);            //Relay 2 stays energized for ON_TIME_2 milliseconds
  digitalWrite(RELAY2,LOW); //Immediately after delay, de-energize relay. 
  photoCellstate=true; 
  } 
    if (photoCellValue > 400) {     
    // de-energize Relay 2:    
  digitalWrite(RELAY2,LOW); //Immediately after delay, de-energize relay. 
  photoCellstate=false; 
  } 

  
   /***Relay Number 3 control ***/
  if (button3State == HIGH) {     
    // energize Relay 3:    
  digitalWrite(RELAY3,HIGH);//energize the relay 3
  delay(ON_TIME_3);            //Relay 3 stays energized for ON_TIME_3 milliseconds
  digitalWrite(RELAY3,LOW); //Immediately after delay, de-energize relay.  
  } 
  else {
    // de-energize Relay 3:
    digitalWrite(RELAY3, LOW); 
  }
  
  /***Relay Number 4 control ***/
  if (button4State == HIGH) {     
    // energize Relay 4:    
  digitalWrite(RELAY4,HIGH);//energize the relay 4
  delay(ON_TIME_4);            //Relay 4 stays energized for ON_TIME_4 milliseconds
  digitalWrite(RELAY4,LOW); //Immediately after delay, de-energize relay. 
  } 
  else {
    // de-energize Relay 4:
    digitalWrite(RELAY4, LOW); 
  }
 
}

Google "hysteresis"

As well as code to turn the relay on for just 1 second you need code that doesn't read the photocell again for a suitable time - perhaps 30 minutes.

...R

I have a delay at the end of the read should I increase that for 30 min

Trickyrick:
I have a delay at the end of the read should I increase that for 30 min

It's hard to follow your code. I don't see where the photocell is read. (A function called readPhotocell() would be nice and obvious).

I just meant there is no point reading the photocell if the lock is shut and you don't want to open it again immediately. You will have to figure out a suitable time interval yourself.

...R