Es geht um folgendes.
Habe ein RTC Modul DS1302 an meinen Arduino soweit so gut.
Die Ausgabe über den Seriellen Monitor funktioniert auch bestens.
Jetzt zu meinen Problem.
Möchte den Wochentag mit 7 Leds darstellen. Z.B.
Am Montag leuchtet LED 1,Am Dienstag LED2 usw.
schaffe allerdings nicht das in meinen Code zu bekommen vlt. könnte mir jemand unter die arme greifen.
Ich hoffe das mein Code bis hier hin passt danke schon mal mfg
Daniel
#include <stdio.h>
#include <DS1302.h>
namespace {
const int kCePin = 5; // Chip Enable
const int kIoPin = 6; // Input/Output
const int kSclkPin = 7; // Serial Clock
DS1302 rtc(kCePin, kIoPin, kSclkPin);
String dayAsString(const Time::Day day) {
switch (day) {
case Time::kSunday: return "Sunday";
case Time::kMonday: return "Monday";
case Time::kTuesday: return "Tuesday";
case Time::kWednesday: return "Wednesday";
case Time::kThursday: return "Thursday";
case Time::kFriday: return "Friday";
case Time::kSaturday: return "Saturday";
}
return "(unknown day)";
}
void printTime() {
Time t = rtc.time();
const String day = dayAsString(t.day);
char buf[50];
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
day.c_str(),
t.yr, t.mon, t.date,
t.hr, t.min, t.sec);
Serial.println(buf);
}
}
void setup() {
Serial.begin(9600);
rtc.writeProtect(true);
rtc.halt(false);
Time t(2016, 5, 2, 13, 26, 00, Time::kSaturday);
rtc.time(t);
}
void loop() {
printTime();
delay(1000);
}
Dann musst du aber noch die letzte LED vom letzten Wochentag (wenn das System schon up war)
noch ausschalten. Sonst hast du nach 7 Tagen allen an.
Man sollte sowieso den Tageswechsel antesten sonst ballert der arme AT-Mega permanent auf
der Schnittstelle und macht immer die selbe LED an obwohl die doch schon leuchtet !
beeblebrox:
Dann musst du aber noch die letzte LED vom letzten Wochentag (wenn das System schon up war)
noch ausschalten. Sonst hast du nach 7 Tagen allen an.
Ja, das stimmt.
Also alle anderen Leds ausschalten.
Das sollte da noch mit rein.
Aber evtl. hat hier noch jemand eine einfachere Lösung parat.
michael_x:
noch mehr RAM sparen kannst du per F() Makro
Und PROGMEM und PSTR()
Man kann die Strings in ein String Array im Flash packen. Und den entsprechenden Tag dann mit strcpy_P() in einen Puffer im RAM kopieren. Wobei der Index der Wochentag ist
Ok aber kann mir wer sagen warum meine Led so schwach leuchtet wenn ich den Blink Code verwende als test ist sie schön hell und in diesen komplett dunkel. hat da jemand eine Idee? Stimmt mein Code jetzt soweit?
mfg
#include <stdio.h>
#include <DS1302.h>
namespace {
const int kCePin = 5; // Chip Enable
const int kIoPin = 6; // Input/Output
const int kSclkPin = 7; // Serial Clock
DS1302 rtc(kCePin, kIoPin, kSclkPin);
const char* dayAsString(const Time::Day day) {
switch (day) {
case Time::kSunday: digitalWrite(10, HIGH);
return "Sunday";
case Time::kMonday: digitalWrite(9, HIGH);
return "Monday";
case Time::kTuesday: digitalWrite(8, HIGH);
return "Tuesday";
case Time::kWednesday: digitalWrite(3, HIGH);
return "Wednesday";
case Time::kThursday: digitalWrite(2, HIGH);
return "Thursday";
case Time::kFriday: digitalWrite(12, HIGH);
return "Friday";
case Time::kSaturday: digitalWrite(11, HIGH);
return "Saturday";
}
return "(unknown day)";
}
void printTime() {
Time t = rtc.time();
const char* day = dayAsString(t.day);
char buf[50];
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
day,
t.yr, t.mon, t.date,
t.hr, t.min, t.sec);
Serial.println(buf);
}
}
void setup() {
Serial.begin(9600);
rtc.writeProtect(false);
rtc.halt(false);
Time t(2016, 5, 2, 13, 26, 00, Time::kSaturday);
rtc.time(t);
}
void loop() {
printTime();
delay(1000);
}