Kids messing up Cupcake Frosting Machine

Push "start button" - Get one frosted cupcake in 4 seconds. But darn kids can't wait that long. I need the start button to go dead until the cupcake is frosted and machine returns to base. The machine's timing is controlled by internal button switches (as sensors) so they cannot be dead. My multiple "if/else" are not working. Please help. Do it for the kids :wink: - Mark

// Cupcake Froster – 2016

// Waits for start button to be pushed

// When button pushed, MyServo moves nozzle over
// cupcake and relay1 (motor) opens butterfly valve to
// dispense pressurized frosting

// When full open, sensor1 (button switch) stops relay1
// After 200ms delay - relay2 turns on to close valve

// When closed, sensor2 (button switch) stops relay2 
// MyServo moves nozzle back to start position



#include <Servo.h>  // this sketch requires Servo Library be loaded

Servo MyServo;  // create servo object to control servo

int pos = 0; // variable to store the servo position

// set pin numbers
#define relay1 2
#define relay2 3  
const int StartButton = 4;  // Start Button
const int OpenSensor =  5;  // internal Button to stop open relay
const int CloseSensor = 6;  // internal Button to stop close relay


void setup()
{
   // attaches the servo on pin 9 to the servo object
   MyServo.attach(9);
  
   // initialize relay Pins as output
   pinMode(relay1, OUTPUT);
   pinMode(relay2, OUTPUT);

   // initialize the pushbutton pins as input
   pinMode(StartButton, INPUT);
   pinMode(OpenSensor, INPUT);
   pinMode(CloseSensor, INPUT);
}


void loop()
{
   // hold pushbutton states
   int StartButtonState, OpenSensorState, CloseSensorState;
   StartButtonState = digitalRead(StartButton);
   OpenSensorState = digitalRead(OpenSensor);
   CloseSensorState = digitalRead(CloseSensor);

   // checks that int relay states are off
   digitalWrite(relay1,HIGH);
   digitalWrite(relay2,HIGH);
 

// What follows is the start button action


   if (StartButtonState == LOW) {     // start button is pressed
     digitalWrite(relay1, LOW);  // turn motor on with relay1
     MyServo.write(45);   // move nozzle over cupcake
  
   // when relay1 full open OpenSensorState goes LOW
     if (OpenSensorState == LOW) {
       digitalWrite(relay1, HIGH);  // stop motor
       delay(200);  // delay to not hurt motor and tune frosting
       digitalWrite(relay2, LOW);  // reverse motor to start closing
            
     } else;{
   // wait and listen for OpenSendorState to go LOW
     }

// motor has been turned around and is closing

     if (CloseSensorState == LOW) {
       digitalWrite(relay2, HIGH);  // stop motor
       MyServo.write(0);  // move servo to start position

     } else;{
   // wait and listen for CloseSensorState to go LOW
     }


   } else {
     // do nothing:    
   }

}

Hi Mark, please go back and edit your post to include [code ] tags around to code. To know how, read the How to use this forum (which you should have read already...) After that we'll come back to the problem. But probably all you have to do is save the state of the machine. Is it ready to start making a cup cake? Only then react to the button.

I need the start button to go dead

No, you don't. What you need to do is ignore the state of the switch if the machine is already running.

else;{
   // wait and listen for OpenSendorState to go LOW
     }

Why is that semicolon there? The comment in the empty block is NOT what the code does. There is nothing about the code that waits for anything except the two short delay()s.

Putting every { on a new line, and using Tools + Auto Format would certainly help make your code clearer. Deleting useless else statements, and blocks containing nothing but incorrect comments would not be wrong, either.

Thank you Paul. I will delete (well) ALL the else statements. I guess they were only there to maintain some sort of "machine is running don't bother me" state. The action of the valve opening and closing are like an eternity in machine language. I need it to (like you said) ignore everything else but for the appropriate sensor. I will try again tonight. And Septillion - I will not be using live frosting.

No cupcakes were harmed in the making of this post

septillion's point was that you should have a global boolean variable, makingCupcake, initially set to false.

When the machine is !makingCupcake and the start switch BECOMES (not IS) pressed (see the state change detection example), set makingCupcake to true. When the machine finishes making the cupcake (however you detect that, set makingCupcake to false.

Ahhhhh. It is all making sense. I can use similar statements to keep the machine from doing anything while the valve opens, while the valve closes, or servo returns nozzle to base. Again Thank you both.