#include <Arduino.h>
#include <U8g2lib.h>
//#include <MyDS3231.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <MechaQMC5883.h>
//#include <U8glib.h>
//#include <avr/wdt.h>
#include <I2CMux.h>
#define I2CMulti_Addr 0x70
I2CMux I2CMulti(I2CMulti_Addr);
#include <INA219_WE.h>
#define I2C_ADDRESS 0x40
INA219_WE ina219(I2C_ADDRESS);
//MechaQMC5883 qmc;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, SCL, SDA, U8X8_PIN_NONE); // All Boards without Reset of the Display
//U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // All Boards without Reset of the Display
//U8GLIB_SSD1306_128X64 u8g2(U8G_I2C_OPT_FAST); // Init of the OLED #1 using I2C
//U8GLIB_SSD1306_128X64 u8g2(U8G_I2C_OPT_FAST);
#define DS3231_ADDRESSE 0x68
#define drehzahl 150
File myFile;
int sensorwertlinks = 0;
int sensorwertrechts = 0;
int mitte = 0;
int x = 0;
int o = 0;
int relay = 3;
long e;
// Gleichstrommotor 1
int GSM1 = 10;
int in1 = 9;
int in2 = 8;
int ax, ay, az;
int azimuth;
void setup()
{
//ina219.init();
/* Set ADC Mode for Bus and ShuntVoltage
* Mode * * Res / Samples * * Conversion Time *
BIT_MODE_9 9 Bit Resolution 84 µs
BIT_MODE_10 10 Bit Resolution 148 µs
BIT_MODE_11 11 Bit Resolution 276 µs
BIT_MODE_12 12 Bit Resolution 532 µs (DEFAULT)
SAMPLE_MODE_2 Mean Value 2 samples 1,06 ms
SAMPLE_MODE_4 Mean Value 4 samples 2,13 ms
SAMPLE_MODE_8 Mean Value 8 samples 4,26 ms
SAMPLE_MODE_16 Mean Value 16 samples 8,51 ms
SAMPLE_MODE_32 Mean Value 32 samples 17,02 ms
SAMPLE_MODE_64 Mean Value 64 samples 34,05 ms
SAMPLE_MODE_128 Mean Value 128 samples 68,10 ms
*/
//ina219.setADCMode(SAMPLE_MODE_128); // choose mode and uncomment for change of default
/* Set measure mode
POWER_DOWN - INA219 switched off
TRIGGERED - measurement on demand
ADC_OFF - Analog/Digital Converter switched off
CONTINUOUS - Continuous measurements (DEFAULT)
*/
//ina219.setMeasureMode(CONTINUOUS); // choose mode and uncomment for change of default
//ina219.startSingleMeasurement();
/* Set PGain
* Gain * * Shunt Voltage Range * * Max Current *
PG_40 40 mV 0,4 A
PG_80 80 mV 0,8 A
PG_160 160 mV 1,6 A
PG_320 320 mV 3,2 A (DEFAULT)
*/
//ina219.setPGain(PG_320); // choose gain and uncomment for change of default
/* Set Bus Voltage Range
BRNG_16 -> 16 V
BRNG_32 -> 32 V (DEFAULT)
*/
//ina219.setBusRange(BRNG_32); // choose range and uncomment for change of default
//Serial.println("INA219 Current Sensor Example Sketch - Continuous");
/* If the current values delivered by the INA219 differ by a constant factor
from values obtained with calibrated equipment you can define a correction factor.
Correction factor = current delivered from calibrated equipment / current delivered by INA219
*/
//ina219.setCorrectionFactor(0.1); // insert your correction factor if necessary
//qmc.init();
SD.begin(4);
Wire.begin();
u8g2.begin();
u8g2.enableUTF8Print();
pinMode (relay, OUTPUT);
pinMode(GSM1, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
digitalWrite (relay, LOW);
u8g2.setFont(u8g2_font_6x10_tf);
//u8g2.setFont(u8g_font_unifont);
pinMode(A0, INPUT);//Sensor links
pinMode(A1, INPUT);//Sensor rechts
pinMode(A2, INPUT);//Sensor mitte
Serial.begin(9600);
}
void loop()
{
zeigeZeit(); // Zeit ausgeben
//delay(e);
}
// Temparutur auslesen
int tempDS3231() {
// Die Temperatur des DS3231 auslesen
float temp;
int msb, lsb;
Wire.beginTransmission(DS3231_ADDRESSE);
Wire.write(0x11); // DS3231 Register zu 11h
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESSE, 2); // 2 Byte Daten vom DS3231 holen
msb = Wire.read();
lsb = Wire.read();
temp=((msb << 2) + (lsb >> 6) ) /4.0;
return temp;
}
void leseDS3231zeit(byte *sekunde, byte *minute,byte *stunde, byte *wochentag, byte *tag, byte *monat, byte *jahr) {
Wire.beginTransmission(DS3231_ADDRESSE);
Wire.write(0); // DS3231 Register zu 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_ADDRESSE, 7); // 7 Byte Daten vom DS3231 holen
*sekunde = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*stunde = bcdToDec(Wire.read() & 0x3f);
*wochentag = bcdToDec(Wire.read());
*tag = bcdToDec(Wire.read());
*monat = bcdToDec(Wire.read());
*jahr = bcdToDec(Wire.read());
}
void zeigeZeit(){
/* float shuntVoltage_mV = 0.0;
float loadVoltage_V = 0.0;
float busVoltage_V = 0.0;
float current_mA = 0.0;
float power_mW = 0.0;
bool ina219_overflow = false;*/
byte sekunde, minute, stunde, wochentag, tag, monat, jahr;
leseDS3231zeit(&sekunde, &minute, &stunde, &wochentag, &tag, &monat, &jahr); // Daten vom DS3231 holen
Serial.print(sekunde);
int jetzt = minute;
int std = stunde;
int sec = sekunde;
int day = tag;
int month = monat;
int j = jahr;
/*if(jetzt >= 0)
{
shuntVoltage_mV = ina219.getShuntVoltage_mV();
busVoltage_V = ina219.getBusVoltage_V();
current_mA = ina219.getCurrent_mA();
power_mW = ina219.getBusPower();
loadVoltage_V = busVoltage_V + (shuntVoltage_mV/1000);
ina219_overflow = ina219.getOverflow();
}*/
I2CMulti.switchToBus(2);
u8g2.firstPage();
do {
u8g2.setCursor(0, 10);
switch (wochentag)
{
case 1: u8g2.print("Mo ");
break;
case 2: u8g2.print("Di ");
break;
case 3: u8g2.print("Mi ");
break;
case 4: u8g2.print("Do ");
break;
case 5: u8g2.print("Fr ");
break;
case 6: u8g2.print("Sa ");
break;
case 7: u8g2.print("So ");
}
if (tag < 10) { u8g2.print("0");}
u8g2.print(tag); // ausgeben T.M.J H:M:S
u8g2.print(".");
if (monat < 10) { u8g2.print("0");}
u8g2.print(monat);
u8g2.print(".");
u8g2.print(jahr);
u8g2.setCursor(0, 22);
if (stunde < 10) { u8g2.print("0");}
u8g2.print(stunde, DEC); // byte in Dezimal zur Ausgabe
u8g2.print(":");
if (minute < 10) { u8g2.print("0");}
u8g2.print(minute, DEC);
u8g2.print(":");
if (sekunde < 10) { u8g2.print("0"); }
u8g2.print(sekunde, DEC);
u8g2.setCursor(0, 34);
u8g2.print("Temp:");
u8g2.print(tempDS3231());
u8g2.print(" \260C");
/*
u8g2.setCursor(0,46);
u8g2.print("Bus Power[mW]: ");
u8g2.print(current_mA);
u8g2.setCursor(0,58);
u8g2.print("Load[V]: ");
u8g2.print(loadVoltage_V);
if(!ina219_overflow){
u8g2.setCursor(0,70);
//u8g2.print("Values OK - no overflow");
}
else{
u8g2.setCursor(0,70);
u8g2.print("Overflow! Choose higher PGAIN");
}*/
//qmc.read(&ax, &ay, &az, &azimuth);
//qmc.setMode(Mode_Continuous,ODR_200Hz,RNG_2G,OSR_256);
// azimuth = qmc.azimuth(&ay,&ax);
//u8g2.setCursor(0, 46);
// Serial.print("Richtung: ");
//Serial.println(azimuth);
/*int std = stunde;
int jetzt = minute;
int sec = sekunde;*/
/* if(std >= 22)
{
u8g2.sleepOn();
}
*/
if(std >= 6 && std < 21)
{
u8g2.sleepOff();
}
}
while( u8g2.nextPage() );