/* This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details. */

#include <SparkFun_SCD30_Arduino_Library.h>
#include <Wire.h>
#include <rgb_lcd.h>
#include <Adafruit_NeoPixel.h>

int CO2 = 0 ;
//Reading CO2, humidity and temperature from the SCD30 By: Nathan Seidle SparkFun Electronics 

//https://github.com/sparkfun/SparkFun_SCD30_Arduino_Library

SCD30 airSensorSCD30; // Objekt SDC30 Umweltsensor
int Luftfeuchte = 0 ;
int Temp = 0 ;
//LCD RGB, 2013 Copyright (c) Seeed Technology Inc.   Author:Loovee
rgb_lcd lcd;

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(2,13,NEO_GRBW + NEO_KHZ800);

void setup(){ // Einmalige Initialisierung
  Wire.begin(); // ---- Initialisiere den I2C-Bus 

  if (Wire.status() != I2C_OK) Serial.println("Something wrong with I2C");

  if (airSensorSCD30.begin() == false) {
    Serial.println("The SCD30 did not respond. Please check wiring."); 
    while(1) {
      yield(); 
      delay(1);
    } 
  }

  airSensorSCD30.setAutoSelfCalibration(false); // Sensirion no auto calibration

  airSensorSCD30.setMeasurementInterval(2);     // CO2-Messung alle 5 s

  Serial.begin(115200);
  lcd.begin(16, 2);// LCD Backlight initialisieren 

  pixels.begin();//-------------- Initialisierung Neopixel
  delay(1);
  pixels.show();
  pixels.setPixelColor(0,0,0,0,0); // alle aus
  pixels.setPixelColor(1,0,0,0,0);
  pixels.show();                 // und anzeigen

  Serial.println();
  Wire.setClock(100000L);            // 100 kHz SCD30 
  Wire.setClockStretchLimit(200000L);// CO2-SCD30
}

void loop() { // Kontinuierliche Wiederholung 
  CO2 = airSensorSCD30.getCO2() ;
  Luftfeuchte = airSensorSCD30.getHumidity() ;
  Temp = airSensorSCD30.getTemperature() ;
  lcd.setCursor(0,0);
  lcd.print(String("CO2:"+String(String(airSensorSCD30.getCO2())))+"                ");
  lcd.setCursor(0,1);
  lcd.print(String("Temperatur:"+String(String(airSensorSCD30.getTemperature())))+"                ");
  if (( ( CO2 ) < ( 700 ) ))
  {
    pixels.setPixelColor(0,0,30,0,0);
    pixels.show();
  }
  else
  {
    if (( ( CO2 ) < ( 1000 ) ))
    {
      pixels.setPixelColor(0,30,30,0,0);
      pixels.show();
    }
    else
    {
      pixels.setPixelColor(0,40,0,0,0);
      pixels.show();
    }
  }
  if (( ( Temp ) < ( 20 ) ))
  {
    pixels.setPixelColor(1,40,0,0,0);
    pixels.show();
  }
  else
  {
    if (( ( Temp ) < ( 24 ) ))
    {
      pixels.setPixelColor(1,30,30,0,0);
      pixels.show();
    }
    else
    {
      pixels.setPixelColor(1,0,30,0,0);
      pixels.show();
    }
  }
  Serial.print("CO2_"+String(String(CO2)));
  Serial.println();
  Serial.print("Temp_"+String(String(Temp)));
  Serial.println();
  Serial.print("Luftfeuchte_"+String(String(Luftfeuchte)));
  Serial.println();
  delay( 1000 );
}
