Ive got a thermometer with optional RS232. When RS232 is turned on i want the data to be logged on a SD-card. Since the Arduino would be running continuously and wasting a lot of energy i want to use LowPower.h with a trigger set to the data, which is either never or only once per second if RS232 is on.
However... the logged Data is not correct anymore. Its incomplete, cut of.
I can see a difference in the intensity of the LED during the reading, without any powersaving its on for maybe 100ms and brightly lit, with powersaving its only shortly lit up a tiny bit.
So i figure the interrupt must be working but the device it not on long enough to send everything to the SD-card. I tryed to add a 100ms delay after the reading but that didnt change anything.
The code works fine without the energy/interrupt parts. How can i change things to get the data out correctly?
#include <SoftwareSerial.h>
#include <SD.h>
#include <SPI.h>
#include <LowPower.h>
const int chipSelect = 10;
#define rxPin 8
#define txPin 9
const int wakeUpPin = 8; // Use pin 8 as wake up pin
int led = 13;
volatile int state = LOW;
// set up a new serial port
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin, true);
void setup() {
pinMode(wakeUpPin, INPUT); // Configure wake up pin as input.
pinMode(led, OUTPUT); //digitalWrite(2, HIGH);
Serial.begin(9600); // open serial port, set the baud rate as 9600 bps
delay(1000); // while (!Serial) {
Serial.print("Initializing SD card..."); // wait for serial port to connect. Needed for native USB port only
if (!SD.begin(chipSelect)) { // see if the card is present and can be initialized:
Serial.println("Card failed or not present");
return; // don't do anything more:
}
Serial.println("card initialized.");
// define pin modes for tx, rx:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
Serial.begin(9600);
}
void loop() {
LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
// Allow wake up pin to trigger interrupt on high.
attachInterrupt(0, toggle, HIGH); //note the High on my design
// Enter power down state with ADC and BOD module disabled.
// Wake up when wake up pin is high.
if (mySerial.available() > 0) {
char data = mySerial.read();
File dataFile = SD.open("log.txt", FILE_WRITE);
dataFile.print(data);
dataFile.close();
Serial.print(data);
}
delay (100);
}
void toggle() {
if (state == LOW)
{
state = HIGH;
}
else
{
state = LOW;
}
digitalWrite(led, state);
}