rtc 1307 tijd naar sdcard ?

Arduino specifieke Software
Berichten: 144
Geregistreerd: 29 Okt 2012, 16:54

rtc 1307 tijd naar sdcard ?

Berichtdoor dirk ambu » 23 Jul 2015, 22:34

beste

ik ben bezig met het maken van een temperatuur logger die ook de tijd registreert
nu lukt het mij niet om de tijd in de eerste cel van een csv file op te slaan
ik zou graag de rtc tijd opnemen in een datastring
het lukt mij alleen niet om eerst de tijd samen te pakken tot een pakketje dat ik daarna in de string kan zetten als tijd


code is nog niet echt netjes en is door het zoeken nog meer een zooitje geworden
maar het werkt al aardig als het lukt om de tijd in de eerste cel te zetten dan kan ik verder met het netjes maken van de code

wie kan me even verder helpen ?


code2=cpp

#include <SD.h> //SD Card Library
#include <Wire.h> //I2C Library
#include <OneWire.h>
#include <SPI.h>
#include "Adafruit_WS2801.h"
#include <Time.h>
#include <TimeAlarms.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t


//SPI SD Card Pins
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
int CS_pin = 10;
int pow_pin = 8;
int time_val;


//ds18d20 pin
OneWire ds(7); // on pin 10 (a 4.7K resistor is necessary)
float refresh_rate = 0.0; //Dataloger Refresh Rate
int temp_address = 45; //Address of the I2C Temp Sensor
long id = 1; //Use this to store the id # of our reading.


//IR Distance Sensor Pins
int IR1_pin = 1;
int IR2_pin = 2;
//int time_pin = 2;

//Light Sensor Pins
int light_pin = 0;


// the pin that the LED is attached to
int LedPin1 = 4;

// Choose which 2 pins you will use for output.
// Can be any valid output pins.
// The colors of the wires may be totally different so
// BE SURE TO CHECK YOUR PIXELS TO SEE WHICH WIRES TO USE!
int dataPin = 2; // Yellow wire on Adafruit Pixels
int clockPin = 3; // Green wire on Adafruit Pixels

// Don't forget to connect the ground wire to Arduino ground,
// and the +5V wire to a +5V supply

// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
Adafruit_WS2801 strip = Adafruit_WS2801(8, dataPin, clockPin);

void setup() {
//
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
//
Wire.begin();
//
strip.begin();
//
// Update LED contents, to start they are all 'off'
strip.show();

pinMode(LedPin1, OUTPUT); // declare LedPin1 to be an output:
//

Serial.println("Initializing Card");
//CS Pin is an output
pinMode(CS_pin, OUTPUT);

//SD Card will Draw Power from Pin 8, so set it high
pinMode(pow_pin, OUTPUT);
digitalWrite(pow_pin, HIGH);

//Initialize Card
if (!SD.begin(CS_pin))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");

//Read the Configuration information (COMMANDS.txt)
File commandFile = SD.open("COMMANDS.txt");
if (commandFile)
{
Serial.println("Reading Command File");

float decade = pow(10, (commandFile.available() - 1));
while(commandFile.available())
{
float temp = (commandFile.read() - '0');
refresh_rate = temp*decade+refresh_rate;
decade = decade/10;
}
Serial.print("Refresh Rate = ");
Serial.print(refresh_rate);
Serial.println("ms");
commandFile.close();
}
else
{
Serial.println("Could not read command file.");
return;
}

//Write Log File Header
File logFile = SD.open("LOG.csv", FILE_WRITE);
if (logFile)
{
logFile.println(", , , , ,"); //Just a leading blank line, incase there was previous data
String header = "ID, time, Light, Temp, IR1, IR2";
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println("Couldn't open log file");
}
//setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011 by hand
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");

// create the alarms
// Alarm.timerOnce(10, OnceOnly); // called once after 10 seconds
/*Alarm.timerRepeat(15, Repeats); // timer for every 15 seconds
Alarm.alarmRepeat(8,0,0, MorningAlarm); // 8:00 am worden we wakker led gaan naar roze
Alarm.alarmRepeat(9,0,0, MorningAlarm1); // 9:00 am every day led gaan uit
Alarm.alarmRepeat(17,0,0,EveningAlarm2); // 17:00pm every day led gaan aan oranje kniperen on
Alarm.alarmRepeat(20,00,00,EveningAlarm1); // 20:00pm every day led gaan aan paars on
/* Alarm.alarmRepeat(20,2,30,EveningAlarm2); // 20:02:30 pm every day led gaan knipper tanden poetsen on
Alarm.alarmRepeat(20,4,30,EveningAlarm3); // 20:04:30 pm every day klaar met poetsen licht gaat naar blauw
Alarm.alarmRepeat(20,4,30,EveningAlarm4); // 20:04:30 pm every day klaar met poetsen licht gaat naar blauw
Alarm.alarmRepeat(dowSaturday,21,00,00,WeeklyAlarm); // 21:00:00 every Saturday
*/}



void loop(void) {
//{
if(Serial.available())
{
time_t t = processSyncMessage();
if(t >0)
{
RTC.set(t); // set the RTC and the system time to the received value
setTime(t);
}
}


byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;

if ( !ds.search(addr)) {
// Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
Alarm.delay(250);
return;
}

// Serial.print("ROM =");
for( i = 0; i < 8; i++) {
// Serial.write(' ');
// Serial.print(addr[i], HEX);
}

if (OneWire::crc8(addr, 7) != addr[7]) {
// Serial.println("CRC is not valid!");
return;
}
/* Serial.println();

// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
*/
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
//ds.write(0x44); // start conversion, with parasite power on at the end

Alarm.delay(1500); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.

present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

// Serial.print(" Data = ");
// Serial.print(present, HEX);
// Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
}
//Serial.print(" CRC=");
// Serial.print(OneWire::crc8(data, 8), HEX);
// Serial.println();

// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
//fahrenheit = celsius * 1.8 + 32.0;
// Serial.print(" Temperature = ");
//Serial.print(celsius);
// Serial.println(" Celsius, ");
//Serial.print(fahrenheit);
// Serial.println(" Fahrenheit");

//Check Light Level
int light_level = analogRead(light_pin);
//Serial.println(light_level);
//Read Distances
int IR1_val = analogRead(IR1_pin);
int IR2_val = analogRead(IR2_pin);
//Serial.println(IR1_val);
//Serial.println(IR2_val);
// int time_val = analogRead(IR2_pin);
//Serial.print(time);

//Create Data string for storing to SD card
//We will use CSV Format
// String dataString = String(id) + ", " + String(light_level) + ", " + String(temp_f) + ", " + String(IR1_val) + ", " + String(IR2_val);
String dataString = String(id) + ", " + String(time_val) + ", " + String(light_level) + ", " + String(celsius) + ", " + String(IR1_val) + ", " + String(IR2_val);

//Open a file to write to
//Only one file can be open at a time
File logFile = SD.open("LOG.csv", FILE_WRITE);
if (logFile)
{
logFile.println(dataString);
logFile.close();
Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
}

//Increment ID number
id++;

Alarm.delay(refresh_rate);

digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
/*
// functions to be called when an alarm triggers:
void MorningAlarm(){
Serial.println("MorningAlarm: - turn lights off");
colorWipe(Color(10, 0, 2), 10); // roze
}
/*
void MorningAlarm1(){
Serial.println("MorningAlarm1: - turn lights off");
colorWipe(Color(0, 0, 0), 50);
}

void EveningAlarm(){
Serial.println("EveningAlarm: - turn lights on");
//rainbow(20);
rainbowCycle(20);

}
void EveningAlarm1(){
Serial.println("EveningAlarm1: - turn lights on");
rainbow(20);
//rainbowCycle(20);

}
void EveningAlarm2(){
Serial.println("EveningAlarm2: - turn lights on");
rainbow(20);
rainbowCycle(20);

}
void EveningAlarm3(){
Serial.println("EveningAlarm3: - turn lights on");
//rainbow(20);
rainbowCycle(20);

}
void EveningAlarm4(){
Serial.println("EveningAlarm4: - turn lights on");
//rainbow(20);
rainbowCycle(20);

}
void WeeklyAlarm(){
Serial.println("weeklyAlarm: - its Monday Morning");
}
*/
void ExplicitAlarm(){
Serial.println("explicitAlarm: - this triggers only at the given date and time");
}

void Repeats(){
Serial.println("15 second timer");
digitalWrite(LedPin1, HIGH); // turn the LED on (HIGH is the voltage level)
Alarm.delay(1000); // wait for a second
digitalWrite(LedPin1, LOW); // turn the LED off by making the voltage LOW
Alarm.delay(1000); // wait for a second
}

void OnceOnly(){
Serial.println("This timer only triggers once");
// testen eerst de strip of hij werkt dit doen we 1 maal bij het opstarten
colorWipe(Color(10, 0, 2), 10); // rood groen blauw
//colorWipe(Color(0, 255, 0), 50);
//colorWipe(Color(0, 0, 255), 50);
//colorWipe(Color(0, 0, 0), 50);


}


void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();

}

void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}

/*-----------------------------------------------------set led program----------------------------------------------------*/

void rainbow(uint8_t wait) {
int i, j;

for (j=0; j < 256; j++) { // 3 cycles of all 256 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel( (i + j) % 255));
}
strip.show(); // write all the pixels out
Alarm.delay(wait);
}
}

// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle(uint8_t wait) {
int i, j;

for (j=0; j < 256 * 5; j++) { // 5 cycles of all 25 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
// tricky math! we use each pixel as a fraction of the full 96-color wheel
// (thats the i / strip.numPixels() part)
// Then add in j which makes the colors go around per pixel
// the % 96 is to make the wheel cycle around
strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
}
strip.show(); // write all the pixels out
Alarm.delay(wait);
}
}

// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(uint32_t c, uint8_t wait) {
int i;

for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
Alarm.delay(wait);
}
}

/*-----------------------------------------------------set Helper functions----------------------------------------------------*/

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}

//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
uint32_t Wheel(byte WheelPos)
{
if (WheelPos < 85) {
return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}

/*-----------------------------------------------------set rtc program----------------------------------------------------*/

/* code to process time sync messages from the serial port */
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message

time_t processSyncMessage() {
// return the time if a valid sync message is received on the serial port.
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
return pctime;
}
}
return 0;
}
/ code2

Advertisement

Gebruikers-avatar
Berichten: 188
Geregistreerd: 23 Dec 2011, 00:12

Re: rtc 1307 tijd naar sdcard ?

Berichtdoor Duality » 24 Jul 2015, 02:07

ik denk dat je wat blokhaken bent vergeten.
[ code2=cpp][/ code2]
maar dan zonder de spaties.

Berichten: 144
Geregistreerd: 29 Okt 2012, 16:54

Re: rtc 1307 tijd naar sdcard ?

Berichtdoor dirk ambu » 24 Jul 2015, 10:04

zo het is gelukt om het tussen [ code2=cpp][ /code2]te krijgen

ik zou graag in regel 266 String(time_val) willen vervangen voor iets van time waarbij ik de tijd in 1 keer kan aanroepen
hoe kan ik dit het beste doen met minimaal gebruik van geheugen van de uno


cpp code
#include <SD.h> //SD Card Library
#include <Wire.h> //I2C Library
#include <OneWire.h>
#include <SPI.h>
#include "Adafruit_WS2801.h"
#include <Time.h>
#include <TimeAlarms.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t


//SPI SD Card Pins
//MOSI = Pin 11
//MISO = Pin 12
//SCLK = PIN 13
int CS_pin = 10;
int pow_pin = 8;
int time_val;


//ds18d20 pin
OneWire ds(7); // on pin 10 (a 4.7K resistor is necessary)
float refresh_rate = 0.0; //Dataloger Refresh Rate
int temp_address = 45; //Address of the I2C Temp Sensor
long id = 1; //Use this to store the id # of our reading.


//IR Distance Sensor Pins
int IR1_pin = 1;
int IR2_pin = 2;
//int time_pin = 2;

//Light Sensor Pins
int light_pin = 0;


// the pin that the LED is attached to
int LedPin1 = 4;

// Choose which 2 pins you will use for output.
// Can be any valid output pins.
// The colors of the wires may be totally different so
// BE SURE TO CHECK YOUR PIXELS TO SEE WHICH WIRES TO USE!
int dataPin = 2; // Yellow wire on Adafruit Pixels
int clockPin = 3; // Green wire on Adafruit Pixels

// Don't forget to connect the ground wire to Arduino ground,
// and the +5V wire to a +5V supply

// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
Adafruit_WS2801 strip = Adafruit_WS2801(8, dataPin, clockPin);

void setup() {
//
Serial.begin(9600); // initialize serial communication at 9600 bits per second:
//
Wire.begin();
//
strip.begin();
//
// Update LED contents, to start they are all 'off'
strip.show();

pinMode(LedPin1, OUTPUT); // declare LedPin1 to be an output:
//

Serial.println("Initializing Card");
//CS Pin is an output
pinMode(CS_pin, OUTPUT);

//SD Card will Draw Power from Pin 8, so set it high
pinMode(pow_pin, OUTPUT);
digitalWrite(pow_pin, HIGH);

//Initialize Card
if (!SD.begin(CS_pin))
{
Serial.println("Card Failure");
return;
}
Serial.println("Card Ready");

//Read the Configuration information (COMMANDS.txt)
File commandFile = SD.open("COMMANDS.txt");
if (commandFile)
{
Serial.println("Reading Command File");

float decade = pow(10, (commandFile.available() - 1));
while(commandFile.available())
{
float temp = (commandFile.read() - '0');
refresh_rate = temp*decade+refresh_rate;
decade = decade/10;
}
Serial.print("Refresh Rate = ");
Serial.print(refresh_rate);
Serial.println("ms");
commandFile.close();
}
else
{
Serial.println("Could not read command file.");
return;
}

//Write Log File Header
File logFile = SD.open("LOG.csv", FILE_WRITE);
if (logFile)
{
logFile.println(", , , , ,"); //Just a leading blank line, incase there was previous data
String header = "ID, time, Light, Temp, IR1, IR2";
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println("Couldn't open log file");
}
//setTime(8,29,0,1,1,11); // set time to Saturday 8:29:00am Jan 1 2011 by hand
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");

// create the alarms
// Alarm.timerOnce(10, OnceOnly); // called once after 10 seconds
/*Alarm.timerRepeat(15, Repeats); // timer for every 15 seconds
Alarm.alarmRepeat(8,0,0, MorningAlarm); // 8:00 am worden we wakker led gaan naar roze
Alarm.alarmRepeat(9,0,0, MorningAlarm1); // 9:00 am every day led gaan uit
Alarm.alarmRepeat(17,0,0,EveningAlarm2); // 17:00pm every day led gaan aan oranje kniperen on
Alarm.alarmRepeat(20,00,00,EveningAlarm1); // 20:00pm every day led gaan aan paars on
/* Alarm.alarmRepeat(20,2,30,EveningAlarm2); // 20:02:30 pm every day led gaan knipper tanden poetsen on
Alarm.alarmRepeat(20,4,30,EveningAlarm3); // 20:04:30 pm every day klaar met poetsen licht gaat naar blauw
Alarm.alarmRepeat(20,4,30,EveningAlarm4); // 20:04:30 pm every day klaar met poetsen licht gaat naar blauw
Alarm.alarmRepeat(dowSaturday,21,00,00,WeeklyAlarm); // 21:00:00 every Saturday
*/}



void loop(void) {
//{
if(Serial.available())
{
time_t t = processSyncMessage();
if(t >0)
{
RTC.set(t); // set the RTC and the system time to the received value
setTime(t);
}
}


byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;

if ( !ds.search(addr)) {
// Serial.println("No more addresses.");
Serial.println();
ds.reset_search();
Alarm.delay(250);
return;
}

// Serial.print("ROM =");
for( i = 0; i < 8; i++) {
// Serial.write(' ');
// Serial.print(addr[i], HEX);
}

if (OneWire::crc8(addr, 7) != addr[7]) {
// Serial.println("CRC is not valid!");
return;
}
/* Serial.println();

// the first ROM byte indicates which chip
switch (addr[0]) {
case 0x10:
Serial.println(" Chip = DS18S20"); // or old DS1820
type_s = 1;
break;
case 0x28:
Serial.println(" Chip = DS18B20");
type_s = 0;
break;
case 0x22:
Serial.println(" Chip = DS1822");
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}
*/
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
//ds.write(0x44); // start conversion, with parasite power on at the end

Alarm.delay(1500); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.

present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

// Serial.print(" Data = ");
// Serial.print(present, HEX);
// Serial.print(" ");
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(" ");
}
//Serial.print(" CRC=");
// Serial.print(OneWire::crc8(data, 8), HEX);
// Serial.println();

// Convert the data to actual temperature
// because the result is a 16 bit signed integer, it should
// be stored to an "int16_t" type, which is always 16 bits
// even when compiled on a 32 bit processor.
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// "count remain" gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
// at lower res, the low bits are undefined, so let's zero them
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
//// default is 12 bit resolution, 750 ms conversion time
}
celsius = (float)raw / 16.0;
//fahrenheit = celsius * 1.8 + 32.0;
// Serial.print(" Temperature = ");
//Serial.print(celsius);
// Serial.println(" Celsius, ");
//Serial.print(fahrenheit);
// Serial.println(" Fahrenheit");

//Check Light Level
int light_level = analogRead(light_pin);
//Serial.println(light_level);
//Read Distances
int IR1_val = analogRead(IR1_pin);
int IR2_val = analogRead(IR2_pin);
//Serial.println(IR1_val);
//Serial.println(IR2_val);
// int time_val = analogRead(IR2_pin);
//Serial.print(time);

//Create Data string for storing to SD card
//We will use CSV Format
// String dataString = String(id) + ", " + String(light_level) + ", " + String(temp_f) + ", " + String(IR1_val) + ", " + String(IR2_val);
String dataString = String(id) + ", " + String(time_val) + ", " + String(light_level) + ", " + String(celsius) + ", " + String(IR1_val) + ", " + String(IR2_val);

//Open a file to write to
//Only one file can be open at a time
File logFile = SD.open("LOG.csv", FILE_WRITE);
if (logFile)
{
logFile.println(dataString);
logFile.close();
Serial.println(dataString);
}
else
{
Serial.println("Couldn't open log file");
}

//Increment ID number
id++;

Alarm.delay(refresh_rate);

digitalClockDisplay();
Alarm.delay(1000); // wait one second between clock display
}
/*
// functions to be called when an alarm triggers:
void MorningAlarm(){
Serial.println("MorningAlarm: - turn lights off");
colorWipe(Color(10, 0, 2), 10); // roze
}
/*
void MorningAlarm1(){
Serial.println("MorningAlarm1: - turn lights off");
colorWipe(Color(0, 0, 0), 50);
}

void EveningAlarm(){
Serial.println("EveningAlarm: - turn lights on");
//rainbow(20);
rainbowCycle(20);

}
void EveningAlarm1(){
Serial.println("EveningAlarm1: - turn lights on");
rainbow(20);
//rainbowCycle(20);

}
void EveningAlarm2(){
Serial.println("EveningAlarm2: - turn lights on");
rainbow(20);
rainbowCycle(20);

}
void EveningAlarm3(){
Serial.println("EveningAlarm3: - turn lights on");
//rainbow(20);
rainbowCycle(20);

}
void EveningAlarm4(){
Serial.println("EveningAlarm4: - turn lights on");
//rainbow(20);
rainbowCycle(20);

}
void WeeklyAlarm(){
Serial.println("weeklyAlarm: - its Monday Morning");
}
*/
void ExplicitAlarm(){
Serial.println("explicitAlarm: - this triggers only at the given date and time");
}

void Repeats(){
Serial.println("15 second timer");
digitalWrite(LedPin1, HIGH); // turn the LED on (HIGH is the voltage level)
Alarm.delay(1000); // wait for a second
digitalWrite(LedPin1, LOW); // turn the LED off by making the voltage LOW
Alarm.delay(1000); // wait for a second
}

void OnceOnly(){
Serial.println("This timer only triggers once");
// testen eerst de strip of hij werkt dit doen we 1 maal bij het opstarten
colorWipe(Color(10, 0, 2), 10); // rood groen blauw
//colorWipe(Color(0, 255, 0), 50);
//colorWipe(Color(0, 0, 255), 50);
//colorWipe(Color(0, 0, 0), 50);


}


void digitalClockDisplay()
{
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.println();

}

void printDigits(int digits)
{
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}

/*-----------------------------------------------------set led program----------------------------------------------------*/

void rainbow(uint8_t wait) {
int i, j;

for (j=0; j < 256; j++) { // 3 cycles of all 256 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, Wheel( (i + j) % 255));
}
strip.show(); // write all the pixels out
Alarm.delay(wait);
}
}

// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle(uint8_t wait) {
int i, j;

for (j=0; j < 256 * 5; j++) { // 5 cycles of all 25 colors in the wheel
for (i=0; i < strip.numPixels(); i++) {
// tricky math! we use each pixel as a fraction of the full 96-color wheel
// (thats the i / strip.numPixels() part)
// Then add in j which makes the colors go around per pixel
// the % 96 is to make the wheel cycle around
strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
}
strip.show(); // write all the pixels out
Alarm.delay(wait);
}
}

// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(uint32_t c, uint8_t wait) {
int i;

for (i=0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
Alarm.delay(wait);
}
}

/*-----------------------------------------------------set Helper functions----------------------------------------------------*/

// Create a 24 bit color value from R,G,B
uint32_t Color(byte r, byte g, byte b)
{
uint32_t c;
c = r;
c <<= 8;
c |= g;
c <<= 8;
c |= b;
return c;
}

//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
uint32_t Wheel(byte WheelPos)
{
if (WheelPos < 85) {
return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
} else if (WheelPos < 170) {
WheelPos -= 85;
return Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else {
WheelPos -= 170;
return Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
}

/*-----------------------------------------------------set rtc program----------------------------------------------------*/

/* code to process time sync messages from the serial port */
#define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
#define TIME_HEADER 'T' // Header tag for serial time sync message

time_t processSyncMessage() {
// return the time if a valid sync message is received on the serial port.
while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
char c = Serial.read() ;
Serial.print(c);
if( c == TIME_HEADER ) {
time_t pctime = 0;
for(int i=0; i < TIME_MSG_LEN -1; i++){
c = Serial.read();
if( c >= '0' && c <= '9'){
pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
}
}
return pctime;
}
}
return 0;
}

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: rtc 1307 tijd naar sdcard ?

Berichtdoor nicoverduin » 24 Jul 2015, 10:43

In de rtc lib staan alle functies voor uren, tijd, minuten etc.
met de functie sprintf() kun je een formatted sting opbouwen. En ipv String(time_val) kun je gewoon de resultaat string opnemen.
en als je geheugen wilt besparen zou ik:
a) alle String functies vervangen voor gewone char[] functies zoals dtrcpy, strcat etc. String is een hele dure functie met betrekking tot geheugen gebruik en geheugen fragmentatie. Zelfs die string die je opbouwt zou wel eens 3-4x zoveel RAM geheugen kunnen gebruiken tijdens het opbouwen van die String. Ook al wordt alles weer vrij gegeven na beindiging van de functie.
b) in de SD functies de file 1x openen en na de schrijf actie SD.flush() uitvoeren. Dan wordt de buffer gelijk weggeschreven.
c) Als het kan alleen die variabelen globaal maken die echt globaal moeten. Door variabelen lokaal te maken worden ze gecreerd tijdens de uitvoering van die functie en weer vrijgegeven na beindiging van de functie.
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 92 gasten