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();
}