Go Down

Topic: [Arduino Micro + HopeRF69] : Serial doesn't work when waking up from sleep (Read 518 times) previous topic - next topic

arizk

Hello guys, hope everyone's doing fine !

I'm working these days on a communication project and i'm currently blocked at waking my ATMEGA32U4 from sleep power down mode. Indeed, to save as much as possible the power consumption of my radio device, i'd like to put it on sleep and wake it up everytime i'm receiving or sending data.

For that, i'd like to get my Atmega to wake up from power down-sleep mode everytime i trigger RXLED (by pin change interrupt on PCINT0 in other words) to test it firstly and thenif it works i can implement my radio library and link both to ensure a reliable use of my system.

The results of my test are currently satisfying, i can wake up my system from sleep (using a simple push-button system) but the only problem left is the use of serial.

In fact, to test it the interruption, i'm using an int-typed value so that each time i push the button it increments it and then i simply print in on the serial but the problem remain in the fact that it only works when i reset the serial monitor. Here is the code i'm using :

/////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <arduino.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <avr/io.h>

volatile int value = 0;
int pinBtn = 17 ;



void setup()
 {
     Serial.begin(9600);
     while (!Serial.available());
   
   Serial.println("Begin:");
     
   //IO configuration  
   pinMode (pinBtn,INPUT);
   digitalWrite(pinBtn,LOW);
    /////////////////////////////////////////////////
 
   //Interrupt configuration
   init_interrupt();        
   /////////////////////////////////////////////////
   
   set_sleep_mode(SLEEP_MODE_PWR_DOWN); // set to power down mode
 }


ISR(PCINT0_vect)
 {    
     delay (100);
     SMCR = 0;
     value++;
     Serial.println(value);
 }

void init_interrupt()
{
           noInterrupts(); //Disable interrupts
           PCICR|=(1<<PCIE0);   // Pin Change Interrupt Control Register : - - - - - PCIC0 => "-------1"
           PCIFR&=0xFE;   // Pin Change Interrupt Flag Register : - - - - - PCIF0 => "-------0"
           PCMSK0|=(0x01<<PCINT0);  // Pin Change Mask Register = Enable interrupt  PCINT0
           interrupts(); // Enable interrupts
 }


void put_to_sleep()
 {
     USBDevice.detach();
     sleep_enable(); // enables sleep bit for safety
     sleep_cpu();
     sleep_disable();
     delay(100);
     USBDevice.attach();//USB physically connected
     
     
     
     Serial.begin(9600); //restart the serial communication
     while (!Serial.available());
 }



void loop()
 {
     delay(100);
     Serial.println("AB");
     delay(1000);
     put_to_sleep();
     
 }

///////////////////////////////////////////////////////////////////////////////////////////////////////////


#include <arduino.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <avr/io.h>

volatile int value = 0;
int pinBtn = 17 ;



void setup()
 {
     Serial.begin(9600);
     while (!Serial.available());
   
   Serial.println("Begin:");
     
   //IO configuration  
   pinMode (pinBtn,INPUT);
   digitalWrite(pinBtn,LOW);
    /////////////////////////////////////////////////
 
   //Interrupt configuration
   init_interrupt();        
   /////////////////////////////////////////////////
   
   set_sleep_mode(SLEEP_MODE_PWR_DOWN); // set to power down mode
 }


ISR(PCINT0_vect)
 {    
     delay (100);
     SMCR = 0;
     value++;
     Serial.println(value);
 }

void init_interrupt()
{
           noInterrupts(); //Disable interrupts
           PCICR|=(1<<PCIE0);   // Pin Change Interrupt Control Register : - - - - - PCIC0 => "-------1"
           PCIFR&=0xFE;   // Pin Change Interrupt Flag Register : - - - - - PCIF0 => "-------0"
           PCMSK0|=(0x01<<PCINT0);  // Pin Change Mask Register = Enable interrupt  PCINT0
           interrupts(); // Enable interrupts
 }


void put_to_sleep()
 {
     USBDevice.detach();
     sleep_enable(); // enables sleep bit for safety
     sleep_cpu();
     sleep_disable();
     delay(100);
     USBDevice.attach();//USB physically connected
     
     
     
     Serial.begin(9600); //restart the serial communication
     while (!Serial.available());
 }



void loop()
 {
     delay(100);
     Serial.println("AB");
     delay(1000);
     put_to_sleep();
     
 }

///////////////////////////////////////////////////////////////////////////////////////////////////////////
The problem is probably in the attach/ detach use in the sleep function i implemented. In fact, it detach the USB then attach it before and after setting to sleep so that it doesn't disable them.
When i run the code, only the println("AB") is showed firstly (and only one time because of the sleep instruction) even when pushing the button as many times as we do it doesn't display anything until i reset manually the serial monitor and then it shows the value incremented as many times as i pushed the button from the debut (Ex : if i run the code, i trigger the serial so that it works and then i push the button 2 times, I reset the serial monitor and then it shows 5 (pin CHANGE interrupt detects both FALLING and RISING that's why it shows 5 instead of 2) and then if the loop is woken up only until i trigger the serial again by writting and sending smtg)

Maybe the methode i'm using is not really the one to choose or maybe i'm just forgetting, i'm fairly new at arduino coding so i'm struggling a little bit. If i can make an assumption, it would be that when re-attaching the USBDevice, it chooses randomly a serial but it's still confusing so i'd like to hear from ur knowledgeable opinions, maybe you can help and save me !

Thank you in advance !

Go Up