So , habe das mal hingefummelt . Kann ja ein Programmierer almasys oder so noch mal drüber schauen .. ich bin kein Programmiere , ich Fummel das nur mit Try and error anhand anderer Beispiele hin . Es scheint aber zu funktionieren
Der Sketch sieht jetzt so aus :
C
// um die RTC zu stellen den Sketch Datei - Beispiele - RTC - DS3231 - DS3231_Adjust einspielen
const bool debug=false;
// #define CO2OFFSET -60.0 // if necessary you can add here an offset for meassured co2 values
// #define TEMPOFFSET 0.0
// #define HUMOFFSET 0.0
bool daymode;
// #define DAYLIMIT 5 //Tag-/Nachtschwelle
const int sensorintervall=2; //Sensorintervall in Sekunden
typedef struct {
uint16_t session; // startup count
float co2offset, // CO2 offset
tempoffset, // temperature offset
humoffset, // humidity offset
luxlimit; // min lux for day mode
uint8_t daynight_auto; // switch automatically between day/night if luminiscence sensor present
bool daymode; // true for day, false for night (manual/no sensor present)
uint32_t _crc; // settings checksum
bool menuvsbl; //menu visible?
} settings_t;
settings_t SETTINGS;
#include <avr/wdt.h>
#include <EEPROM.h>
#include <Wire.h>
#include "SparkFun_SCD30_Arduino_Library.h" //Click here to get the library: http://librarymanager/All#SparkFun_SCD30
SCD30 airSensor;
//float co2=0;float temp=0;float humidity=0;
//die Adresse des Displays wird in ~/Arduino/libraries/ssd1306/src/intf/i2c/ssd1306_i2c_conf.h Zeile 47 festgelegt.
#include "ssd1306.h"
char str_temp[6];
char buf[10];
char clockbuf[]=" ";
#include "myFont.h"
#include "LEDRing.h"
LEDRing ledring;
const byte dayr[]={0,0,0,0, 0,0,0,0, 2,4,6,7, 7,7,7,7};
const byte dayg[]={7,7,7,7, 7,7,7,7, 6,5,4,3, 3,2,1,0};
const byte dayb[]={0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
const byte nightr[]={0,0,0,0, 0,0,0,0, 2,2,2,2, 2,2,2,2};
const byte nightg[]={2,2,2,2, 2,2,2,2, 2,2,2,2, 0,0,0,0};
const byte nightb[]={0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0};
#include <RTC.h>
static DS3231 RTC;
boolean rtc;
#include <Wire.h>
//#include <BH1750.h>
#include <Max44009.h>
Max44009 lightMeter(0x4A); // gy-49 einbinden
bool luxsensor;
void setup()
{
wdt_enable(WDTO_8S);
Serial.begin(115200);
Serial.println("CO2-Raumluftwaechter Reboot");
Wire.begin();
EEPROM.get(0, SETTINGS);
daymode=SETTINGS.daymode;
/* unsigned int session= EEPROM.read(0) //Lower-
|(EEPROM.read(1)<<8); //+ Upperbyte
session++;
Serial.print("Session ");Serial.println(session);
EEPROM.update(0,session);
EEPROM.update(1,session>>8);*/
SETTINGS.session++;
EEPROM.put(0, SETTINGS);
if (!SETTINGS.menuvsbl) {Serial.println(F("Press [M] for menu."));}
pinMode(13,OUTPUT);
cleari2c();
while (!airSensor.begin()) {
Serial.println(F("Air sensor not detected. Please check wiring."));
digitalWrite(13,HIGH);delay(100);digitalWrite(13,LOW);delay(900);
}
pinMode(13,INPUT);
cleari2c();
ssd1306_128x64_i2c_init();
ssd1306_setFixedFont(myFont);
cleari2c();
ssd1306_clearScreen();
ssd1306_negativeMode();
cleari2c();
/*ssd1306_printFixed(0,8," Session # ",STYLE_NORMAL);
dtostrf(SETTINGS.session, 5, 0, str_temp);
sprintf(buf, "%s", str_temp);*/
cleari2c();
ssd1306_printFixed(55+42, 8, buf, STYLE_NORMAL);
ssd1306_positiveMode();
cleari2c();
ssd1306_printFixed( 2, 24, "Temp.", STYLE_NORMAL);
char co2[]={'C','O',126,0};
cleari2c();
ssd1306_printFixed( 6+32+11, 24, co2, STYLE_NORMAL);
cleari2c();
ssd1306_printFixed( 94, 24, "Hum.", STYLE_NORMAL);
char gradc[]={' ',' ',' ',127,'C',' ',0};
cleari2c();
ssd1306_printFixed( 2, 55, gradc, STYLE_NORMAL);
cleari2c();
ssd1306_printFixed( 6+32+1+24, 55, "ppm", STYLE_NORMAL);
cleari2c();
ssd1306_printFixed( 94+24, 55, "%", STYLE_NORMAL);
//ssd1306_drawLine(0,48, 127, 48);
ledring.init();
rtc=RTC.begin();
if (rtc) {report(F("RTC vorhanden\n"));} else {report(F("keine RTC\n"));}
// luxsensor=lightMeter.getError();
// luxsensor=false; //Der Lichtsensor startet oft mit Verzoegerung, wird deshalb im loop() initialisiert
// if (luxsensor) {Serial.println("Luxsensor gefunden.");delay(5000);}
//Serial.print("Start max44009_test01 : ");
// Serial.println(MAX44009_LIB_VERSION);
// Wire.begin();
// Wire.setClock(100000);
}
void loop()
{
wdt_reset();
static long nextturn=millis();
nextturn+=100;
//Serial.print("Idle: ");Serial.print(nextturn-millis());Serial.println(" ms, ");
while (millis()<nextturn) {};
// bool luxsenor auf false oder true setzen damit das Programm wieder funktioniert
int err = lightMeter.getError();
if (err != 0)
{
luxsensor=false;
}
else
{
luxsensor=true;
}
// if (!luxsensor) {luxsensor=lightMeter.getError();}
//int luxsensor=lightMeter.getError();
if (luxsensor) {
//Serial.print("Luxsensor wird gelesen: ");
//float lux=lightMeter.readLightLevel();
float lux=lightMeter.getLux(); // neuer parameter getLux für Max44009
//Serial.println(lux);
if (SETTINGS.luxlimit>lux) {daymode=false;} else {daymode=true;}
}
if (daymode) {Serial.println("Tag");} else {Serial.println("Nacht");}
ledring.daymode=daymode;
ledring.poll();
trennlinien();
cleari2c();
if (airSensor.dataAvailable()) {
if (SETTINGS.menuvsbl) {Serial.print("\x1b\x5b\x48\x1b\x5b\x32\x4a\x1b\x5b\x33\x4a");}
static float lastco2=-1701;
//float co2=int((airSensor.getCO2()+CO2OFFSET)*10)/10.0;
cleari2c();
float co2=airSensor.getCO2()+SETTINGS.co2offset;
//co2=380;
if (co2>9999) {co2=9999;} //5stellige Werte bringen die Displaydarstellung durcheinander
Serial.print(F("co2(ppm):"));Serial.print(co2);
static float lasttemp=-1701;
cleari2c();
float temp=int((airSensor.getTemperature()+SETTINGS.tempoffset)*10)/10.0;
Serial.print(F(" temp(°C):"));Serial.print(temp);
static float lasthumidity=-1701;
cleari2c();
float humidity=int((airSensor.getHumidity()+SETTINGS.humoffset)*10)/10.0;
Serial.print(F(" humidity(%):"));Serial.print(humidity);
Serial.println(" ");
ssd1306_setFixedFont(ssd1306xled_font8x16);ssd1306_positiveMode();
//char str_temp[6];char buf[80];
if (lasttemp!=temp) {
report(F("Temperatur hat sich geaendert."));
dtostrf(temp, 4, 1, str_temp);
sprintf(buf, "%s", str_temp);
cleari2c();
ssd1306_printFixed( 2, 32, buf, STYLE_NORMAL);
lasttemp=temp;
}
if (lastco2!=co2) {
report(F("CO2 hat sich geaendert."));
dtostrf(co2, 4, 0, str_temp);
sprintf(buf, "%s", str_temp);
cleari2c();
ssd1306_printFixed(6+32+11, 32, buf, STYLE_NORMAL);
lastco2=co2;
}
if (lasthumidity!=humidity) {
report(F("Luftfeuchtigkeit hat sich geaendert."));
dtostrf(humidity, 4, 1, str_temp);
sprintf(buf, "%s", str_temp);
cleari2c();
ssd1306_printFixed(94, 32, buf, STYLE_NORMAL);
lasthumidity=humidity;
}
if (rtc) {
byte stunde=RTC.getHours();
byte minute=RTC.getMinutes();
char ziffern[]=" 123456789";
clockbuf[ 0]=ziffern[stunde/10];
ziffern[0]='0';
clockbuf[ 1]=ziffern[stunde%10];
clockbuf[ 2]=':';
clockbuf[ 3]=ziffern[minute/10];
clockbuf[ 4]=ziffern[minute%10];
//buf[5]=0;
byte tag=RTC.getDay();
byte monat=RTC.getMonth();
int jahr=RTC.getYear();
ziffern[0]=' ';
clockbuf[11]=ziffern[tag/10];
ziffern[0]='0';
clockbuf[12]=ziffern[tag%10];
clockbuf[13]='.';
clockbuf[14]=ziffern[monat/10];
clockbuf[15]=ziffern[monat%10];
clockbuf[16]='.';
int jahrtausend=jahr/1000;
clockbuf[17]=ziffern[jahrtausend];
jahr=jahr-jahrtausend*1000;
int jahrhundert=jahr/100;
clockbuf[18]=ziffern[jahrhundert];
jahr=jahr-jahrhundert*100;
clockbuf[19]=ziffern[jahr/10];
clockbuf[20]=ziffern[jahr%10];
drawclock();
}
ledring.flash=co2>2000;
float level=(co2-400)/(700/16);
report(F("Level "));report(String(level));report("\n");
if (level<16) {
if (daymode) {
for (int n=0;n<16;n++) { ledring.r[n]=dayr[n];ledring.g[n]=dayg[n];ledring.b[n]=dayb[n]; }
int partialled=int(level);
if (debug) {Serial.print("BruchLED: ");Serial.println(partialled);}
float partialintensity=level-partialled;
ledring.r[partialled]*=partialintensity;
ledring.g[partialled]*=partialintensity;
ledring.b[partialled]*=partialintensity;
if (debug) {Serial.print(ledring.r[partialled]);Serial.print(ledring.g[partialled]);Serial.println(ledring.b[partialled]);}
if (debug) {Serial.print(ledring.r[partialled]);Serial.print(ledring.g[partialled]);Serial.println(ledring.b[partialled]);}
for (int offled=partialled+1;offled<16;offled++) { ledring.r[offled]=0;ledring.g[offled]=0;ledring.b[offled]=0; }
if (partialled>11) {
ledring.r[ 0]=7;ledring.g[ 0]=3;ledring.b[ 0]=0;
ledring.r[ 1]=7;ledring.g[ 1]=3;ledring.b[ 1]=0;
ledring.r[ 2]=7;ledring.g[ 2]=3;ledring.b[ 2]=0;
ledring.r[ 3]=7;ledring.g[ 3]=3;ledring.b[ 3]=0;
ledring.r[ 4]=7;ledring.g[ 4]=3;ledring.b[ 4]=0;
ledring.r[ 5]=7;ledring.g[ 5]=3;ledring.b[ 5]=0;
ledring.r[ 6]=7;ledring.g[ 6]=3;ledring.b[ 6]=0;
ledring.r[ 7]=7;ledring.g[ 7]=3;ledring.b[ 7]=0;
ledring.r[ 8]=7;ledring.g[ 8]=3;ledring.b[ 8]=0;
ledring.r[ 9]=7;ledring.g[ 9]=3;ledring.b[ 9]=0;
ledring.r[10]=7;ledring.g[10]=3;ledring.b[10]=0;
ledring.r[11]=7;ledring.g[11]=3;ledring.b[11]=0;
}
}
else {
for (int n=0;n<16;n++) {
if (n==int(level)) { ledring.r[n]=nightr[n];ledring.g[n]=nightg[n];ledring.b[n]=nightb[n]; }
else { ledring.r[n]=0 ;ledring.g[n]=0 ;ledring.b[n]=0; }
}
}
}
else {
if (daymode) { for (int p=0;p<16;p++) { ledring.r[p]=7;ledring.g[p]=0;ledring.b[p]=0; } }
else { for (int p=0;p<16;p++) {
ledring.r[p]=bool((p==4)||(p==12))*2;
ledring.g[p]=0;
ledring.b[p]=0;
}
}
}
airSensor.setMeasurementInterval(sensorintervall);
}
else {report(F("Waiting for new data\n"));}
menu();
if (debug) {
static byte progress;
progress=(progress+1)&B11;
ssd1306_setFixedFont(myFont);
cleari2c();
switch (progress) {
case 0:{ssd1306_printFixed(0,0, "-", STYLE_NORMAL);} break;
case 1:{ssd1306_printFixed(0,0,"\\", STYLE_NORMAL);} break;
case 2:{ssd1306_printFixed(0,0, "|", STYLE_NORMAL);} break;
case 3:{ssd1306_printFixed(0,0, "/", STYLE_NORMAL);} break;
}
}
}
void report(String text)
{
if (debug) {Serial.print(text);}
}
void cleari2c()
{
for (int n=1;n<=20;n++) {
pinMode(A5,OUTPUT);
pinMode(A5, INPUT);
}
}
Alles anzeigen
und im Anhang noch mal als ins angehängt .
Man braucht auch noch eine Library dafür
Danach konnte alles ordentlich geflasht werden .