/*
* UV machine EMCS counts for selesta interface
* REv: 1.5
* Changed sensor from PH14 to PH15 (UV pin) n. Now is normally High triggering Low
* Adding Disable feature and blinkg ext led 15/11/20
*/
// pin declaration
const int towerPin = 2;
const int uvPin = 3;
const int outputPin = 5;
// Disable Feature:
const int disableOutPinStartBtn = 11; // disabling the Start button, until terminal input has been made OR terminal request is released
const int disableOutPinStopBtn = 7; // Momentary disable (1 sec max) simulating a human pressing the button to stop the machine, in this case when terminal needs input
const int disableInPin = 8; // Get the disable signal from Terminal/Selesta
const int ledPin = LED_BUILTIN;
const int extLedPin = 4; // shield board LED
// time
const long interval = 3000; // less than 3 seconds, Mode 4 count protocol
const long intervalIdelLedWasOff = 4000;
// var declaration
// Output
int outputState = LOW;
// Input
int currentTowerVal = 0;
int previousTowerVal = 0;
int currentUvVal = 0;
int previousUvVal = 0;
int varDisableIn = 0;
int varDisableOut = 0;
bool incrementCount = false;
bool machineDisabled = false;
bool blinkLedIdleState = true;
// stores and updates millis only when UV was 'on' and back to 'off'
unsigned long uvOn = 0;
unsigned long uvOff = 0;
// stores and updates millis only when Status was back to 'off'
unsigned long idleLed = 0;
void setup() {
pinMode(disableOutPinStartBtn, OUTPUT); // set pin 10 as OUTPUT
pinMode(disableOutPinStopBtn, OUTPUT); // set pin 7 as OUTPUT
pinMode(disableInPin, INPUT_PULLUP); // set pin 8 as INPUT from selesta to know when to disable (using internal pull-up)
pinMode(towerPin, INPUT_PULLUP); // tower green light - low means ON
pinMode(uvPin, INPUT_PULLUP); // UV sensor on machine - low means it is uncovered / ongoing tape UV exposure
// the followingn outputs are being outputed with the same results i.e. led HIGH means output high
pinMode(extLedPin, OUTPUT); // shield led indicator
pinMode(5, OUTPUT); // output to selesta
pinMode(ledPin, OUTPUT); // internal led
Serial.begin(9600);
}
void loop() {
currentTowerVal = digitalRead(towerPin);
currentUvVal = digitalRead(uvPin);
//unsigned long currentMillis = millis();\
varDisableIn = digitalRead(disableInPin);
// digitalWrite(outputPin,HIGH);//////////////// test ...to remove
// IMPORTANT: The first code block used only when Arduino is restared!
// Without it when Arduino is started and the inputs states are ON (i.e. tower on, signal on) it will ignore it. With this block it will change to the actual state
if( (currentTowerVal == HIGH) && (previousTowerVal == HIGH) ){
//Continously looping and initialising the follow either low or high
/**
* Arduino Start-Up output conditioned as always low
*/
outputState = LOW;
digitalWrite(ledPin,outputState);
// digitalWrite(outputPin,outputState);
digitalWrite(extLedPin,outputState);
} else {
outputState = HIGH;
digitalWrite(ledPin,outputState);
// digitalWrite(outputPin,outputState);
digitalWrite(extLedPin,outputState);
}
/**
* Disable
* Params-
* varDisableIn: int, reading from disableInPin
* machineDisabled bool, current board(machine) state
*/
if ((varDisableIn == LOW) && (machineDisabled == false)){
machineDisabled = true;
digitalWrite(disableOutPinStartBtn,HIGH);
digitalWrite(disableOutPinStopBtn,HIGH);
delay(600); // simulating a human pressing STOP button for 6ms
digitalWrite(disableOutPinStopBtn,LOW);
} else if ((varDisableIn == HIGH) && (machineDisabled == true)){
machineDisabled = false;
digitalWrite(disableOutPinStartBtn,LOW);
// digitalWrite(disableOutPinStopBtn,HIGH);
}
// digitalWrite(disableOutPinStartBtn,HIGH);
// delay(1000);
// digitalWrite(disableOutPinStopBtn,HIGH);
// delay(3000);
// digitalWrite(disableOutPinStartBtn,LOW);
// delay(1000);
// digitalWrite(disableOutPinStopBtn,LOW);
// delay(6000);
/**
* blink fast external led to show that the disable function in active
*/
if(machineDisabled == true){
digitalWrite(extLedPin, HIGH);
delay(50);
digitalWrite(extLedPin, LOW);
delay(150);
} else { // blink momentary to show board is idle during non-executing and not disabled
if ((blinkLedIdleState == true) && (currentTowerVal == HIGH)){
digitalWrite(extLedPin, HIGH);
delay(20);
digitalWrite(extLedPin, LOW);
delay(100);
digitalWrite(extLedPin, HIGH);
delay(20);
digitalWrite(extLedPin, LOW);
blinkLedIdleState = false;
idleLed = millis();
}
}
if(millis() - idleLed > intervalIdelLedWasOff)
blinkLedIdleState = true;
// -------------------------- MACHINE EXECUTING AND STOP SECTION ----------------------------------
if(currentTowerVal != previousTowerVal){ // there was a change
if(currentTowerVal == LOW){ // if change tower is ON
Serial.println("Tower On");
outputState = HIGH;
} else { // else if tower is off i.e machine is not executing
Serial.println("Tower OFF");
outputState = LOW;
}
delay(50); // a safe delay
digitalWrite(ledPin, outputState);
digitalWrite(outputPin, outputState); // SEND TO SELESTA A HIGH TO ADHERE WITH MODE 4 AS MACHINE STATE IS RUNNING / EXECUTING
digitalWrite(extLedPin, outputState); // SHOW THE SAME OUTPUT STATE ON LED
previousTowerVal = currentTowerVal;
}// previousTowerVal
// -------------------------- MACHINE COUNT SECTION ----------------------------------
if(currentUvVal != previousUvVal ){
if (currentTowerVal == LOW && currentUvVal == LOW) {
Serial.println("UV Sensor on");
uvOn = millis();
// UV SENSOR WAS TRIGGERED
} else if (currentUvVal == HIGH && previousTowerVal == LOW ){ // UV APPLICATION COMPLETED
uvOff = millis();
// if the difference of the UV sensor between ON and OFF is greater than the interval generate a pulse!
Serial.println("UV Sensor Off");
Serial.println((millis() - uvOn)/1000);
if (millis() - uvOn > interval){
Serial.println("UV Ready");
Serial.println("Output Low");
digitalWrite(extLedPin,LOW);
digitalWrite(ledPin, LOW);
digitalWrite(outputPin, LOW); // PUT LOW
delay(600); // not more than 3 seconds (mode 4 count)
digitalWrite(extLedPin,HIGH);
Serial.println("Output High");
digitalWrite(ledPin, HIGH);
digitalWrite(outputPin, HIGH); // PUT BACK HIGH AS MACHINE IS STILL EXECUTING
}
} // end if else
delay(50); // a safe delay
previousUvVal = currentUvVal;
}// previousUvVal
} // loop