Hello,
I'm trying to program my arduino pro mini such that when I press a particular button on Infrared Remote, the IR Sensor senses the infrared signal and switch on a mosfet on and off based on that signal.
I also want the pro mini to sleep when there is no signal received so as to save power.
#include <avr/sleep.h> // library for sleep
#include <avr/power.h> // library for power control
#include <IRremote.h>
#define RECV_PIN 2
#define mosfet A0
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(RECV_PIN, INPUT);
pinMode(mosfet, OUTPUT);
sleepNow();
}
void IRInterrupt() {
}
void loop() {
switchMosfet();
}
void sleepNow() {
attachInterrupt(0, IRInterrupt, LOW);
sleep_enable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_mode();
sleep_disable();
detachInterrupt(0);
}
void switchMosfet() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
if (results.value == 0xff629d) {
Serial.println("UP");
analogWrite(mosfet, 255);
}
if (results.value == 0xffa857) {
Serial.println("DOWN");
analogWrite(mosfet, 0);
}
irrecv.resume(); // Receive the next value
}
sleepNow();
}
The problem is that the arduino goes to deep sleep and wakes up when i press a button on my remote but the code goes so fast that the code in the function 'switchMosfet' does not get the time to execute and the arduino goes back to sleep. Can anyone help me please ?
I am open to suggestions also 
Make a global flag, set it at the end of your ISR, test for it before you go back to sleep (while not flag) and then clear it either before sleeping or immediately after waking.
Can you please post a sample the code to implement in the ISR? I tried the suggestion that you said but it seems that I cannot get it right 
So, if you've modified your code then we're no longer on the same page as I have no intuitive ability to deduce what your new code may look like. As your style of coding differs from the conventions that I am accustomed to, it would be irresponsible for me to offer coded solutions based on my own preferences. I'm afraid that the best I can do is offer advice in a manner similar to that already given. This is all predicated on the assumption that you may choose to post your modified code.
I do so love these posts. When my crystal ball comes back from the polishing salon, I may be in a better position.
Here is my updated codes after setting the flag to false in the Interrupt Code
#include <avr/sleep.h> // library for sleep
#include <avr/power.h> // library for power control
#include <IRremote.h>
#define RECV_PIN 2
#define mosfet A0
volatile boolean sleepArduino = true;
const int iStep = 60;
int brightness = 0;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(RECV_PIN, INPUT);
pinMode(mosfet, OUTPUT);
sleepNow();
}
void IRInterrupt() {
sleepArduino = false;
}
void loop() {
switchMosfet();
}
void sleepNow() {
Serial.println("Sleeping");
delay(1000);
attachInterrupt(0, IRInterrupt, CHANGE);
sleep_enable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_mode();
sleep_disable();
detachInterrupt(0);
}
void switchMosfet() {
if (irrecv.decode(&results)) {
int mosfetVal = analogRead(mosfet);
prd("mosfetVal", mosfetVal);
prd("brightness", brightness);
if ((results.value == 0xff629d || results.value == 0xff629) && (brightness >= 0 && brightness < 240)) { // UP Button
brightness = brightness + iStep;
}
else if ((results.value == 0xffa857 || results.value == 0xffa85) && (brightness > 60 && brightness <= 255)) { // DOWN button
brightness = brightness - iStep;
}
else if (results.value == 0xff02fd || results.value == 0xff02f) { // OK button
Serial.println(mosfetVal);
if (mosfetVal > 0)
brightness = 0;
else
brightness = 255;
}
irrecv.resume(); // Receive the next value
analogWrite(mosfet, brightness);
sleepArduino = true;
}
if (sleepArduino == true) {
sleepNow();
}
}
void prd(String str, int val) {
Serial.print(str);
Serial.print(" = ");
Serial.println(val);
}
Okay, your code really needs to be re-written, we'll deal with that later. Fundamentally, your code works.
The problems (and there are several) involve sleep and interrupts. You should spend some time in bed with the datasheet and get intimate with the section on Power Management and Sleep Modes, in the 2016 version it's ch14.
One issue that will always rear its head as long as you're using the Arduino IDE is that too many of the standard library functions rely on the timer that gets invoked in the background. In a nutshell, the way the timer works involves generating an overflow interrupt approximately every second. Now in sleep idle mode, mcu wakes up on any interrupt, which means every second if nothing but the timer triggers it. This can be useful as the interval can be taken advantage of, scan a few inputs, blah, blah, blah, go back to sleep.
In any of the other sleep modes, the timer0 overflow interrupt has no effect. If you examine the datasheet you'll note that in all the other sleep modes the wakeup associated with INT0 and INT1 is level only. Other pins can handle changes. The interrupt service routine should always be short and sweet. As you have much to do following your interrupt, it is why I suggested using a flag. Set the flag in the interrupt and get out. Once you return to your regular code you can test the flag and move on to switchMosfet. Immediately, or soon after, clear that flag so you're ready to receive the next interrupt.
Now, if you choose to use a pin change interrupt, that would entail another carnal session with the datasheet. For now, I have your code working and with a bit of thought about what's been said you can too.
Before that however, my workspace has quite a bit of background radiation and causes the IR receiver that I'm using to trigger randomly as well as to an intelligent signal. I believe that you need to consider this eventuality as well and think about a way to deal with the wakeup calls that have nothing to do with your process.
Thank you very much for your helpful answer. i will try to understand the datasheet and come back even more stronger

More stronger. Hmmm. Double superlative.
Double superlative.
That's more better, isn't it?