Creating an off button that will shut down all of my code

Hey, I am new to coding and am trying to figure out how to put in a button that will turn off all of my code at any time. I am having a hard trying to figure out the right format for the button code. The off button is hooked into Pin 1. The off button will be turning off Cupcake Controls() at any time. If anyone can help me it would be much appreciated. have a good day:)

I am creating a machine that will ice my cupcakes for me incase you were wondering:)

#include <Stepper.h>
#define ENABLE 5
#define DIRA 3
#define DIRB 4
int lightPin = 0;
int lightPin2 = 1;
int i;
boolean conveyerControl = false;
const int stepsPerRevolution = 1000; // number of steps per revolution
const int rolePerMinute = 17; // Adjustable range of 28BYJ-48 stepper is 0~17 rpm
Stepper myStepper(stepsPerRevolution, 8, 10, 9, 11);
const int rolePerMinute2 = 17; // Adjustable range of 28BYJ-48 stepper is 0~17 rpm
Stepper myStepper2(stepsPerRevolution, 6, 12, 7, 13);
boolean conveyerControl2 = false;
const int buttonPin = 2; // the number of the pushbutton pin
int onflag = 0;
int reading;

const int buttonPin2 = 1; // the number of the pushbutton pin
int onflag2 = 1;
int reading2;

void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(ENABLE, OUTPUT);
pinMode(DIRA, OUTPUT);
pinMode(DIRB, OUTPUT);
Serial.begin (9600);
myStepper.setSpeed(rolePerMinute);
myStepper2.setSpeed(rolePerMinute2);

Serial.print(onflag);

}

void CupcakeControls() {

int reading = analogRead(lightPin);
//Serial.println(reading);
if (reading > 100) {
conveyerControl = true;
}
if (reading < 100) {
conveyerControl = false;
}
if (conveyerControl == true) {
digitalWrite(ENABLE, HIGH); // enable on
digitalWrite(DIRA, HIGH); //one way
}
if ( conveyerControl == false) {
digitalWrite(ENABLE, LOW); // enable on
digitalWrite(DIRA, LOW); //one way
myStepper.step(stepsPerRevolution * 1);
}

int reading2 = analogRead(lightPin2);
//Serial.println(reading2);
if (reading2 > 100) {
conveyerControl2 = true;
}
if (reading2 < 100) {
conveyerControl2 = false;
}
if (conveyerControl2 == true) {
digitalWrite(ENABLE, HIGH); // enable on
digitalWrite(DIRA, HIGH); //one way
}
if ( conveyerControl2 == false) {
digitalWrite(ENABLE, LOW); // enable on
digitalWrite(DIRA, LOW); //one way
myStepper2.step(stepsPerRevolution * 1);
myStepper2.step(-stepsPerRevolution * 1);
}
}

void loop()
{
while (onflag == 0) {
reading = digitalRead(buttonPin);
if (reading == LOW) {
onflag = 1;
}
}

CupcakeControls();

}

OK, first things first.

You need to go and read the forum instructions so that you can go back and modify your original post (not re-post it - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. If you do not post it as “code” it can as you now see, be quite garbled and is always more difficult to read.

It is inappropriate to attach it as a “.ino” file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only between complete functional blocks.


Next, you need to pose a meaningful question.

What you you mean by “shut down all of my code” or “turn off all of my code at any time”? That makes no sense.

Do you mean you want to stop a certain process? OK, if that is what you want, say so. When you stop it, what things must happen to do it safely? Do you want to stop it forever, or start it up again later?

const int buttonPin2 = 1;

Pin 1 is the serial TX pin on some Arduinos and not usable for I/O if using serial functions, you didn't say which Arduino.

void loop()
{
  reading = digitalRead(buttonPin);
    if (reading ==  LOW) {
    CupcakeControls();
    }
}

Something like that should only run CupcakeControls() when you want it.

That won’t stop your program while it’s executing the function though.

For that you could make a interrupt subroutine that monitors your button pin (agree don’t use 1 as general practice) and have the ISR set a flag that you check in multiple spots in your code (usually before any movement command)

jess9090:
The off button will be turning off Cupcake Controls() at any time.

If the purpose is to stop motor movements instantly then that should be implemented separately from the Arduino by having a switch that simply cuts off power to the motors. At the same time a separate pole on the switch could be used to let the Arduino know there has been an emergency stop.

...R

If you want button to toggle enable/disable controls you can use following code

int oldReading = LOW;
boolean controlEnabled = true;
void loop()
{
    reading = digitalRead(buttonPin);

    if (reading != oldReading ) {
    	controlEnabled = controlEnabled; 
    }

    if (controlEnabled) {
        CupcakeControls();
    }

  oldReading = reading;
}

jess9090:

  • trying to figure out how to put in a button that will turn off all of my code at any time...

Sounds like a power switch ?

Slumpert:

void loop()

{
 reading = digitalRead(buttonPin);
   if (reading ==  LOW) {
   CupcakeControls();
   }
}



Something like that should only run CupcakeControls() when you want it.

Nice of you to pitch in and make recommendations before we have enticed the OP into making his code properly readable and explaining what it is he actually wants it to do! :grinning:

It may be a nice step to first provide an explanation of the consequences of his actual code.

jess9090:

void loop()

{
 while (onflag == 0) {
   reading = digitalRead(buttonPin);
   if (reading ==  LOW) {
     onflag = 1;
   }
 }
 CupcakeControls();
}

Slumpert:
That won’t stop your program while it’s executing the function though.

True enough.

Slumpert:
For that you could make a interrupt subroutine that monitors your button pin (agree don’t use 1 as general practice) and have the ISR set a flag that you check in multiple spots in your code (usually before any movement command)

You could, but it make little sense.

Firstly because there is no need or value to use interrupts, and secondly as it is the movement commands themselves that introduce the delays while they execute. Going on the values in the code, 17 RPM, four seconds per movement.

But my point was that we need to know whether he really needs to stop the movements in mid-stroke or not? They appear to consist of a full cycle of rotation in one case, and a full cycle back and forth in the other. Little point wasting time and effort making suggestions until it it properly described. :roll_eyes:

I missed the 'why' of this post. the power button does offer the solution.

If I can read into your WHY, and assume is it because something is going wrong and you want to pause, fix, then continue.

In any CNC application. you are running a series of codes, one could think of it like reading a book.

Something happens and you want everything to come an instant halt. Like someone pulls the book out of your hand.

Then, you want too just pick up where you left off. That is the hard part.
Hitting the power button does the easy part to make everything stop.

For most of us doing CNC, we will use a post processor that will make all the movements logical. like drilling all the holes from top left to bottom left in a clockwise motion so movement between holes is minimized.
but, after and emergency stop, you would then need to go back and find the spot you left off and start from there as if you are running a new project. REM out the code up to that point, and start from that point. [ comment out, REM is remark, same idea ]

As far as I know, there is no parallel program that notes what has been completed to allow you to pick up where you left off.
We have been known to put in tool changes. The process comes to a stop, the tool comes to home and you have the opportunity to change tools and carry on. That will break up a long program and give points so you can re-enter at that point.

Since i have not finished my first cup of coffee, I could be dreaming and totally missed the point. But, what we are missing is the WHY.
You have some need that is not well defined to us.

if you want to END the process, and keep the machine alive, retract all devices and move the head to HOME, you can sprinkle that into your sketch.

If the program is in the middle of a for() loop, I am not sure how you could return and continue.
or, if you would be better off to just put in a new part and start all over.

Right now the cupcake controls function runs top to bottom without letting go of execution, nothing else can run then including code to watch a button.

To fix that you need to have a function that does one tiny step then returns to the main code to let other things like checking a button get a chance to happen (might check that button 10 million times before it is pressed) and then the control function runs again one tiny step until it is done if ever. The control function has to keep track of what it is doing and there is a technique to make it easier but there is also background you need to play well at that level, and that is why I push that you learn the whole lesson and save yourself a LOT of time playing with half baked ideas about code.

dave-in-nj:
Since i have not finished my first cup of coffee, I could be dreaming and totally missed the point. But, what we are missing is the WHY.
You have some need that is not well defined to us.

Which is why it is a significant waste of time posting 9 further replies until the only-one-posting OP actually returns to answer my #1! :roll_eyes:

Hi,
Probably waiting for the ultra reliable email notification method to kick in... :o :o :o :o

Tom.... :slight_smile:

Hello,
I have no idea, like most of you, the purpose for this code, but there is a button that does this already.

It's called reset.

Then you can just add a piece to the code that does not allow it to continue until another button has been pressed.

Just a thought.