/*******************************************************************
//
// *****************************************************************
// ----------------< Configuration >--------------------------------
// Pins Arduino Pro Mini and compatible
// MController: Arduino
// Knotentyp: Slave Jalousie
// Interface: serial Ausgabe
// ----------------< sonstige Details >----------------------------
// Der Sketch verwendet 15634 Bytes (50%), Das Maximum sind 30720 Bytes.
// Globale Variablen verwenden 943 Bytes (46%). Das Maximum sind 2048 Bytes.
// Created with: manual ver. 1.3 19.02.2018 8:00
// Änderungen:
//
****************************************************************/
#include <SPI.h>
//#include <Ethernet2.h>
//#include <PubSubClient.h>
//#include <Bounce2.h>
#include <EEPROM.h>
#include <RBD_Timer.h>
#include <RBD_Button.h>
#include <PCF8574.h>
//-----------------EPROM Daten------------------------------------
//#define PUBLISH_DELAY 3000
//#define PUBLISH_ANZAHL 1
//#define Switch_Rollo1 0 //Jalousie Schalter 1
//#define Switch_Rollo2 1 //Jalousie Schalter 2
//---------Pindefinition Inputs und Outputs----------------
RBD::Button Switch_Rollo1(2, false); //BoardKlemme X1-1 Jalousie Taster 1
RBD::Button Switch_Rollo2(3, false); //BoardKlemme X1-2 Jalousie Taster 2
RBD::Button Switch_Kalibr(4, false); //Micro Switch on Board
RBD::Button Switch_Test(10); //Micro Switch on Board
//#define Contact_1 0 //BoardKlemme X1-3
//#define Contact_2 1 //BoardKlemme X1-4
PCF8574 pcf20(0x20);
#define RELAI_K1 5
#define RELAI_K2 6
#define RELAI_K3 7
#define RELAI_K4 8
//-----------------Hilfsvarieblen-----------------------
uint8_t state_J1 = 1;
uint8_t laststate_J1 = 0;
uint8_t state_J2 = 1;
uint8_t laststate_J2 = 0;
uint8_t instate_1 = 0;
uint8_t instate_2 = 0;
//-------- SPI Kommunikation -------------------------
volatile boolean received;
volatile byte Slavereceived, Slavesend;
//-----------eigen Funktionen-----------------------------
uint8_t rollo_runter(uint8_t, uint8_t);
uint8_t rollo_hoch(uint8_t, uint8_t);
void rollo_stopp(uint8_t, uint8_t);
uint8_t rollo_30p(uint8_t, uint8_t);
uint8_t rollo_50p(uint8_t, uint8_t);
uint8_t rollo_70p(uint8_t, uint8_t);
uint8_t r0llo_99p(uint8_t, uint8_t);
void rollo_kalibrierung();
//----------------- SETUP ------------------------------------
void setup() {
Serial.begin(115200);
//-------------------In- und Out-Puts------------------------
pinMode(RELAI_K1, OUTPUT); //Ein / Aus
pinMode(RELAI_K2, OUTPUT); //hoch / runter
pinMode(RELAI_K3, OUTPUT); //Ein / Aus
pinMode(RELAI_K4, OUTPUT); //hoch / runter
//pinMode(Contact_1, INPUT); //Fensterkontakt
//pinMode(Contact_2, INPUT); //Fensterkontakt
digitalWrite(RELAI_K1, LOW);
digitalWrite(RELAI_K3, LOW);
delay(300);
digitalWrite(RELAI_K2, LOW);
digitalWrite(RELAI_K4, LOW);
//----------------------SPI Bus---------------------------
//pinMode(PIN_SPI_SS, INPUT); //SS as Input for Slave Device
pinMode(PIN_SPI_MISO, OUTPUT); //MISO as Output for Slave Device
SPCR |= _BV(SPE); //Turn on SPI in Slave Mode by using SPI Control Register
received = false;
SPI.attachInterrupt(); //turn ON interrupt for SPI communication
//SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV4);
//----------------------EEPROM---------------------------
float ee_value_1 = 0.00f; //Variable to store data read from EEPROM.
float ee_value_2 = 0.00f; //Variable to store data read from EEPROM.
uint8_t eeAddress_1 = 0; //EEPROM address to start reading from
int eeAddress_2 = 0; //EEPROM address to start reading from
EEPROM.get(eeAddress_1, ee_value_1);
EEPROM.get(eeAddress_2, ee_value_2);
}
ISR(SPI_STC_vect) //Inerrrput routine function
{
Slavereceived = SPDR; // Value received from master if store in variable slavereceived
received = true; //Sets received as True
Serial.print("Von Master empfangen: ");
Serial.println(Slavereceived, HEX);
delay(1000);
}
//------------------------------------------------------------
void loop() {
//------ Nachricht vom Master-SPI----------------------
if (received) {
received = false; //reset
/*if ((RELAI_K1 != LOW)||(RELAI_K1 != LOW)) {
digitalWrite(RELAI_K1, LOW); //stopp
digitalWrite(RELAI_K3, LOW);
Serial.println("Rollos aus wegen Master Befehl");
delay(100);
} else {
}*/
switch (Slavereceived) {
case '0x10':
laststate_J1 = state_J1;
state_J1 = rollo_runter(RELAI_K1, RELAI_K2);
break;
case '0x11':
rollo_stopp(RELAI_K1, RELAI_K2); //stopp
if (laststate_J1 == 0) {
state_J1 = 2;
} else {
state_J1 = 0;
}
break;
case '0x12':
laststate_J1 = state_J1;
state_J1 = rollo_hoch(RELAI_K1, RELAI_K2);
break;
case '0x20':
laststate_J2 = state_J2;
state_J2 = rollo_runter(RELAI_K3, RELAI_K4);
break;
case '0x21':
rollo_stopp(RELAI_K3, RELAI_K4); //stopp
if (laststate_J2 == 0) {
state_J2 = 2;
} else {
state_J2 = 0;
}
break;
case '0x22':
laststate_J2 = state_J2;
state_J2 = rollo_hoch(RELAI_K3, RELAI_K4);
break;
}
}
//----------------------------Inputs lesen------------------------------
if (Switch_Kalibr.onPressed()) {
//rollo_kalibrierung();
Serial.println("Funtion KALIBRIERUNG ausgeloest");
//Serial.println(relay1);
}
else {}
if (Switch_Test.onPressed()) {
//rollo_kalibrierung();
Serial.println("Funtion TestSwitch ausgeloest");
//Serial.println(relay1);
}
else {}
//----------------- Fensterkontakt------------------------------
//if (digitalRead(Contact_1) == LOW) {
//---------- Jalousie 1 Input mit Taster----------------------
if (Switch_Rollo1.onPressed()) {
switch (state_J1) //state=0 runter, state=1 stopp, state=2 hoch.
{
case 0:
laststate_J1 = state_J1;
state_J1 = rollo_runter(RELAI_K1, RELAI_K2);
break;
case 1:
rollo_stopp(RELAI_K1, RELAI_K2); //stopp
if (laststate_J1 == 0) {
state_J1 = 2;
} else {
state_J1 = 0;
}
break;
case 2:
laststate_J1 = state_J1;
state_J1 = rollo_hoch(RELAI_K1, RELAI_K2);
break;
}
SPDR = state_J1;
} else {
}
//} else { //mqttclient.publish(Terassenfester ist auf); }
//----------------- Fensterkontakt------------------------------
//if (digitalRead(Contact_2) == LOW) {
//---------- Jalousie 2 Input mit Taster----------------
if (Switch_Rollo2.onPressed()) {
switch (state_J2) //state=0 runter, state=1 stopp, state=2 hoch.
{
case 0:
laststate_J2 = state_J2;
state_J2 = rollo_runter(RELAI_K3, RELAI_K4);
break;
case 1:
rollo_stopp(RELAI_K3, RELAI_K4); //stopp
if (laststate_J2 == 0) {
state_J2 = 2;
} else {
state_J2 = 0;
}
break;
case 2:
laststate_J2 = state_J2;
state_J2 = rollo_hoch(RELAI_K3, RELAI_K4);
break;
}
SPDR = state_J2;
} else {
}
}
void spireceive(uint8_t address, uint8_t value) {
// take the SS pin low to select the chip:
digitalWrite(PIN_SPI_SS, LOW);
delay(10);
// send in the address and value via SPI:
SPI.transfer(address);
SPI.transfer(value);
delay(10);
// take the SS pin high to de-select the chip:
digitalWrite(PIN_SPI_SS, HIGH);
}
void spitransfer(uint8_t address, uint8_t value) {
// take the SS pin low to select the chip:
digitalWrite(PIN_SPI_SS, LOW);
delay(10);
// send in the address and value via SPI:
SPI.transfer(address);
SPI.transfer(value);
delay(10);
// take the SS pin high to de-select the chip:
digitalWrite(PIN_SPI_SS, HIGH);
}
uint8_t rollo_runter(uint8_t relay1, uint8_t relay2) {
uint8_t status = 1;
if (relay1 != LOW) {
digitalWrite(relay1, LOW);
delay(100);
}
digitalWrite(relay2, LOW); //runter
delay(100);
digitalWrite(relay1, HIGH);
delay(100);
Serial.print("Funtion RUNTER ausgeloest, Relai: ");
Serial.println(relay1);
return status;
}
uint8_t rollo_hoch(uint8_t relay1, uint8_t relay2) {
uint8_t status = 1;
if (relay1 != LOW) {
digitalWrite(relay1, LOW);
delay(100);
}
digitalWrite(relay2, HIGH); //hoch
delay(100);
digitalWrite(relay1, HIGH);
delay(100);
Serial.print("Funtion HOCH ausgeloest, Relai: ");
Serial.println(relay1);
return status;
}
void rollo_stopp(uint8_t relay1, uint8_t relay2) {
digitalWrite(relay1, LOW); //stopp
delay(100);
digitalWrite(relay2, LOW);
Serial.print("Funtion STOPP ausgeloest, Relai: ");
Serial.println(relay1);
}
uint8_t rollo_30p(uint8_t, uint8_t);
uint8_t rollo_50p(uint8_t, uint8_t);
uint8_t rollo_70p(uint8_t, uint8_t);
uint8_t r0llo_99p(uint8_t, uint8_t);