New to any programing, need help with external stop switch!!

Im trying to make a cycle test machine stop cycling through the loop when I push the button (or pressure sensor) I used this code to create an interrupt so that it would switch to an empty loop until I pressed the restart button which was:

boolean start = true;
volatile boolean e_stop = false;

void setup() {
  Serial.begin(9600);
  
  attachInterrupt(0, e_stop_ISR ,RISING);

if(start == true){
    if(e_stop == false){
      Serial.println("working");
 }
    else{
      Serial.println("AIR-LEAK!");
     start = false;
    }
  }
}
 
void e_stop_ISR(void){
  detachInterrupt(0);
  e_stop = !e_stop;
}

this code alone worked with what I wanted and would loop until I pressed the button. but when I added my code for the operation I need preformed, it would run through the main loop once and then stop the loop as if I had pressed the button.

boolean start = true;
volatile boolean e_stop = false;
 
int count = 4;      // Connect the transistor Gate (first pin on the left, when writing on the chip is facing you) to Arduino pin 2
int Valve = 7;    // Connect the transistor Gate (first pin on the left, when writing on the chip is facing you) to Arduino pin 7
int VentValve = 8;    // Connect the transistor Gate (first pin on the left, when writing on the chip is facing you) to Arduino pin 11
#define relay1 4
#define relay2 7
#define relay3 8
 
void setup() {
  Serial.begin(9600);
  
  attachInterrupt(0, e_stop_ISR ,RISING);
 
  pinMode(count, OUTPUT);
  pinMode(Valve, OUTPUT);
  pinMode(VentValve, OUTPUT);
  pinMode(relay1, OUTPUT);    //connected to relay 1
  pinMode(relay2, OUTPUT);    //connected to relay 2
  pinMode(relay3, OUTPUT);
}
 
void loop() {
 
  if(start == true){
    if(e_stop == false){
      Serial.println("working");
digitalWrite(VentValve, LOW);
digitalWrite(count, HIGH);
delay(10);
digitalWrite(count, LOW); 
 digitalWrite(VentValve, HIGH);
delay(1000);                    // Will supply air to the MP hose and hold for 10 seconds and increase count by 1
digitalWrite(VentValve, LOW);
digitalWrite(Valve, HIGH);
delay(10000);
digitalWrite(Valve, LOW);
digitalWrite(VentValve, HIGH);
delay(1000); 
 digitalWrite(VentValve, LOW);
delay(10000);                     // Will vent air from MP hose, hold for 10s and restart. 
 
    }
    else{
      Serial.println("AIR-LEAK!");
     start = false;
    }
  }
}
 
void e_stop_ISR(void){
  detachInterrupt(0);
  e_stop = !e_stop;
}

can anyone explain why it doesn't repeat my loop only when I put my code in and how to stop it?

Thank you

  1. Code tags look like this: [​code]code tags[​/code]. Please put your code in them when posting code on the forum.

  2. You can't use an interrupt like that to stop a delay().

The solution to your problem here is don't use delay().

Study the blink without delay example for how to correctly delay while allowing yourself to continuously check for other events (such as a button press) on Arduino.

There are obviously a number of issues:

Read the guidance on how to use the forum, it is good. You need to use code tags as advised above. It makes it easier for people to help you. You will also read about the right way to pose a question. You seem to be asking about how you do something in code which, if answered accurately and literally, won't resolve your problem. You need to explain what you want to achieve in the real world, then say how you are trying to achieve it, then ask for help. You will often then get advice telling you that your solution is the wrong way of going about it and pointing you in the right direction. i.e. you don't want the answer to the question you asked, you want the actual solution to the problem you have.

Delay = do nothing for x time
that means that while it is in operation the arduino is not processing anything else of your code
removing delays is one of the first tutorials which are included in the sketches on the ide and are available in detail online. It is quite simple and will open up the possibilities for you no end.

P.S. you can edit your post to include code tags

What is connected to pin 2 and how is it connected?

Pin 2 is the interrupt and is connected to the button i was using to send the signal to stop the main loop, it is connected to a breadboard with the button and a 10k resistor and then that is connected to the ground and 5v pin on the arduino

You still haven't explained what you are trying to achieve. Why do you want to 'stop the loop'? If you get rid of your delays you can easily stop the void loop function by having a 'while loop'. I.e.

while (button pressed) {
do nothing;
check if button pressed;
}

the thing i am trying to achieve is a automated life cycle test. i have my arduino connected to a relay shield that is connected to 2 solenoids. these solenoids control a medium pressure airflow and the air is pushed through inflatable harnesses to expand and then retract them. (this section i have coded and it works on the machine) i want this cycle to loop as many times is needed and then stop once it detects an air leak instead of me having to manually stop it each time.

That is easy. There are a number of ways to achieve it. You could just stop it in a while loop.

So, whatever your air leak detection is;

While (airleak == true){
}

Now it is permanently stuck in the while loop. You could stick something in there to get it out again

Or just stick all your code in an if statement and use a Boolean to control it.

If (airLeak == False) {
Code;
}

Then just code a function to detect airleak and change airLeak to true.

You could add a couple of button inputs. Code them to set airleak back to false to resume the program and one to restart eg set incremental counter to 0 and airLeak false.

I don’t see the need for Interupt

Mattlloyd:
Pin 2 is the interrupt and is connected to the button i was using to send the signal to stop the main loop, it is connected to a breadboard with the button and a 10k resistor and then that is connected to the ground and 5v pin on the arduino

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: