Okay, took a quick visit to https://wokwi.com
const int buttonPin = 3; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
const int powerPin = 4; // the number of the input power pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
int machineState; // current reading from the power pin
int lastmachineState; // previous readin from the power pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
unsigned long lastDebounceTime1 = 0; // the last time the output pin was toggled
unsigned long debounceDelay1 = 50; // the debounce time; increase if the output flickers
byte pushCount = 0;
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
// set initial LED state
digitalWrite(ledPin, ledState);
Serial.println("Ready");
}
void loop()
{
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
int mreading = digitalRead(powerPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (mreading != lastmachineState) {
lastDebounceTime1 = millis();
}
if ((millis() - lastDebounceTime1) > debounceDelay1) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
if ( mreading != machineState ) {
machineState = mreading;
if (machineState == HIGH) {
Serial.println("MACHINE ON");
}
else {
Serial.println("MACHINE OFF");
}
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastmachineState = mreading;
if ( lastmachineState == LOW ) {
return; // nothing more to do if machine is OFF
}
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
if (reading != buttonState) {
buttonState = reading;
if ( buttonState == LOW) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
if (ledState) {
Serial.println("LED ON");
}
else {
Serial.println("LED OFF");
}
}
}
}
// save the reading. Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
}