Hi,
ich möchte gerne immer wenn ein Pin-Falling an Pin 3 entsteht per RF12 eine Nachricht verschicken und dann wieder in den Schlaf gehen. Dafür habe ich folgenden Quellcode geschrieben. Der aber läuft nur ständig abstürzt. Vielleicht kann jeman den Code ja für mich optimieren.
Vielen Dank.
Gruß kami
#include <Ports.h>
#include <RF12.h>
#include <avr/sleep.h>
#define LOGGERNUM 56
#define TESTSEC 5
typedef struct {
unsigned int device;
unsigned int error;
unsigned int lobat;
unsigned int status1;
} lowbatterystruct;
static byte radioIsOn,testval = 1;
MilliTimer countTimer;
int seccount2 = 0;
static void wakeupToSend (const void* ptr, byte len) {
char sending = rf12_easySend(ptr, len);
if (sending) {
// make sure the radio is on again
if (!radioIsOn)
rf12_sleep(-1); // turn the radio back on
radioIsOn = 1;
}
Serial.println("Send");
}
void setup()
{
Serial.begin(57600);
Serial.println("Logger");
rf12_initialize(8, RF12_868MHZ, 5);
rf12_easyInit(3);
lowbatterystruct msgfile;
msgfile.device = LOGGERNUM;
msgfile.error = 20;
msgfile.lobat = rf12_lowbat();
msgfile.status1 = testval;
while(seccount2 < TESTSEC)
{
if (countTimer.poll(1000)) {
seccount2+=1;
}
rf12_easyPoll();
if (testval == 1)
testval =0;
else testval=1;
lowbatterystruct msgfile;
msgfile.device = LOGGERNUM;
msgfile.error = 20;
msgfile.lobat = rf12_lowbat();
msgfile.status1 = testval;
// send measurement data, but only when it changes
rf12_easySend(&msgfile, sizeof msgfile);
}
rf12_easyInit(0);
rf12_sleep(0); // turn the radio off
radioIsOn = 0;
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
attachInterrupt(1,domessage, FALLING); // use interrupt 0 (pin 2) and run function
sleep_mode();
}
void loop()
{
if (radioIsOn && rf12_easyPoll() == 0 ) {
rf12_sleep(0); // turn the radio off
radioIsOn = 0;
sleepNow();
}
}
void sleepNow() // here we put the arduino to sleep
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
attachInterrupt(1,domessage, FALLING); // use interrupt 0 (pin 2) and run function
// wakeUpNow when pin 2 gets LOW
Serial.println("Sleep");
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...
detachInterrupt(1); // disables interrupt 0 on pin 2 so the
// wakeUpNow code will not be executed
// during normal running time.
}
void domessage()
{
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
if (interrupt_time - last_interrupt_time > 200)
{
if (rf12_easyPoll() == 0 ) {
Serial.println("Start");
if (testval==1) testval=0; else testval=1;
lowbatterystruct msgfile;
msgfile.device = LOGGERNUM;
msgfile.error = 123;
msgfile.lobat = rf12_lowbat();
msgfile.status1 = testval;
wakeupToSend(&msgfile, sizeof msgfile);
}}
last_interrupt_time = interrupt_time;
}