 int irBase=20;
 int irMax=230;
 
+
 int t1=0;
 int t2=0;
 
-int state= 0; // 0= off, 1 = on
+int state=0; // 0= off, 1 = on
+int readyForChange=1;
 
 
 int pwmOut=9;
 
-
+unsigned long lastTime;
+int timeDelta=50;
 
 
 void switchState(){
-  if(state == 0){
-    state = 1;
-  }else{
-    state = 0;
+  if(readyForChange==1){
+    if(state == 0 ){
+      state = 1;
+    }else{
+      state = 0;
+    }
+    readyForChange=0;
   }
+
   
 }
 


void loop() { 

  
  readIR();


  if( irSensorVal > (t1 + irDelta) ){
    switchState();
  }
  
  if(state == 1){
    analogWrite(pwmOut,200);  // analogRead values go from 0 to 1023, analogWrite values from 0 to 255  


    
  Serial.println("");

}


