const byte Signal = 2; // sima gia na traviksei i kama tou magniti .sima mesa apo rele plaketas. No epafi , 110/48v rele travaei otan parei 110v/48v apo pinaka
const byte ContactOpen = 5; // sima apo pouraki anoigmatos portas se stasi . Sima mesa apo rele pou pernei tasi apo epafi NO pouraki .
const byte ContactClose = 6;
const byte ContactOveride = 4;
const byte ButtonOpen = A0;
const byte ServiceSwitch = A1;
const byte ButtonClose = A2;
const byte SpeedRead = A6 ;
boolean flagOpen = false;
boolean flagClose = false;
// Defines Stepper pins numbers
const int stepPin = 7;
const int dirPin = 8;
const int enablePin = 9;
int customDelay, customDelayMapped; // Define speed variable
int customDelaySpeed = 1000;
int customDelay1 = 0;
void setup() {
Serial.begin(9600);
// INPUTS
pinMode(Signal, INPUT_PULLUP);
pinMode(ContactOpen, INPUT_PULLUP);
pinMode(ContactClose, INPUT_PULLUP);
pinMode(ContactOveride, INPUT_PULLUP);
pinMode(ButtonOpen, INPUT_PULLUP);
pinMode(ServiceSwitch, INPUT_PULLUP);
pinMode(ButtonClose, INPUT_PULLUP);
// OUTPUTS
// stepper
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enablePin, OUTPUT);
delay(100);
digitalWrite(enablePin, HIGH); //Enables or disable the motor HIGH = Disable , LOW = Enable
}
void loop() {
digitalRead(Signal);
digitalRead(ContactOpen);
digitalRead(ContactClose);
digitalRead(ContactOveride);
digitalRead(ButtonOpen);
digitalRead(ServiceSwitch);
digitalRead(ButtonClose);
int SignalState = digitalRead(Signal);
int ContactOpenState = digitalRead(ContactOpen);
int ContactCloseState = digitalRead(ContactClose);
int ContactOverideState = digitalRead(ContactOveride);
int ButtonOpenState = digitalRead(ButtonOpen);
int ServiceSwitchState = digitalRead(ServiceSwitch);
int ButtonCloseState = digitalRead(ButtonClose);
//////////////////// When No Input Signal /////////////////////////
if (SignalState == HIGH && ContactOpenState == HIGH && ContactOverideState == HIGH) {
digitalWrite(dirPin, HIGH); //Enables the motor to move in a particular direction
digitalWrite(enablePin, LOW); //Enables the motor
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
////////// When No Input Signal and the mechanism is at open position - open contact tiggered //////////////
if (SignalState == HIGH && ContactOpenState == LOW && ContactOverideState == HIGH) {
digitalWrite(enablePin, HIGH); // Disable the motor
}
//////////////////// When Signal input is present signal /////////////////////////
if (SignalState == LOW && ContactCloseState == HIGH && ContactOverideState == HIGH) {
digitalWrite(dirPin, LOW); //Enables the motor to move in a particular direction
digitalWrite(enablePin, LOW); //Enables or disable the motor
customDelayMapped = speedUp(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}
///////// When Signal input is present and the mechanism is at close position - close contact tiggered ////////////
if (SignalState == LOW && ContactCloseState == LOW && ContactOverideState == HIGH) {
digitalWrite(enablePin, HIGH); // Disable the motor
}
//////////////////// When Button Switch is on Service /////////////////////////
if (ServiceSwitchState == LOW ) {
Service();
}
//////////////////// When Terminal contacts are activated /////////////////////////
if (ContactOpenState == LOW && ContactCloseState == LOW || ContactOverideState == LOW ) {
Errors();
}
}
void Service() {
int ServiceSwitchState = digitalRead(ServiceSwitch);
while (ServiceSwitchState == LOW ) {
int ServiceSwitchState = digitalRead(ServiceSwitch);
int ButtonCloseState = digitalRead(ButtonClose);
int ButtonOpenState = digitalRead(ButtonOpen);
int ContactOpenState = digitalRead(ContactOpen);
int ContactCloseState = digitalRead(ContactClose);
int ContactOverideState = digitalRead(ContactOveride);
int customDelay1 = analogRead(A6); // Reads the potentiometer
int customDelaySpeed = map(customDelay1, 0, 1023, 300, 4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
// return newCustom;
//////////////// Check terminal contacts ///////////////////////////////
if (ContactOverideState == LOW) {
Errors();
}
/////// Manual closing ///////////////////////////////
if (ButtonCloseState == LOW && ContactCloseState == HIGH && ContactOverideState == HIGH ) {
digitalWrite(dirPin, HIGH); //Enables the motor to move in a particular direction
digitalWrite(enablePin, LOW); //Enables the motor
//int customDelay1 = analogRead(A6); // Reads the potentiometer
//int customDelaySpeed = map(customDelay1, 0, 1023, 300, 4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
//customDelayMapped = customDelaySpeed; // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
digitalWrite(stepPin, HIGH);
delayMicroseconds(300);
digitalWrite(stepPin, LOW);
delayMicroseconds(300);
Serial.println("Closing door");
}
else if (ButtonCloseState == LOW && ContactCloseState == LOW && ContactOverideState == HIGH) {
digitalWrite(enablePin, HIGH); // Disable the motor
Serial.println("Door Closed");
}
/////// Manual open ///////////////////////////////
if (ButtonOpenState == LOW && ContactOpenState == HIGH && ContactOverideState == HIGH ) {
digitalWrite(dirPin, LOW); //Enables the motor to move in a particular direction
digitalWrite(enablePin, LOW); //Enables the motor
//customDelayMapped = customDelaySpeed; // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
//digitalWrite(stepPin, HIGH);
//delayMicroseconds(customDelayMapped);
//digitalWrite(stepPin, LOW);
//delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1500);
digitalWrite(stepPin, LOW);
delayMicroseconds(1500);
Serial.println("opening door");
}
/////// No action ///////////////////////////////
else if (ButtonOpenState == LOW && ContactOpenState == LOW && ContactOverideState == HIGH) {
digitalWrite(enablePin, HIGH); // Disable the motor
Serial.println("Door Opend");
}
if (ButtonOpenState == HIGH && ButtonCloseState == HIGH && ContactOverideState == HIGH) {
digitalWrite(enablePin, HIGH); // Disable the motor
Serial.println("No Button");
}
Serial.println("Service Mode");
if (ServiceSwitchState == HIGH ) {
break;
}
}
}
void Errors() {
int ContactOpenState = digitalRead(ContactOpen);
int ContactCloseState = digitalRead(ContactClose);
int ContactOverideState = digitalRead(ContactOveride);
digitalWrite(enablePin, HIGH); // Disable the motor
if (ContactOpenState == LOW && ContactCloseState == LOW) {
Serial.println("Error");
}
if (ContactOverideState == LOW) {
Serial.println("Overide");
}
}
// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A6); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 300, 4000); // Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}