code launched periodically, sleeping between

Hi

I've put in my arduino the sketch you can get at

I have any sensor on my arduino, but it's just a test, all analogRead gives fancy values, that's not a problem

The aim is to launch some code, than makes arduino sleep to consume low power, than, wake it up, launch some code, and so on...

I've only modified "int seconds = 60" in the sketch to launch the script every 60 sec (more often than 300)

Then I connect hterm to the arduino to read info from serial
and i see this:
23@#al=0#t0=381#h0=339#l0=333#b0=329#r\x0\x0\x0\x0\x0\x0
23@#al=0#t0=361#h0=347#l0=368#\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0
23@#al=0#t0=367#h0=352#l0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0

I should read something like:
23@#al=0#t0=381#h0=339#l0=333#b0=329#r\r\n
23@#al=0#t0=361#h0=347#l0=368#b0=329#r\r\n
23@#al=0#t0=367#h0=352#l0=333#b0=329#r\r\n

I really don't understand why i can see nul chars (\x0) in hterm and not the complete data... Any idea ?
(However every message has the same length)

And another question, the function ISR() is launched 91times /sec, but it contains sendData() which may put several seconds to execute herself, so ISR() is blocked until sendData is finished ?

Thanks !

I'm just another noob, but I wonder if you've got the baud rate on your PC set to 19200 also, as it is in the sketch? Maybe you should try 9600.

And another question, the function ISR() is launched 91times /sec, but it contains sendData() which may put several seconds to execute herself, so ISR() is blocked until sendData is finished ?

That would be a problem. The ISR has 'higher priority' than any other function, so this will cause some strange bugs.

I suggest that your ISR sets a flag and that loop triggers the sendData if flag is true. And that send data uses noInterrupt(); and interrupt();

[pseudocode]
volatile boolean send=false;
ISR send=true;

loop()
send ? send();

send(){
noInterrupt();
Serial.println("sending...");
interrupt();
}
[/pseudocode]

Hi AlphaBeta,

So i've changed the code to put this:
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>

#define INIT_TIMER_COUNT 0
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT

int int_counter = 0;
int second = 0;
int counter = 0;

int XBee_pin = 7; // this pins wake up xbee and put it to sleep

int seconds = 60; // time sleeping (aprox seconds)
// MINIMUM VALUE = 5

int time = 2*seconds; // aux. time

int sens0 = 0; // SquidBee variables
int sens1 = 1;
int sens2 = 2;
int sens3 = 3;

int val0 = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;

int id = 23; // node identifier
int al = 0; // alarm code

volatile boolean launch = false;

// Aruino runs at 16 Mhz, so we have 61 Overflows per second?
// 1/ ((16000000 / 1024) / 256) = 1 / 61

ISR(TIMER2_OVF_vect) { // this subroutine is executed 61 times per second

// If sendData is running, we do nothing, not update any counter
// until the end of sendData
if (launch) {
int_counter = 0;
second = 0;
return;
}

int_counter ++;

if (int_counter == 15) {
second++;
int_counter = 0;
}

if (second == time) {
second = 0;
launch = true;
}
}

// function to send data
void sendData(int id, int al, int val_t0,int val_h0,int val_l0,int val_b0){

//ID-virtualNode@#al=0#t0=6#gas0=234#...

Serial.print(id);
Serial.print("@#");

Serial.print("al=");
Serial.print(al);

Serial.print("#t0=");
Serial.print(val_t0);

Serial.print("#h0=");
Serial.print(val_h0);

Serial.print("#l0=");
Serial.print(val_l0);

Serial.print("#b0=");
Serial.print(val_b0);

Serial.println("#r"); // end of message
}

void setup() {

Serial.begin(19200);
pinMode(XBee_pin, OUTPUT);
digitalWrite(XBee_pin,HIGH);

//Timer2 Settings: Timer Prescaler /1024
TCCR2B |= ((1 << CS22) | (1 << CS21) | (1 << CS20));
//Timer2 Overflow Interrupt Enable
TIMSK2 |= (1 << TOIE2);
RESET_TIMER2;
sei();
}

void sleepNow() // here we put the arduino to sleep
{
/* Now is the time to set the sleep mode. In the Atmega8 datasheet

  • Smart | Connected | Secure | Microchip Technology on page 35
  • there is a list of sleep modes which explains which clocks and
  • wake up sources are available in which sleep modus.
  • In the avr/sleep.h file, the call names of these sleep modus are to be found:
  • The 5 different modes are:
  • SLEEP_MODE_IDLE -the least power savings
  • SLEEP_MODE_ADC
  • SLEEP_MODE_PWR_SAVE
  • SLEEP_MODE_STANDBY
  • SLEEP_MODE_PWR_DOWN -the most power savings
  • For now, we want as much power savings as possible, so we
  • choose the according
  • sleep modus: SLEEP_MODE_PWR_DOWN
  • Timer 2 overflow interrupt is only able to wake up the ATmega in PWR_SAVE

*/
set_sleep_mode(SLEEP_MODE_PWR_SAVE); // sleep mode is set here

sleep_enable(); // enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin

sleep_mode(); // here the device is actually put to sleep!!
// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP

sleep_disable(); // first thing after waking from sleep:
// disable sleep...
}

void loop() {

if (launch) {
noInterrupts();
val0 = analogRead(sens0);
val1 = analogRead(sens1);
val2 = analogRead(sens2);
val3 = analogRead(sens3);

sendData(id,al,val0,val1,val2,val3);
launch = false;
interrupts();
}
sleepNow(); // do nothing, only sleep
}

But I get the same problem, and even more \x0 than before !

However when i read http://arduino.cc/en/Reference/NoInterrupts, serial communication may be dropped while no interrupts, so i don't think the noInterrupts() / interrupts() is needed

But even if i delete these 2 lines, i get the same error, \x0 are present and replace the end of each message of SendData

Any idea ?

I think you'll need to declare the variables inside the ISR volatile

Well thanks to chroos on IRC, it works now, I had to add interrupts() in the ISR routine