Window controlled by remote and ldr

I'm controlling my window blinds using a remote control and ldr. I have the code below. I have a case called Half light. Meaning when i click the button 3 on the remote control the motor turns to half close the blind and allow a light level of analogRead 80 to pass through. It works but it leaves the case and enters the loop. I want it to stay in the case and constantly adjust the blind to different light levels while waiting for another button (eg.Fully close blind) to be pressed. Does it sound too complicated?

/* AUTOMATED MODE FOR WINDOW BLINDS
----------------------------------------------------


/*-----( Import needed libraries )-----*/

#include "IRremote.h"

/*-----( Declare Constants )-----*/
int receiver = 5; // pin 1 of IR receiver to Arduino digital pin 11
int lightPin = A1;  //define a pin for Photo resistor
int Direction = 6; //left  
int Enable = 7; //right
int counter = 0;
int fo = 8;
int fc = 9;
int natural = 10;
int halflight = 11;
int chose = 0;


/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'
/*-----( Declare Variables )-----*/


void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);
  Serial.println("IR Receiver Raw Data + Button Decode Test");
  irrecv.enableIRIn(); // Start the receiver
  pinMode(6, OUTPUT); //set direction pin as output
  pinMode(7, OUTPUT); //set enable pin as output
  

}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  Serial.println(analogRead(lightPin));                 //Write the value of the photoresistor to the serial monitor.
  if (irrecv.decode(&results)) // have we received an IR signal?

  {
//    Serial.println(results.value, HEX);  UN Comment to see raw values
    
    translateIR(); 
    irrecv.resume(); // receive the next value
  }  
}/* --(end main loop )-- */

/*-----( Declare User-written Functions )-----*/
void translateIR() // takes action based on IR code received

// describing Car MP3 IR codes 

{

  switch(results.value)

  {

  case 0x1FED02F:  
    
    Serial.println(" FULLY OPEN.1"); 
    Serial.print('\n'); 
    
      while(counter<=10){
        counter++;
        digitalWrite(Enable, 1);
        digitalWrite(Direction, 1);    //turn left
        delay(300);
        digitalWrite(Enable, 0);
         }
    break;

  case 0x1FED827:  
    Serial.println(" FULLY CLOSED.2"); 
    Serial.print('\n'); 
      while(counter>0){
        counter--;
        digitalWrite(Enable, 1);
        digitalWrite(Direction, 0);    //turn right
        delay(300);
        digitalWrite(Enable, 0);
      }
    break;
    
 

  case 0x1FEC03F:  
    Serial.println(" NATURAL.4"); 
    Serial.print('\n'); 
        delay(10);
 
        while(analogRead(lightPin)>120){           //bright mode
   
            Serial.println(analogRead(lightPin));
            if(counter>=5){
            digitalWrite(Enable,0);  
            delay(300);}
 
            else {
          
            Serial.println(analogRead(lightPin));
            digitalWrite(Enable, 1);
            digitalWrite(Direction, 0);    //turn left
            delay(300);
            digitalWrite(Enable, 0);
            counter=counter+1;
            }
        }
        
        while(analogRead(lightPin)<80){
          
          Serial.println(analogRead(lightPin));
          if(counter<=-5){
            digitalWrite(Enable,0);
            delay(300);}
          
           else{
            Serial.println(analogRead(lightPin));   
            digitalWrite(Enable, 1);
            digitalWrite(Direction, 1);    //turn right
            delay(300);
            digitalWrite(Enable, 0);
            counter=counter-1;
           }
      }
    break;
  
  case 0x1FEF807:  
    Serial.println(" HALFL LIGHT.3"); 
    Serial.print('\n'); 
    Serial.println(analogRead(lightPin));
    delay(10);
        while(analogRead(lightPin)>120){           //bright mode
            if(counter<=0){
            digitalWrite(Enable,0);  
            delay(300);}
        
            else{
            digitalWrite(Enable, 1);
            digitalWrite(Direction, 0);    //turn right
            delay(300);
            digitalWrite(Enable, 0);
            counter--;
          }
        }
        while(analogRead(lightPin)<80){          //dark mode
          if(counter>=10){
            digitalWrite(Enable,0);
            delay(300);}
          
            
           else{
          digitalWrite(Enable, 1);
            digitalWrite(Direction, 1);    //turn left
            delay(300);
            digitalWrite(Enable, 0);
          counter++;
           }
           }
          
    break;

  case 0xFFC23D:  
    Serial.println(" PLAY/PAUSE     "); 
    break;

  case 0xFFE01F:  
    Serial.println(" VOL-           "); 
    break;

  case 0xFFA857:  
    Serial.println(" VOL+           "); 
    break;

  case 0xFF906F:  
    Serial.println(" EQ             "); 
    break;

  case 0xFF6897:  
    Serial.println(" 0              "); 
    break;

  case 0xFF9867:  
    Serial.println(" 100+           "); 
    break;

  case 0xFFB04F:  
    Serial.println(" 200+           "); 
    break;

  case 0xFF30CF:  
    Serial.println(" 1              "); 
    break;

  case 0xFF18E7:  
    Serial.println(" 2              "); 
    break;

  case 0xFF7A85:  
    Serial.println(" 3              "); 
    break;

  case 0xFF10EF:  
    Serial.println(" 4              "); 
    break;

  case 0xFF38C7:  
    Serial.println(" 5              "); 
    break;

  case 0xFF5AA5:  
    Serial.println(" 6              "); 
    break;

  case 0xFF42BD:  
    Serial.println(" 7              "); 
    break;

  case 0xFF4AB5:  
    Serial.println(" 8              "); 
    break;

  case 0xFF52AD:  
    Serial.println(" 9              "); 
    break;

  default: 
    Serial.println(" other button   ");

  }
 
   
   
    
    
  delay(500);


} //END translateIR



/* ( THE END ) */

Does it sound too complicated?

No, but you need to take a slightly different approach. You need to separate reading the IR pin from dealing with what you read from the pin.

int blindState = 0;

void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  Serial.println(analogRead(lightPin));                 //Write the value of the photoresistor to the serial monitor.
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    switch(results.value)
    {
        // cases here for open, close, and depending on light
        // set blindState to 1, 2, or 3
    }
    irrecv.resume(); // receive the next value
  }

  setBlindPosition(state);
}

Then, in setBlindPosition(), you open the blinds, close the blinds, or set the position based on the light level, depending on the value in blindState. There is, obviously, no need to read the light sensor in loop(). That should be done in setBlindPosition(), only in the case of depending on light level.

int blindState = 0;

void loop() /----( LOOP: RUNS CONSTANTLY )----/
{
Serial.println(analogRead(lightPin)); //Write the value of the photoresistor to the serial monitor.
if (irrecv.decode(&results)) // have we received an IR signal?
{
switch(results.value)
{
// cases here for open, close, and depending on light
// set blindState to 1, 2, or 3
}
irrecv.resume(); // receive the next value
}

setBlindPosition(state);
}


HI!! Can I know the full codes? I would like to use it as reference for my study :slight_smile: Please reply. Thanks!

Hello there, I know this post is a little bit old but I am still hoping fo a reply. Can I request for a copy of the Full Codes? I just need it for my study. Please please reply.. Thank you very much :slight_smile: :slight_smile: