Dann kommt der nächste der mault weil er Copy Paste machen muss aber gut
#include <DS3232RTC.h>
#include <avr/sleep.h>
#include <SD.h> //SD Library hinzufügen
#include <DallasTemperature.h>
#include <OneWire.h>
#include <Wire.h>
#define interruptPin 2 //Pin we are going to use to wake up the Arduino
#define ONE_WIRE_BUS 3 // pin an Arduino Temp Sensoren
int sdPWR = 5;
int sensorPWR = 6;
int sensorFehler = 7;
float a = 0; // Variable für Temperaturen
float b = 0;
const int chipSelect = 4; //Chip Pin für die SD Karte(bei UNO 4,bei MEGA 53)
//defines von temp code
#define DS18B20_Aufloesung 12
DeviceAddress DS18B20_Adressen;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature myDS18B20(&oneWire);
#define Anzahl_Sensoren_DS18B20 2 // Anzahl der angeschlossenen Sensoren - Mögliche Werte: '0','1','2'
const float No_Val = 999.99;
float Temperatur[2] = {No_Val, No_Val};
void setup()
{
pinMode(sdPWR, OUTPUT);
pinMode(sensorFehler,OUTPUT);
pinMode(sensorPWR,OUTPUT);
digitalWrite(sdPWR,HIGH);
digitalWrite(sensorPWR,HIGH);
pinMode(interruptPin,INPUT_PULLUP);//Set pin d2 to input using the buildin pullup resistor
Serial.begin(9600);
delay(3000); // wait for console opening
setSyncProvider(RTC.get);
if (timeStatus() != timeSet)
{
Serial.println("Unable to sync with the RTC");
}
else
{
Serial.println("RTC has set the system time");
}
pinMode (13, OUTPUT);
if (startSDCard() == true) // Durch den Rückgriff auf den Programmblock "startSDCard" wird die SD-Karte geprüft. Wenn die SD Karte gelesen werden kann dann soll die onboard-LED an Pin13 zweimal blinken
{
digitalWrite(sensorFehler, HIGH); //an
delay(500);
digitalWrite(sensorFehler, LOW); //aus Brauchbar / bleibt
delay(500);
digitalWrite(sensorFehler, HIGH); //an
delay(500);
digitalWrite(sensorFehler, LOW); //aus
delay(500);
}
Serial.println("DS18B20 Test");
Serial.println();
delay(500);
Serial.print("Anzahl aktivierter Sensoren: ");
Serial.println(Anzahl_Sensoren_DS18B20);
if ((Anzahl_Sensoren_DS18B20 > 0) and (Anzahl_Sensoren_DS18B20 < 3))
{
myDS18B20.begin();
Serial.print("Anzahl angeschlossener Sensoren: ");
Serial.println(myDS18B20.getDeviceCount(), DEC);
Serial.println();
if(Anzahl_Sensoren_DS18B20 != myDS18B20.getDeviceCount())
{
digitalWrite(sensorFehler,HIGH);
}
}
for (byte i = 0 ; i < myDS18B20.getDeviceCount(); i++)
{
if (myDS18B20.getAddress(DS18B20_Adressen, i))
{
myDS18B20.setResolution(DS18B20_Adressen, DS18B20_Aufloesung);
}
}
// initialize the alarms to known values, clear the alarm flags, clear the alarm interrupt flags
RTC.setAlarm(ALM1_MATCH_DATE, 0, 0, 0, 1);
RTC.setAlarm(ALM2_MATCH_DATE, 0, 0, 0, 1);
RTC.alarm(ALARM_1);
RTC.alarm(ALARM_2);
RTC.alarmInterrupt(ALARM_1, false);
RTC.alarmInterrupt(ALARM_2, false);
RTC.squareWave(SQWAVE_NONE);
/*
Uncomment the block block to set the time on your RTC. Remember to comment it again
otherwise you will set the time at everytime you upload the sketch
/
/* Begin block
tmElements_t tm;
tm.Hour = 00; // set the RTC to an arbitrary time
tm.Minute = 00;
tm.Second = 00;
tm.Day = 4;
tm.Month = 2;
tm.Year = 2018 - 1970; // tmElements_t.Year is the offset from 1970
RTC.write(tm); // set the RTC from the tm structure
Block end * */
time_t t; //create a temporary time variable so we can set the time and read the time from the RTC
t = RTC.get(); //Gets the current time of the RTC
RTC.setAlarm(ALM1_MATCH_MINUTES , 0, 30, 0, 0); // halb stunden messintervall
// clear the alarm flag
RTC.alarm(ALARM_1);
// configure the INT/SQW pin for "interrupt" operation (disable square wave output)
RTC.squareWave(SQWAVE_NONE);
// enable interrupt output for Alarm 1
RTC.alarmInterrupt(ALARM_1, true);
}
void loop()
{
delay(5000);
schlafmodus();
}
void schlafmodus()
{
sleep_enable(); //Enabling sleep mode
attachInterrupt(0, wakeUp, LOW); //attaching a interrupt to pin d2
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Setting the sleep mode, in our case full sleep
time_t t; // creates temp time variable
t = RTC.get(); //gets current time from rtc
Serial.println("Sleep Time: " + String(hour(t)) + ":" + String(minute(t)) + ":" + String(second(t))); //prints time stamp on serial monitor
digitalWrite(sdPWR,LOW);
digitalWrite(sensorPWR,LOW);
delay(2000); //wait a second to allow the led to be turned off before going to sleep
sleep_cpu(); //activating sleep mode
Serial.println("neue messung");//next line of code executed after the interrupt
delay(200);
if ((Anzahl_Sensoren_DS18B20 > 0) and (Anzahl_Sensoren_DS18B20 < 3))
{
myDS18B20.requestTemperatures();
}
for (byte i = 0 ; i < Anzahl_Sensoren_DS18B20; i++)
{
if (i < myDS18B20.getDeviceCount())
{
Serial.print("Sensor ");
Serial.print(i + 1);
Serial.print(": ");
Temperatur[i] = myDS18B20.getTempCByIndex(i);
if (Temperatur[i] == DEVICE_DISCONNECTED_C)
{
Temperatur[i] = No_Val;
Serial.println("Fehler");
digitalWrite(sensorFehler, HIGH);
}
else
{
Serial.print(Temperatur[i]);
Serial.println(" 'C");
if(sensorFehler != LOW)
{
digitalWrite(sensorFehler,LOW);
}
}
}
}
delay(500);
if (startSDCard() == true) // Durch den Rückgriff auf den Programmblock "startSDCard" wird die SD-Karte geprüft. Wenn die SD Karte gelesen werden kann dann soll die onboard-LED an Pin13 zweimal blinken
{
delay(1000);
schreiben_SD();
delay(1000);
}
t = RTC.get();
Serial.println("Sleep Time: " + String(hour(t)) + ":" + String(minute(t))); //prints time stamp on serial monitor
RTC.setAlarm(ALM1_MATCH_MINUTES , 0, 30, 0, 0);
// clear the alarm flag
RTC.alarm(ALARM_1);
}
boolean startSDCard() // Dieser Programmblock wird benötigt, um zu prüfen, ob die SD-Karte einsatzbereit ist.
{
boolean result = false;
pinMode(4, OUTPUT); // 4 bei UNO, bei MEGA in 53 ändern
if (!SD.begin(chipSelect)) //Überprüfen ob die SD Karte gelesen werden kann
{
result = false;
}
else // Wenn ja Datei wie im Loop anlegen
{
File dataFile = SD.open("datalog.csv", FILE_WRITE);
if (dataFile)
{
dataFile.close();
result = true;
}
}
return result;
}
void printDigits(int digits)
{
if (digits < 10)
Serial.print("0");
Serial.print(digits);
}