How to create a stop button as a function call

I need a stop button that will work only when the motor which are the C 1.1 -c2.2 as i cannot use interrupt to call the stop button anytime i would have to use function call to check the stop so i need some idea on how to do it ?

//c1_1 --c2_2 are the motors
int c1_1 = 3;
int c1_2 =4;
int c2_1 =5;
int c2_2 = 6;
int act =7;
int green =8;
int red =9``;
int fan =10;
int buzzer =11;
int s1 =12;
int htr =13;
int start =A0;
int stop =A1;
int s2 = A2;
int s3 =A3;
int s4 =A4;
int s5 = A5;
int state1= 0;
int state2 = 0;
int state3 = 0;
int state4 = 0;
int startstate = 0;
int stopstate =0;
int state5 =0;
int c=0;
int time =0;
unsigned long previousMillis = 0;
unsigned long interval = 50;
void setup()
{
  pinMode(c1_1,OUTPUT);
  pinMode(c1_2,OUTPUT);
  pinMode(c2_1,OUTPUT);
  pinMode(c2_2,OUTPUT);
  pinMode(act,OUTPUT);
  pinMode(green,OUTPUT);
  pinMode(red,OUTPUT);
  pinMode(fan,OUTPUT);
  pinMode(s1,INPUT_PULLUP);
  pinMode(htr,OUTPUT);
  pinMode(start,INPUT_PULLUP);
  pinMode(stop,INPUT_PULLUP);
  pinMode(s2,INPUT_PULLUP);
  pinMode(s3,INPUT_PULLUP);
  pinMode(s4,INPUT_PULLUP);
  pinMode(s5,INPUT_PULLUP);
  pinMode(buzzer,OUTPUT);

  digitalWrite(red,HIGH);
  digitalWrite(green,LOW);
  digitalWrite(buzzer,LOW);
  digitalWrite(c1_1,LOW);
  digitalWrite(c1_2,LOW);
  digitalWrite(c2_1,LOW);
  digitalWrite(c2_2,LOW);
  digitalWrite(act,LOW);
  digitalWrite(htr,LOW);
  digitalWrite(fan,LOW);
  Serial.begin(9600);

}

void loop()
{
  state1= digitalRead(s1);
  state2 = digitalRead(s2);
  state3 = digitalRead(s3);
  state4 = digitalRead(s4);
  state5 = digitalRead(s5);
  startstate = digitalRead(start);
  stopstate = digitalRead(stop);

  if(startstate ==LOW && time ==0 ){
    digitalWrite(red,LOW);
    digitalWrite(green,HIGH);
    digitalWrite(c1_1,HIGH);
    digitalWrite(c1_2,LOW);
  }
  if(state1 == LOW){
    digitalWrite(c1_1,LOW);
    digitalWrite(c1_2,LOW);
    digitalWrite(act,HIGH);
  }
  if(state2 ==LOW){
    digitalWrite(c2_1,HIGH);
    digitalWrite(act,LOW);
  }
  if(c<4) {
    if(state3==LOW){
      cooking(11,1000,13,5);
      c++;
    }
    if(state4==LOW){
      cooking(23,1000,10,6);
    }
  }
  if(state5==LOW){
    while(true){
      startstate = digitalRead(start);
      unsigned long currentMillis = millis();
      if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;
        time += interval;
        Serial.println(time);
        digitalWrite(c2.2,LOW);
        digitalWrite(buzzer,HIGH);
        if(startstate ==LOW){
          inspect(11,3,500,500);
          break;
        }
        if(time >=5000){
          inspect(10,3,500,500);
          break;
        }
      }
    }
  }
}
void cooking(int i,int time,int system,int motor){
  int z=0;
  for(;z<i;z++){ 
    digitalWrite(c2_1,LOW);
    digitalWrite(c2_2,LOW);
    digitalWrite(system,HIGH);
    delay(time);
  }
  digitalWrite(system,LOW);
  digitalWrite(motor,HIGH);

}
void inspect(int led, int m, int ontime1 ,int offtime1){
  int n=0;
  digitalWrite(buzzer,LOW); 
  for(;n<m;n++){
    digitalWrite(led,HIGH);
    delay(ontime1);
    digitalWrite(led,LOW);
    delay(offtime1);
  }
  time =0;
  digitalWrite(green,HIGH);
}

Is the dot allowed as part of variable name? Is your code compile?

oh yea just realised i was supposed to use _ instead i would change it

If you need help, try to express your problem more clearly, with much details. Please use a full sentencies with punctuation marks. If English is not your native language, use a google translator.

I don't see anything about motors in your code. In general, it is a good idea to add a comments to the code.

ok i will make it clearer i need a stop button that will work only when the motor which are the C 1.1 -c2.2 as i cannot use interrupt to call the stop button anytime i would have to use function call to check the stop so i need some idea on how to do it ?

The method you use a millis() :

make the code blocking the same way as if you use a delay()

You must not add a while(1) loop. You already have a loop - a function loop(). Put all your calls inside it and test the millis to run the things in proper time.

so remove the while and put the code in the if? is that what you mean

so like this?

if(state5 ==LOW){
      currentMillis = millis();
      if (currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;
        time += interval;
        Serial.println(time);
        startstate = digitalRead(start);
        digitalWrite(c22,LOW);
        digitalWrite(buzzer,HIGH);
        if(startstate ==LOW){
          inspect(8,3,500,500);
          break;
        }
        if(time >=5000){
          inspect(9,3,500,500);
          break;
        }
      }

And add the stop button same way as you use a start flag.

how? can i ask from all i search is all use interrupt and i don't get it

If your code became non-blocking, you will not need an interrupt.
Just test the button on every start of loop() cycle

so if i remove the while it will stop after the state5 becomes high again

can you give me an example? i don't really get wat you mean

To write an example I need to understand what are you try to achieve in your code...

so it like a e stop basically it will only work IF the c which is the motors are moving and will stop only the motors

so it like a e stop basically it will only work IF the c which is the motors are moving and will stop only the motors i cannot use interupt as the pins are not the right one

why not make the code state and event driven? an event can be a button press, as well as a timer expiration.

not sure what your code for state5 == LOW is doing except to flash an LED and check for a button press.

toggling an LED can be done using a timer

look this over


byte PinsLed [] = { 10, 11, 12 };
byte PinsBut [] = { A1, A2, A3 };
#define N_BUT   sizeof(PinsBut)

byte butState [N_BUT];

enum { But1, But2, But3 };
enum { Off = HIGH, On = LOW };


// -----------------------------------------------------------------------------
int
isPressed (
    int  idx)
{
    byte but = digitalRead (PinsBut [idx]);
    if (butState [idx] != but)  {
        butState [idx] = but;
        delay (20);     // debounce

        return LOW == but;
    }
    return false;
}

// -----------------------------------------------------------------------------
const unsigned long MsecPeriod = 500;
      unsigned long msec0;
bool  tmr;

void
loop ()
{
    unsigned long msec = millis ();
    if (tmr && msec - msec0 >= MsecPeriod)  {
        msec0 = msec;
        digitalWrite (PinsLed [2], ! digitalRead (PinsLed [2]));
    }

    if (isPressed (But1))  {
        digitalWrite (PinsLed [0], ! digitalRead (PinsLed [0]));
    }

    else if (isPressed (But2)) {
        digitalWrite (PinsLed [1], ! digitalRead (PinsLed [1]));
    }

    else if (isPressed (But3)) {
        digitalWrite (PinsLed [2], Off);
        tmr = ! tmr;
    }
}

// -----------------------------------------------------------------------------
void
setup ()
{
    Serial.begin (9600);

    for (unsigned n = 0; n < sizeof(PinsBut); n++)  {
        pinMode (PinsBut [n], INPUT_PULLUP);
        butState [n] = digitalRead (PinsBut [n]);
    }

    for (unsigned n = 0; n < sizeof(PinsLed); n++)  {
        digitalWrite (PinsLed [n], Off);
        pinMode      (PinsLed [n], OUTPUT);
    }
}

it like a sensor i say

as the way i coded it is fine for now i say as that how they want me to code it