Activated Interrupt by using remote control

i have sony remote control and viksay tsop for ir receiving..
what i want to do is to activated the interrupt when press the button on remote..
here the flow chart

how to make it happen?

how to make it happen?

magic

Have you even tried?

We don't simply write code for people.

here i make one..but it not working, the button pressed on remote cannot be read..why? :fearful:

#define NUMVALUES 4
int pin = 13;
volatile int state = LOW;

void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(1, remote, CHANGE);
}

void loop()
{
  digitalWrite(pin, state);
 
       delay(500);
       int values [NUMVALUES];
       int maxVal = -1;
  
  // READ THE VALUES
  for (int i=0; i< NUMVALUES; i++)
    {
    int val = analogRead(i);
    values[i] = val;
    if (val > maxVal)
      maxVal = val;
    
    }  // end of taking readings

  // SET ALL MAX PINS TO HIGH
  for (int i = 0; i < NUMVALUES; i++)
    digitalWrite(9+i, values[i] == maxVal); 
  delay(1000);
 }


void remote()
{
  state = !state;
     int coderemote;
    coderemote = getSircs(); //read the button remote
  Serial.print("Heard :");
  Serial.println( coderemote );
  if ( coderemote == 149){
     Serial.println("RemoteON:");
     coderemote = getSircs();
     if ( coderemote == 147){
        Serial.println("Remote: Open");
     }
     if (coderemote == 146){
        Serial.println("Remote: OFF");
}
}
}
//decode value from ir reciever
int getSircs() {
  int duration;
  int irCode;
  int mask;

  // wait for start bit
  do {
    duration = pulseIn(2, LOW);
  } while (duration < 2160 || duration > 2640);

  // get 12-bit SIRCS code
  irCode = 0;							  // clear ir code
  mask = 1;							    // set mask to bit 0
  for (int idx = 0; idx < 12; idx++) {			 // get all 12 bits
    duration = pulseIn(2, LOW);			    // measure the bit pulse
    if (duration >= 1080)					  // 1 bit?
	irCode |= mask;						// yes, update ir code
    mask <<= 1;							// shift mask to next bit
  }
  return irCode;
}

Never (unless you really know what you're up to) put serial comms in an interrupt service routine.

Why does it need to be an interrupt?
Why not poll?

i need to check if remote key is press. it stop doing the program in loop() function. and when the button is not press, it continue to do the loop() function..

Why not poll?

what poll?

Polling means looking at the resource at regular intervals, like every time through "loop".
Using an interrupt as a pause function is not the way to go.

Why don't you tell us what you want to do, not how you think you should do it?

what i want to do is, it my arduino make to function,
1st detect the LDR //auto
2nd user control the position on which LDR by remote control..( it mean it light the led on which button on remote is press.) //manual remote

so i want the remote function work when only the button is pressed. if not, it cont do the auto

when i using the interrupt, why i never been back into the loop function..it keep running in the interrupt?

#define NUMVALUES 4
int pin = 13;
volatile int state = 0;
int isr=0;
void setup()
{
  Serial.begin(9600);
  pinMode(pin, OUTPUT);
  Serial.println("Begin");
  attachInterrupt(0, inter, RISING);

}

void remote(int r){
 

  digitalWrite(pin, HIGH);
 
  Serial.println("Remote");
    
   delay(100); //read the button remote
  Serial.print("Heard :");
  Serial.println( r );
  if ( r == 149){
     Serial.println("RemoteMOde:");

  }
     if ( r == 147){
        Serial.println("Remote: Open");
     }
     if (r == 146){
        Serial.println("Remote: OFF");
     }
     

}

     
void loop()
{
  Serial.println("doing the auto");
  delay(100);
  while (isr <10){
    if (state ==0){
    break;
    }
    int values [NUMVALUES];
       int maxVal = -1;
  isr++;
  // READ THE VALUES
  for (int i=0; i< NUMVALUES; i++)
    {
    int val = analogRead(i);
    values[i] = val;
    if (val > maxVal)
      maxVal = val;
    
    }  // end of taking readings

  // SET ALL MAX PINS TO HIGH
  for (int i = 0; i < NUMVALUES; i++)
    digitalWrite(9+i, values[i] == maxVal); 
  delay(100);
  
}
state=0;
}
 
 

     

//interupt 

void inter ()
{
   state=1;
  //get the remote decode
    int duration;
  int irCode;
  int mask;

  // wait for start bit
  do {
    duration = pulseIn(2, LOW);
  } while (duration < 2160 || duration > 2640);

  // get 12-bit SIRCS code
  irCode = 0;							  // clear ir code
  mask = 1;							    // set mask to bit 0
  for (int idx = 0; idx < 12; idx++) {			 // get all 12 bits
    duration = pulseIn(2, LOW);			    // measure the bit pulse
    if (duration >= 1080)					  // 1 bit?
	irCode |= mask;						// yes, update ir code
    mask <<= 1;							// shift mask to next bit
  }
  int r= irCode; //save the code that get from remote
  
  remote(r);
  while (r == 151){
     //back in the loop(?)
        digitalWrite(pin, LOW);
       digitalWrite(2,LOW); 
       r--;
     // return;  //exist interrupt back to loop()
      }
   
 
  
}
//decode value from ir reciever

The idea of an interrupt is it is something to which the processor must respond very quickly.
The interrupt service routine should be very short, so as to allow the processor to get on with what it is normally doing.
Typically, a processor examines the state of the interrupts every single instruction cycle, and calls the interrupt service routine.

Doing multiple pulse timings of pulses in the millisecond range and serial output in the tens of milliseconds range, in an interrupt, doesn't really fit into this scale of events.

Can you get someone to help you translate exactly what it is you are trying to do into English, then we can help better?

@AWOL

like i said, when i pressed the button 1, program do the auto (inside loop() ), then button 2, do the manual (isr).
when i pressed button go to manual, it work, but when i want to go back to auto, it not working.

@dut:
What I'm trying to communicate to you is that you're using the wrong technique (or, at best, an inappropriate technique) to try to achieve what you are trying to do.

Until you can see that, we're unlikely to progress.

@AWOL

ok , the other technique i use is not working,
so i think by using isr is worth to make i running..
what the other technique that u recommend?

If you want to find yourself in an even deeper hole, just keep digging and use the ISR.
Interrupts hold just about more techniques to bite you where it hurts most than anything else I know.
It may seem to work for weeks, and suddenly, because of something very simple that you forgot about, it will pounce.

Please, explain simply what you want to do.

Don't assume that we know anything about what you have, or what you've written here or elsewhere.
Don't assume we know how things are connected, or to what.
I want to see your terms "LDR", "auto" and whatever else explained.

ok here want i want to do..

4 pin A0 to A3 connected to LDR
4 pin digital 9 to 12 connected to LED //to show the position of each LDR;
IR rec output (tsop) from Viksay connted to digitalpin 2;

Two program involve here;
1st auto detect position of LDR

#define NUMVALUES 4;
int values [NUMVALUES];
       int maxVal = -1;

  // READ THE VALUES of LDR
  for (int i=0; i< NUMVALUES; i++)
    {
    int val = analogRead(i);
    values[i] = val;
    if (val > maxVal)
      maxVal = val;
    
    }  // end of taking readings

  // SET ALL MAX PINS TO HIGH  which have the most light detected will know from LED
  for (int i = 0; i < NUMVALUES; i++)
    digitalWrite(9+i, values[i] == maxVal); 
  delay(100);

second program is by using remote (im try to use SONY remote tv)
button remote (1 to 4)activate the LED position (so it change the current position of LDR);

//decode the button press on REMOTE
int duration;
  int irCode;
  int mask;

  // wait for start bit
  do {
    duration = pulseIn(2, LOW);
  } while (duration < 2160 || duration > 2640);

  // get 12-bit SIRCS code
  irCode = 0;							  // clear ir code
  mask = 1;							    // set mask to bit 0
  for (int idx = 0; idx < 12; idx++) {			 // get all 12 bits
    duration = pulseIn(2, LOW);			    // measure the bit pulse
    if (duration >= 1080)					  // 1 bit?
	irCode |= mask;						// yes, update ir code
    mask <<= 1;							// shift mask to next bit
  }
  key= irCode; //save the code that get from remote
  return (irCode);
  
}

i want to combine this 2 program..
1: active the program by press on key on remote (value= 149 decimal)
2: button 1 to go to auto program.
3: button 2 go to remote program that user can manual select the desired position.

the coding before this, when i press the remote to start the auto program back. it not working..

that what im trying to do

here my lattes result.

#define irPin 2 //pin recieving signal
 #define NUMVALUES 4
  int values [NUMVALUES];
int Position;
int position1;
int position2;
int position3;
int position4;
volatile byte remoteOn = 0;  // ==1, means remote has been pressed


void setposition(){
  position1= digitalRead(9);
  position2=digitalRead(10);
  position3=digitalRead(11);
  position4= digitalRead(12);
  
  if (position1==1){
   if (position2 ==0){
       if (position3 ==0){
         if (position4 ==0){
              Position = 1;           //position on LDR 1
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
  }
 if (position1==0){
   if (position2 ==1){
       if (position3 ==0){
         if (position4 ==0){
        Position = 2;              //position on LDR 2
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
    
  } 
  if (position1==0){
    if(position2 ==0){
       if (position3 ==1){
         if (position4 ==0){
            Position = 3;            //position on LDR 3
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
  }
   if (position1==0){
    if(position2 ==0){
       if (position3 ==0){
         if (position4 ==1){
            Position = 4;            //position on LDR 4
    Serial.print("Position");
    Serial.println(Position);
         }
       }
    }
  }
     
     
   if (position1 ==1){
       if (position2 ==1){
         if (position3 ==1){
          if (position4==1){
             Position = 0;     //no light detected 
    Serial.print("Position");
    Serial.println("No light detected");
          }
         }
       }
   }


   }
   


  

void setup()

{
  Serial.begin(9600);
    Serial.println("Initializing");
  for (int i = 9; i<= 12; i++)  //set pin LED as output
    pinMode(i, OUTPUT);
    pinMode(irPin, INPUT);
    digitalWrite(irPin , HIGH);
    pinMode(13, OUTPUT); //button on remote
    digitalWrite(13,HIGH); //enable Remote 
    delay(500);
    Serial.println("Done..");
    delay(500);
    
}



void loop()
{
  digitalWrite(13,LOW);
  sensor();
  
  while (remoteOn == 1){
    digitalWrite(13,HIGH);
    int key = getSircs();
      if (key == 149){
         digitalWrite(13,LOW);
           
            key= 0;
      }
      
    int led =0;
     //get key remote
    Serial.println("Remote ON");
     Serial.println(key);
    
         
    if (key == 146){
     
     Serial.println("Up");
      //off the led
    for (int i = 0; i < 4; i++)
    digitalWrite(9+i, values[i] == -1); 
    delay(40);
    //status remoteON or OFF  
      
    
        }
    if (key ==147){
        Serial.println("Down");
       //off the led
    for (int i = 0; i < 4; i++)
    digitalWrite(9+i, values[i] == -1); 
    delay(40);
    //status remoteON or OFF  
    
      
    }
    if (key == 224){
 
      remoteOn= 0;
      key =0;
      return (loop());
     
      
    }

  
  }
  
  
  
  remoteOn=0;
  attachInterrupt (0, remoting, RISING);
  
}


void sensor (){
 
  int maxVal = -1;
  
  // READ THE VALUES
  for (int i=0; i< NUMVALUES; i++)
    {
    int val = analogRead(i);
    values[i] = val;
    if (val > maxVal)
      maxVal = val;
    
    }  // end of taking readings

  // SET ALL MAX PINS TO HIGH
  for (int i = 0; i < NUMVALUES; i++)
    digitalWrite(9+i, values[i] == maxVal); 
 setposition();
 delay(1000);
}

int getSircs() { // function for decode the remote control button
  int duration;  
  int irCode;
  int mask;

  // wait for start bit
  do {
    duration = pulseIn(irPin, LOW);
  } while (duration < 2160 || duration > 2640);

  // get 12-bit SIRCS code
  irCode = 0;							  // clear ir code
  mask = 1;							    // set mask to bit 0
  for (int idx = 0; idx < 12; idx++) {			 // get all 12 bits
    duration = pulseIn(irPin, LOW);			    // measure the bit pulse
    if (duration >= 1080)					  // 1 bit?
	irCode |= mask;						// yes, update ir code
    mask <<= 1;							// shift mask to next bit
  }
 
  return irCode;
}


void remoting()  // The ISR
{
  remoteOn=1;
}

i got problem when pushing button '224'
it wont exit the while loop even i reset the isr flag..what wrong ?

how can i setting when i push button '224' it exit the while loop and continue into main loop.