Start/Stop for a process

Hello,

Good day, hope we’ve been having lovely days.

Please, I need help on a problem I’ve been battling with for days, which is, “STARTING AND STOPPING AN ARDUINO PROCESS”.

I’ve tried to use if… else, but, it seems, because I introduced while loops, if… else isn’t giving me the desired result, I therefore switched to using just if… else, which seemed promising, but, unfortunately, millis still continues running, and in the program, if millis is 8 seconds and a switch state doesn’t change, it prints error, please, I need help, first, how do I write a correct start/stop sequence which would also affect millis, below is the code.

Thanks in advance.

Ceejay. :slight_smile:

Fail_safe_detection_for_swiches_and_socket.ino (5.69 KB)

Why is it SO VERY DIFFICULT to simply post code?

/*Fail safe detection tester with actuonix linear servo:
  www.actuonix.com
  Author: Cajetan Chinonso Emmanuel
  Company name: Impact Automation/Robotics Technologies */




#include <Wire.h>                                                     // Include wire library
#include <LiquidCrystal_I2C.h>                                        // Include New Liquid crystal for I2C bus
#include <Servo.h>                                                    // Include Servo Library

#define LINEAR_ACTUATORPIN 11                                               // Define Servo Pin

LiquidCrystal_I2C lcd(0x27, 20, 4);                                         // set the LCD address to 0x27 for a 16 chars and 2 line display

Servo LINEAR_ACTUATOR;                                                      // Set object as servo

int linearValue = 1300;                                                     // Value for linear actuator on start
int actuatorPos = 2;                                                        // Positional pin for linear servo
int aPos = 0;                                                               // Positional Value

int switchPin = 13;                                                         // Test Pin
int startPin = 3;                                                           // Start pin
int stopPin = 4;

int count;                                                                  // Counter Value
const int setPoint = 2000;
int countPin = A1;

int buzzer = 6;                                                             // Alarm Pin


int val = 0;                                                              // Value to store test pin
int startVal = 0;                                                         // Value to store start
int stopVal = 0;


unsigned long previousMillis;
const long setTime = 10000;                                                  // Switch monitor

byte currentButtonState;
byte previousButtonState;

boolean counting;
boolean funcRunning;


void setup() {
  Serial.begin(115200);                                                     // Begin Serial Monitoring

  lcd.init();                                                               // LCD with 16 chars, 2 line display
  lcd.clear();                                                              // Clear screen
  lcd.backlight();
  lcd.setCursor(0, 0);
  lcd.print("****WELCOME****");
  delay(3000);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("  ***TRANOS***  ");
  lcd.setCursor(0, 1);
  lcd.print("CONTRACTING LTD");
  delay(5000);
  lcd.clear();
  delay(200);
  lcd.print("Loading.");
  delay(800);
  lcd.clear();
  lcd.print("Loading..");
  delay(800);
  lcd.clear();
  lcd.print("Loading...");
  delay(800);
  lcd.clear();
  lcd.print("Loading.");
  delay(800);
  lcd.clear();
  lcd.print("Loading..");
  delay(800);
  lcd.clear();
  lcd.print("Loading...");
  delay(800);
  lcd.clear();
  lcd.print("Loading.");
  delay(800);
  lcd.clear();
  lcd.print("Loading..");
  delay(800);
  lcd.clear();
  lcd.print("Loading...");
  delay(2000);
  lcd.clear();

  LINEAR_ACTUATOR.attach(LINEAR_ACTUATORPIN, 1050, 2000);                            // min and max position for servo

  pinMode(startPin, INPUT_PULLUP);                                                   // Set start pin as input
  pinMode(stopPin, INPUT_PULLUP);

  pinMode(A1, INPUT_PULLUP);


  LINEAR_ACTUATOR.writeMicroseconds(linearValue);                                    // Linear value
  delay(20);

  funcRunning = true;

}

void stopRun() {
  lcd.setCursor(6, 0);
  lcd.print("START");
  linearValue = 1300;
  Serial.println(linearValue);
}

void testFail() {
  lcd.clear();
  lcd.setCursor(4, 0);
  lcd.print("TEST FAIL");
  delay(4000);
  digitalWrite(buzzer, HIGH);
}

void loop() {
  stopVal = digitalRead(stopPin);
  startVal = digitalRead(startPin);

  digitalWrite(buzzer, OUTPUT);

  stopRun();

  if ((startVal == LOW) && (stopVal == HIGH)) {
    //    funcRunning = true;
    lcd.clear();
    linearValue = 1350;
    delay(4000);
    Serial.println("Start");



    while ((startVal == LOW) && (stopVal == HIGH)) {
      //      funcRunning = true;
      lcd.setCursor(0, 0);
      lcd.print("Testing = ");
      lcd.setCursor(0, 1);
      lcd.print("Max Count = ");

      unsigned long currentMillis = millis();

      previousButtonState = currentButtonState;

      currentButtonState = digitalRead(A1);

      if (currentButtonState == HIGH) {
        linearValue = 1500;
        Serial.println(linearValue);
      }

      if (previousButtonState == LOW) {
        linearValue = 1700;
        Serial.println(linearValue);
      }
      if (currentButtonState == HIGH and previousButtonState == LOW) {


        count++;

        lcd.setCursor(12, 1);
        lcd.print(setPoint);
        lcd.setCursor(10, 0);
        lcd.print(count);
        Serial.println(count);
        previousMillis = currentMillis;



        if (count == setPoint) {
          while (counting = true) {
            Serial.println("Successful");
            lcd.clear();
            lcd.setCursor(0, 0);
            lcd.print("Start New");
            lcd.setCursor(0, 1);
            lcd.print("Test Successful");
            delay(4000);
            counting = false;

            stopRun();
          }
        }

      }

      if ((currentButtonState == HIGH or previousButtonState == LOW) && (currentMillis - previousMillis >= setTime)) {
        while (counting = true) {

          previousMillis = currentMillis;

          lcd.clear();
          Serial.println("Fail");
          testFail();
          delay(100);
          stopRun();
          counting = false;
        }
      }

      if (stopVal == LOW && startVal == HIGH) {
        stopRun();
        Serial.println("Stop");
      }
    }
  }
}

First you need to change your thinking. The Arduino processor never stops. It is always executing whatever instructions you give it at a fixed speed (16MHz for most Arduinos.) So while the Arduino appears to be stopped waiting for you to push a button it isn't actually stopped. It's just checking that button 16 million times per second.

The delay() function appears to stop the Arduino because it becomes unresponsive. But really it's looking at the clock a few million times per second and not looking at any other inputs.

Now check out Planning and Implementing an Arduino Program.

Converting your program away from using delay() might seem like a lot of unnecessary work but it will be worthwhile in the end.

The following is utterly ridiculous and can be omitted. It doesn't do anything except print a useless message and waste time.

delay(5000);
  lcd.clear();
  delay(200);
  lcd.print("Loading.");
  delay(800);
  lcd.clear();
  lcd.print("Loading..");
  delay(800);
  lcd.clear();
  lcd.print("Loading...");
  delay(800);
  lcd.clear();
  lcd.print("Loading.");
  delay(800);
  lcd.clear();
  lcd.print("Loading..");
  delay(800);
  lcd.clear();
  lcd.print("Loading...");
  delay(800);
  lcd.clear();
  lcd.print("Loading.");
  delay(800);
  lcd.clear();
  lcd.print("Loading..");
  delay(800);
  lcd.clear();
  lcd.print("Loading...");
  delay(2000);
  lcd.clear();

But it LOOKS LIKE it's doing something important. Usually everything works better if it looks good.

/*Fail safe detection tester with actuonix linear servo:
www.actuonix.com
Author: Cajetan Chinonso Emmanuel
Company name: Impact Automation/Robotics Technologies */

#include <Wire.h> // Include wire library
#include <LiquidCrystal_I2C.h> // Include New Liquid crystal for I2C bus
#include <Servo.h> // Include Servo Library

#define LINEAR_ACTUATORPIN 11 // Define Servo Pin

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars and 2 line display

Servo LINEAR_ACTUATOR; // Set object as servo

int linearValue = 1300; // Value for linear actuator on start
int actuatorPos = 2; // Positional pin for linear servo
int aPos = 0; // Positional Value

int switchPin = 13; // Test Pin
int startPin = 3; // Start pin
int stopPin = 4;

int count; // Counter Value
const int setPoint = 2000;
int countPin = A1;

int buzzer = 6; // Alarm Pin

int val = 0; // Value to store test pin
int startVal = 0; // Value to store start
int stopVal = 0;

unsigned long previousMillis;
const long setTime = 10000; // Switch monitor

byte currentButtonState;
byte previousButtonState;

boolean counting;
boolean funcRunning;

void setup() {
Serial.begin(115200); // Begin Serial Monitoring

lcd.init(); // LCD with 16 chars, 2 line display
lcd.clear(); // Clear screen
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(“WELCOME”);
delay(3000);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" TRANOS ");
lcd.setCursor(0, 1);
lcd.print(“CONTRACTING LTD”);
delay(5000);
lcd.clear();
delay(200);
lcd.print(“Loading.”);
delay(800);
lcd.clear();
lcd.print(“Loading…”);
delay(800);
lcd.clear();
lcd.print(“Loading…”);
delay(800);
lcd.clear();
lcd.print(“Loading.”);
delay(800);
lcd.clear();
lcd.print(“Loading…”);
delay(800);
lcd.clear();
lcd.print(“Loading…”);
delay(800);
lcd.clear();
lcd.print(“Loading.”);
delay(800);
lcd.clear();
lcd.print(“Loading…”);
delay(800);
lcd.clear();
lcd.print(“Loading…”);
delay(2000);
lcd.clear();

LINEAR_ACTUATOR.attach(LINEAR_ACTUATORPIN, 1050, 2000); // min and max position for servo

pinMode(startPin, INPUT_PULLUP); // Set start pin as input
pinMode(stopPin, INPUT_PULLUP);

pinMode(A1, INPUT_PULLUP);

LINEAR_ACTUATOR.writeMicroseconds(linearValue); // Linear value
delay(20);

funcRunning = true;

}

void stopRun() {
lcd.setCursor(6, 0);
lcd.print(“START”);
linearValue = 1300;
Serial.println(linearValue);
}

void testFail() {
lcd.clear();
lcd.setCursor(4, 0);
lcd.print(“TEST FAIL”);
delay(4000);
digitalWrite(buzzer, HIGH);
}

void loop() {
stopVal = digitalRead(stopPin);
startVal = digitalRead(startPin);

digitalWrite(buzzer, OUTPUT);

stopRun();

if ((startVal == LOW) && (stopVal == HIGH)) {
// funcRunning = true;
lcd.clear();
linearValue = 1350;
delay(4000);
Serial.println(“Start”);

while ((startVal == LOW) && (stopVal == HIGH)) {
// funcRunning = true;
lcd.setCursor(0, 0);
lcd.print("Testing = ");
lcd.setCursor(0, 1);
lcd.print("Max Count = ");

unsigned long currentMillis = millis();

previousButtonState = currentButtonState;

currentButtonState = digitalRead(A1);

if (currentButtonState == HIGH) {
linearValue = 1500;
Serial.println(linearValue);
}

if (previousButtonState == LOW) {
linearValue = 1700;
Serial.println(linearValue);
}
if (currentButtonState == HIGH and previousButtonState == LOW) {

count++;

lcd.setCursor(12, 1);
lcd.print(setPoint);
lcd.setCursor(10, 0);
lcd.print(count);
Serial.println(count);
previousMillis = currentMillis;

if (count == setPoint) {
while (counting = true) {
Serial.println(“Successful”);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Start New”);
lcd.setCursor(0, 1);
lcd.print(“Test Successful”);
delay(4000);
counting = false;

stopRun();
}
}

}

if ((currentButtonState == HIGH or previousButtonState == LOW) && (currentMillis - previousMillis >= setTime)) {
while (counting = true) {

previousMillis = currentMillis;

lcd.clear();
Serial.println(“Fail”);
testFail();
delay(100);
stopRun();
counting = false;
}
}

if (stopVal == LOW && startVal == HIGH) {
stopRun();
Serial.println(“Stop”);
}
}
}
}

This is the code

Please, what's then the right thing to do to say, maybe reset the loop to start again, irrespective of the condition it's executing

Cos I need to reset the program (to start again so I can initialize again with the start button) when something goes wrong or if the execution is successful or while it’s running

Better than that, is the human ability to read and follow instructions.

Yep, instructions, and this website has them. Here, and here.

I think you need to step away from your code for a minute, and let us know what you are trying to do. I'm pretty sure it doesn't involve starting and stopping the program's execution. Arduino programming (well, all programming) involves controlling the flow of execution.

Can you state in plain human language what you want your program to do - don't tell us how you want it to do it.

Cos I need to reset the program (to start again so I can initialize again with the start button) when something goes wrong or if the execution is successful or while it's running

... press the reset button?

I need to press start, everything runs normally and press stop, program stops

Yes, press the reset button

But what does should it do?

The reset button is on the Arduino board. Press it, and the sketch starts over.

Lol, I'm well aware of the arduino reset button, but I don't wanna start from void setup again once the program is set in motion, I wanna reset so, void loop restarts

@christenone, any ideas please

Ceejay90:
@christenone, any ideas please

I have the strange feeling that he wanted you to read the links in Reply #8 and follow their instructions.

...R

Oh, yeah, sorry CJ, you need to define your program better than "it resets and runs loop over again".

But it was the middle of the night and I went to bed.

Hello @ChrisTenone, hope you had a good night rest.

Just like I can use the inbuilt reset button on the arduino to reset the board, likewise, I'd want to reset just void loop with the press of a button, in essence, I give an input, void loop restarts from scratch.

So does your code still look like what Robin copied into the visible for you?

You read the pins for startVal and stopVal at the top of loop() but then you have a while() loop depending on those values which never reads the pins again. So that loop will never end.

You have a digitalRead(A1) which has no explanation. What button is this? Give the pin a name. You also have the previous/current logic wrong - you set previous=current and then test to see if they are different. They will never be different.

Read the provided guides and try again.