// constants won't change. Used here to set a pin number:
const int cncReady = 8; // the number of the LED pin
const int cncRunning = 12; // the number of the LED pin 2
const int cncStart = 9; //mimics the start button
int modeState = 0;
const int machineSateLED = 6; //mimics the state machine wether it is running or at rest.
const int button1 = 2;
const int button3 = 3;
const int button4 = 4;
const int resetButton = 5;
const int debugPin = A4;
unsigned long currentMillis = 0;
bool lastBtnState = false;
int mode = 0;
bool startCncInput = 0;
long interval = 0; // interval at which machine state LED blinks (milliseconds)
// Variables will change:
int machineState = LOW; // ledState used to set the LED
int machineToolRunning = 0; // saves the machine state (running / not running)
unsigned long previousMillis = 0; // will store last time LED was updated
void setup() {
// set the digital pin as output:
pinMode(cncReady, OUTPUT);
pinMode(cncRunning, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(cncStart, INPUT);
// Set the 3 different buttons for the 3 different modes as input
pinMode(button1, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
pinMode(resetButton, INPUT);
//setting debug pin to putput
pinMode(debugPin, OUTPUT);
//setting the machine state LED as output
pinMode(machineSateLED, OUTPUT);
//digitalWrite(machineSateLED , HIGH);
digitalWrite(cncReady , HIGH);
digitalWrite(cncRunning , LOW);
lastBtnState = digitalRead(cncStart);
}
void mode_1(){
switch (modeState){
//state of initiating the
case 0:
machineToolRunning = 1;
digitalWrite(cncReady, LOW);
digitalWrite(cncRunning, HIGH);
modeState = 1;
interval = 2000;
previousMillis = millis();
break;
case 1:
async_delay(2);
break;
case 2:
digitalWrite(cncReady, HIGH);
digitalWrite(cncRunning, LOW);
modeState = 0;
machineToolRunning = 0;
break;
}
}
void mode_3(){
switch (modeState){
case 0:
machineToolRunning = 1;
digitalWrite(cncReady, LOW);
modeState = 1;
interval = 2000;
previousMillis = millis();
break;
case 1:
async_delay(2);
break;
case 2:
digitalWrite(cncReady, HIGH);
modeState = 0;
machineToolRunning = 0;
break;
}
}
void mode_4(){
switch (modeState){
case 0:
interval = 3000;
previousMillis = millis();
async_delay(1);
break;
case 1:
machineToolRunning = 1;
digitalWrite(cncReady, LOW);
digitalWrite(cncRunning, HIGH);
modeState = 2;
interval = 5000;
previousMillis = millis();
break;
case 2:
async_delay(3);
break;
case 4:
digitalWrite(cncReady, HIGH);
digitalWrite(cncRunning, LOW);
modeState = 0;
machineToolRunning = 0;
break;
}
}
//switch case for going to specific mode according to clicked pushbutton
void handling_state(){
switch (mode){
case 1:
mode_1();
break;
case 3:
mode_3();
break;
case 4:
mode_4();
break;
// This sets the default mode to 1 in case no pushbutton is clicked.
default:
mode_1();
break;
}
}
//
void reset(){
previousMillis = millis()+interval;
modeState = 0;
machineToolRunning = 0;
}
//Asynchronous timer
void async_delay(int state){
currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
modeState = state;
}
}
void machineStateBlink(){
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
/*unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
*/
// if the LED is off turn it on and vice-versa:
if (machineState == HIGH) {
machineState = LOW;
} else {
machineState = HIGH;
}
// set the LED with the ledState of the variable:
digitalWrite(machineSateLED , machineState);
//delay(500);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// read the state of the pushbutton value:
if (digitalRead(button1)) {mode = 1; reset();}
if (digitalRead(button3)) {mode = 3; reset();}
if (digitalRead(button4)) {mode = 4; reset();}
//reset the mode selected when the reset button is pressed
if (digitalRead(resetButton)){machineToolRunning= 0; mode = 0; machineState = LOW;}
//the following nested if statements are to make sure that you
// 1- User wasn't keeping the pushbutton clicked when the cycle ended
// 2- User clicked the pushbutton when machine was ready
// 3- User didn't click the pushbutton when a cycle was running
if (digitalRead(cncStart) != lastBtnState){
lastBtnState = digitalRead(cncStart);
if (lastBtnState && !machineToolRunning) {
machineToolRunning =1;
}
}
//this for debouncing after clicking on any pushbutton
while (digitalRead(button1) || digitalRead(button3) || digitalRead(button4)){
delay(10);
}
// handle the chosen mode after making sure of conditions 1,2,3 as written in the comments above
if (machineToolRunning){
handling_state();
//machineStateBlink();
}
//this delay is for debounce after clicking on the cncStart input button
delay(10);
}