#include <Arduino.h>
#include <Wire.h>
#include <stdbool.h>
#include <stdint.h>
#include <LiquidCrystal_I2C.h>
// Define the Pins for Encoder
#define Encoder_A 2 //CLK
#define Encoder_B 4 //DT
#define Encoder_C 3//SW
// Define pins for Relay inputs
#define buttonPin1 8
uint8_t buttonState1 = 0; // variable for reading the pushbutton status
constexpr uint8_t IN11 = 9; // Modul 1, 1.st relay //Start
constexpr uint8_t IN12 = 10; // Modul 1, 2.st relay //F1
constexpr uint8_t IN21 = 11; // Modul 2, 1.st relay //UP Arrow
constexpr uint8_t IN22 = 12; // Modul 2, 1.st relay //Down Arrow
// Define for Relay
#define ON 1
#define OFF 0
// Variable for Siebdrcuck relays
float t_deff = 0.00;
double PrintingTime = 4; // Time that maschine needs for printing one layer
double CuringTime = 0; //2 // Time for curing one layer
double OnePrintingProcesstime = (PrintingTime + CuringTime);// Total time between to prints
//int OffContactCount = 4; // Number of Off contact that needs to be increased
int OffContact = 0 ; //3
int OffContactNumToPrint = 0;
int TotalPrintingCount = 0; //4 // Number of total layer to be printed
int ActualPrintingCount = 0; //Number of actual printed layer until Stop of Process
int PartialPrintingCount = 0; //4 /* Number of layers to be printed until OFF contact needs to be increased*/
// Variable for LCD
int drehzahl = 0;
int drehzahl1 = 0;
int drehzahl2 = 0;
int drehzahl3 = 0;
int drehzahl4 = 0;
int NextStatusDrehzahl = 0;
int Curserplatz=0;
int FlagforCurser = 0;
int FlagforButton = 1;
LiquidCrystal_I2C lcd(0x27, 16, 4); // I2C address 0x27, 16 column and 2 rows
int lastClk = HIGH;
// Functions*************************
// Delay Function
void delaySelbst(double delayTime) // the Output is in second
{
for ( int i = 0 ; i < (20*delayTime) ; i++){
delay(50);
}
}
//set the status of relays
void relay_SetStatus( unsigned char
status_1, unsigned char status_2, unsigned char status_3, unsigned char status_4)
{
digitalWrite(IN11, status_1);
digitalWrite(IN12, status_2);
digitalWrite(IN21, status_3);
digitalWrite(IN22, status_4);
}
//initialize the relay
void relay_init()
{
//set all the relays OUTPUT
pinMode(IN11, OUTPUT);
pinMode(IN12, OUTPUT);
pinMode(IN21, OUTPUT);
pinMode(IN22, OUTPUT);
relay_SetStatus( OFF, OFF , OFF , OFF );
//turn off all the relay
}
void Siebdruck(){
float t_start_funk = millis();
for (int a = 0 ; a < TotalPrintingCount ; a++)
{
//relay_init(); //initialize the relay
for (int b = 0 ; b < PartialPrintingCount ; b++)
{
relay_init(); //initialize the relay
//Start
relay_SetStatus(ON,OFF,OFF,OFF);
delaySelbst(0.5); // the time for Relay start and stop
relay_SetStatus(OFF,OFF,OFF,OFF);
delaySelbst(OnePrintingProcesstime);
++ActualPrintingCount;
}
Serial.println("ActualPrintingCount = ");
Serial.println(ActualPrintingCount);
//if (ActualPrintingCount =! TotalPrintingCount)
{
//F1
relay_SetStatus(OFF,ON,OFF,OFF);
delaySelbst(0.5); // the time for Relay start and stop
relay_SetStatus(OFF,OFF,OFF,OFF);
delaySelbst(0.5);
Serial.println("ertes,F1 Gedruckt");
for (int c = 0 ; c < OffContact ; c++)
{
//UP ARROW
relay_SetStatus(OFF,OFF,ON,OFF);
delaySelbst(0.5); // the time for Relay start and stop
relay_SetStatus(OFF,OFF,OFF,OFF);
delaySelbst(0.5);
++OffContactNumToPrint;
}
}
}
float t_end_funk = millis();
t_deff = (t_end_funk - t_start_funk);
Serial.println("ActualPrintingCount =");
Serial.println(ActualPrintingCount);
Serial.println("OffContactCount =");
Serial.println(OffContactNumToPrint);
Serial.println("ActualPrintingTime =");
Serial.println(t_deff);
//F1
relay_SetStatus(OFF,ON,OFF,OFF);
delaySelbst(0.5); // the time for Relay start and stop
relay_SetStatus(OFF,OFF,OFF,OFF);
delaySelbst(0.5);
Serial.println("F1 Gedruckt");
for (int d = 0 ; d < OffContactNumToPrint ; d++)
{
//Down ARROW
relay_SetStatus(OFF,OFF,OFF,ON);
delaySelbst(0.5); // the time for Relay start and stop
relay_SetStatus(OFF,OFF,OFF,OFF);
delaySelbst(0.5);
}
}
void setup() {
Serial.begin(9600);
relay_init();
// Setup part for Drehgeber and LCD
pinMode(Encoder_A , INPUT);
pinMode(Encoder_B , INPUT);
pinMode(Encoder_C , INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(Encoder_A) , readEncoder , FALLING);
attachInterrupt(digitalPinToInterrupt(Encoder_C) , setButtonDrehgeber, CHANGE);
//attachInterrupt(digitalPinToInterrupt(buttonPin1) , Button , CHANGE);
lcd.init(); // initialize the lcd
lcd.backlight();
}
void loop() {
// Loop part for Drehgeber and LCD
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Total:");
lcd.print(drehzahl1);
lcd.setCursor(0,1);
lcd.print("Partial:");
lcd.print(drehzahl2);
lcd.setCursor(0,2);
lcd.print("Offcontact:");
lcd.print(drehzahl3);
lcd.setCursor(0,3);
lcd.print("Curring Time:");
lcd.print(drehzahl4);
lcd.setCursor(12,0);
lcd.print("t:");
lcd.print((t_deff)/60000);
if (Curserplatz == 0 && FlagforCurser == 10)
{
lcd.setCursor(6,Curserplatz);//9,0//7,1//12,2//14,3
lcd.print(NextStatusDrehzahl);
}
if (Curserplatz == 0 && FlagforCurser == 0)
{
lcd.setCursor(6,Curserplatz);//9,0//7,1//12,2//14,3
lcd.print("-");
}
if (Curserplatz == 1 && FlagforCurser == 10)
{
lcd.setCursor(8,Curserplatz);
lcd.print(NextStatusDrehzahl);
}
if (Curserplatz == 1 && FlagforCurser == 0)
{
lcd.setCursor(8,Curserplatz);
lcd.print("-");
}
if (Curserplatz == 2 && FlagforCurser == 10)
{
lcd.setCursor(11,Curserplatz);
lcd.print(NextStatusDrehzahl);
}
if (Curserplatz == 2 && FlagforCurser == 0)
{
lcd.setCursor(11,Curserplatz);
lcd.print("-");
}
if (Curserplatz == 3 && FlagforCurser == 10)
{
lcd.setCursor(13,Curserplatz);
lcd.print(NextStatusDrehzahl);
}
if (Curserplatz == 3 && FlagforCurser == 0)
{
lcd.setCursor(13,Curserplatz);
lcd.print("-");
}
delay(500);
if( TotalPrintingCount > ActualPrintingCount)
{
// put your main code here, to run repeatedly:
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
for (int j=0 ; j < 10 ; j++)
{
delay(100);
if (buttonState1){
Serial.println("Button gedruckt");
Siebdruck();}
else
relay_init();
break;
}
}
}
// Functions for the Drehgeber and LCD
int readEncoder()
{
int newClk = digitalRead(Encoder_A);
{
lastClk = newClk;
int dtValue = digitalRead(Encoder_B);
if (dtValue == HIGH && newClk ==LOW)
{
Serial.println("Rotated Clockwise -->> ");
drehzahl = drehzahl +1;
NextStatusDrehzahl = drehzahl;
if (digitalRead(Encoder_C) == HIGH && FlagforCurser == 0)
{
++Curserplatz;
if (Curserplatz > 3)
{
Curserplatz=0;
}
}
Serial.println(NextStatusDrehzahl);
}
if (dtValue == LOW && newClk ==LOW)
{
Serial.println("Rotated counterClockwise <<-- ");
drehzahl = drehzahl -1;
NextStatusDrehzahl = drehzahl;
if (digitalRead(Encoder_C) == HIGH && FlagforCurser == 0)
{
--Curserplatz;
if (Curserplatz < 0)
{
Curserplatz = 4 + Curserplatz;
}
}
Serial.println(NextStatusDrehzahl);
}
}
}
void setButtonDrehgeber()
{
delay(100);
if (digitalRead(Encoder_C) == LOW )
{
++FlagforButton;
if (FlagforButton % 2 == 0)
{
Serial.println("Nashode");
FlagforCurser = 10;
if (Curserplatz == 0)
{
drehzahl1 = NextStatusDrehzahl;
}
if (Curserplatz == 1)
{
drehzahl2 = NextStatusDrehzahl;
}
if (Curserplatz == 2)
{
drehzahl3 = NextStatusDrehzahl;
}
if (Curserplatz == 3)
{
drehzahl4 = NextStatusDrehzahl;
}
}
else
FlagforCurser = 0;
if (Curserplatz == 0)
{
drehzahl1 = NextStatusDrehzahl;
TotalPrintingCount = drehzahl1;
}
if (Curserplatz == 1)
{
drehzahl2 = NextStatusDrehzahl;
PartialPrintingCount = drehzahl1;
}
if (Curserplatz == 2)
{
drehzahl3 = NextStatusDrehzahl;
OffContact = drehzahl1;
}
if (Curserplatz == 3)
{
drehzahl4 = NextStatusDrehzahl;
CuringTime = drehzahl1;
}
}
}
// this function is to use, if we have a Arduino Mega
void Button()
{
while( TotalPrintingCount > ActualPrintingCount)
{
// put your main code here, to run repeatedly:
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
for (int j=0 ; j < 10 ; j++)
{
delay(100);
if (buttonState1){
Serial.println("Button gedruckt");
Siebdruck();}
else
relay_init();
break;
}
}
}