Hallo Zusammen,
ich versuche seit ein paar Tage eine Berechnung mit meinem Arduino und meinem RTC Modul zu machen. Die Berechnung funktioniert aber nicht vollständig.
Also zur Verfügung habe ich ein PmodRTCC modul, ein TFT LCD Display von Adafruit und ein Arduino Mega.
Ich möchte die Aktuelle Laufzeit eines Gerät berechnet. Dafür wenn ich auf dem Startknopf drücke wird die Startzeit gezeigt und in der SD-Karte gespeichert. Wenn ich auf dem Save-knopf drücke lese ich meine Startzeit aus und wenn ich auf dem Aktknopf lese ich dann die Aktuelle Laufzeit. Aber manchmal wird die Aktuelle Laufzeit mit einem Minuszeichen vor den Seconde und Minute. Vielleicht ist meine Berechnung falsch. So ist die Gleichung für die Berechnung : Aktuelle Laufzeit=Aktuelle zeit - Startzeit.
Könnt ihr mir bitte helfen!?
Ich danke euch schon im Voraus
Hier ist mein Code
void LZ_Aktuelle()
{
DateTime now = rtc.now();
int start_Std=now.hour(); int start_min=now.minute(); int start_sec=now.second();
while(buttonLZ_Akt = true)
{
buttonFenster = false;
//Darstellung des Fensters
tft.drawLine(0, 200, 320, 200, ILI9341_BLACK); //Draw first vertical line
tft.drawLine(106, 200, 106, 240, ILI9341_BLACK); //Draw first horizontal line
tft.drawLine(212, 200, 212, 240, ILI9341_BLACK); //Draw second horizontal line
tft.setCursor(10,210);
tft.setTextColor(ILI9341_RED,ILI9341_WHITE);
tft.setTextSize(3);
tft.println("Start");
tft.setCursor(10,10);
tft.setTextColor(ILI9341_RED,ILI9341_WHITE);
tft.setTextSize(2);
tft.println("<Back>");
tft.setCursor(116,210);
tft.setTextColor(ILI9341_RED,ILI9341_WHITE);
tft.setTextSize(3);
tft.println("Akt");
tft.setCursor(222,210);
tft.setTextColor(ILI9341_RED,ILI9341_WHITE);
tft.setTextSize(3);
tft.println("Save");
digitalWrite(0, HIGH);
TS_Point p = ts.getPoint();
digitalWrite(0, LOW);
//pinMode(XP, OUTPUT);
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//pinMode(YM, OUTPUT);
if(ts.touched())
{
p.x = map(p.x, TS_MINY, TS_MAXY, 0, tft.height());
p.y = map(p.y, TS_MINX, TS_MAXX, 0, tft.width());
int y = tft.height() - p.x;
int x = p.y;
tft.setRotation(1);
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
// un REM next 2 lines when trying to fin coordinates for a switch etc
//Serial.print("X = "); Serial.print(p.x);
//Serial.print("\tY = "); Serial.println(p.y);
// Checkto see if START pressed
if (((x > 0) && (x <106)) && ((y > 200) && (y <240))) {
// if(v)
// {
Timerfunktion(); // Nachdem Drücken des Startkopfs wird direkt die Start Uhrzeit gezeigt
/* tft.setTextSize(3);
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setCursor(10, 80);
tft.println(now.hour(), DEC);
Serial.print(now.hour(), DEC);
tft.setTextSize(3);
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setCursor(50, 80);
tft.println(':');
Serial.print(':');
tft.setTextSize(3);
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setCursor(70, 80);
tft.println(now.minute(), DEC);
Serial.print(now.minute(), DEC);
tft.setTextSize(3);
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setCursor(110, 80);
tft.println(':');
Serial.print(':');
tft.setTextSize(3);
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setCursor(130, 80);
tft.println(now.second(), DEC);
Serial.print(now.second(), DEC);
Serial.println();*/
// Startzeit wird in der SD-Karte gespeichert
File myFile = SD.open("Def_File.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
sprintf(buf1, "%02d:%02d:%02d", start_Std, start_min, start_sec);
myFile.println(buf1);
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
Serial.println(v);
v=false;
}
// Auslesen der gespeicherte Startzeit
if (((x > 212) && (x <320)) && ((y > 200) && (y <240))) {
myFile = SD.open("Def_File.txt");
get_last_line(myFile);
Serial.print(F("last line: "));
Serial.println(buf1);
tft.setCursor(10, 100);
tft.setTextSize(3);
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.print(buf1);
parse();
if (myFile)
myFile.close();
v=false;
}
// Presst des Aktknopfs und Berechnung der Aktuelle laufzeit
if (((x > 106) && (x <212)) && ((y > 200) && (y <240))) {
sprintf(buf, "%02d:%02d:%02d", rtc.now().hour()-start_Std, rtc.now().minute()-start_min, rtc.now().second()-start_sec);
tft.setTextColor(ILI9341_BLACK, ILI9341_WHITE);
tft.setTextSize(3);
tft.setCursor(10,160);
tft.print(buf);
//Serial.println(start_sec);
}
//zurück zu dem Homepage
if (p.z > MINPRESSURE && p.z < MAXPRESSURE) {
if (((x < 50) && (x > 10)) && ((y < 25) && (y > 5))) (Back = 1);
if (((x < 50) && (x > 10)) && ((y < 25) && (y > 5))){
if (Back == 1) {
tft.fillScreen(ILI9341_WHITE);
Homepage();
}
}
}
}
}
}
}
//Zum Auslesen eine Zeile in der SD-karte
bool read_line(File& myFile)
{
static unsigned int index;
while (myFile.available())
{
char c = myFile.read();
if (c > 21 && index < 20 - 1)
{
buf1[index++] = c;
}
else if (c == '\n')
{
buf1[index] = '\0';
index = 0;
return true;
}
}
return false;
}
bool get_last_line(File& myFile)
{
if (myFile) //wenn Datei geöffnet
{
while (read_line(myFile)) //Zeile auslesen. Gibt false zurück wenn am Ende der Datei
;
return true;
}
return false;
}
void parse()
{
int value1 = atoi(strtok(buf1, ","));
// int value2 = atoi(strtok(NULL, ","));
// int value3 = atoi(strtok(NULL, ","));
// int value4 = atoi(strtok(NULL, ","));
Serial.println(F("\nvalues:"));
Serial.println(value1);
// Serial.println(value2);
// Serial.println(value3);
// Serial.println(value4);
}