0

I have to sleep the system in the night and have to wake the system in day time. I am using LDR to get input. LDR input is connected to interrupt pin 0 of Arduino Uno.

Here is my code.

#include <avr/sleep.h>
int led =13;
int triggerPin = 2;

void wakeUpNow() 
{

}

void setup()
{
pinMode(triggerPin, INPUT);
pinMode(led, OUTPUT);
attachInterrupt(0, sleepNow, LOW); 
}

void loop()
{
digitalWrite(led, HIGH);;
delay(1000);
digitalWrite(led, LOW);
}

void sleepNow() 
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); 
sleep_enable(); 
attachInterrupt(0,wakeUpNow, RISING); 
sleep_mode(); 
sleep_disable();
detachInterrupt(0); 
}

Cannot we attach two function in a interrupt?

Edited The following code works perfectly. I want to replace the conditional check with interrupt.

#include <avr/sleep.h>
int led =13;
int wakePin = 2; 

void wakeUpNow() 
{
delay(100);
}

void setup()
{
pinMode(wakePin, INPUT);
pinMode(led, OUTPUT);
//attachInterrupt(0, sleepNow, LOW); 
}

void loop()
{
digitalWrite(led, HIGH);;
delay(1000);
digitalWrite(led, LOW);

if(digitalRead(wakePin)==LOW)
  sleepNow(); 

}

void sleepNow() 
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); 
sleep_enable(); 
attachInterrupt(0,wakeUpNow, RISING); function
sleep_mode(); 
sleep_disable(); 
detachInterrupt(0);
}

I want to replace `

if(digitalRead(wakePin)==LOW)
  sleepNow()

with`

attachInterrupt(0, sleepNow, LOW);

Please let me know how I can do that.

Thank you.

  • Looks to me like you're attaching an interrupt and then immediately detaching it. Could you explain why? – uint128_t Jun 24 '16 at 2:38
  • After attach the interrupt, it goes to sleep. When it wake up from sleep, it detach from the interrupt. – jeyanth90 Jun 24 '16 at 2:47
1

I want to replace the conditional check with interrupt.

Why? If it is awake it might just as well be testing the pin as doing anything else.

If you make it an interrupt, in the ISR interrupts will be off. Then if you go to sleep it will never wake.

So, your current design is the correct one, except:

attachInterrupt(0,wakeUpNow, RISING); function

What is function doing there?

  • The function is not necessary. The normal interrupt automatically wakes up the system. Is that so? – jeyanth90 Jun 24 '16 at 7:41
  • But we have to add the interrupt to wake up the system when LDR gives output high. Isn't it? Otherwise how will the system wake up? – jeyanth90 Jun 24 '16 at 7:50
  • Yes, you wake when the LDR goes high. But why do you want to have an interrupt when it goes LOW? It is already awake. – Nick Gammon Jun 24 '16 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.