/*Sergio Martinez *CPS42 *Milk shake machinestirrer */ //Serve Switch const int serveSwitch = 4; //Input that enables the serving sequence for strawberry milk. int serveSwitchState = 0; //Listens for the state of the serveSwitch. ON/OFF //Reversable motor leads for the stirrer actuator. const int linearActuatorLead1 = 2; const int linearActuatorLead2 = 3; //Switches to register the bottom and top of the actuator range. const int actuatorBottom = 8; const int actuatorTop = 10; int bottomSwitchState = 0; int topSwitchState = 0; //Enables the driver chip and speed of the motors in the system. const int motorDriverChip = 9; //Motor speed to be used for each motor in Hz. int stirrerDropSpeed = 20; int stirrerLiftSpeed = 45; int stirSpeed = 20; //stirrer motor pin const int stirrer = 5; //Milk pump pin const int milkPump = 6; //Strawberry syrup pump pin const int strawberryPump = 7; void loop(){ if (wantsMilk()){ //If user wants milk //Drop stirrer. dropstirrer(); delay(3000); //make it last longer, just for show. //Serve milk. serveMilk(); delay(3000); //Serve strawberry syrup serveStrawberry(); delay(3000); //Begin stiring. stir(); delay(3000); //Hide the stirrer. liftstirrer(); } } /* *Assign inputs and outputs. */ void setup(){ pinMode(linearActuatorLead1, OUTPUT); pinMode(linearActuatorLead2, OUTPUT); pinMode(motorDriverChip, OUTPUT); pinMode(stirrer, OUTPUT); pinMode(milkPump, OUTPUT); pinMode(strawberryPump, OUTPUT); pinMode(serveSwitch, INPUT); pinMode(actuatorBottom, INPUT); pinMode(actuatorTop, INPUT); //Disables the driver chip analogWrite(motorDriverChip, 0); } /* *Enable the motor driver chip with a predetermined speed. */ void enableMotors(int motorSpeed){analogWrite(motorDriverChip, motorSpeed);} /* *Disable all the motors */ void disableMotors(){analogWrite(motorDriverChip, 0);} /* *Turns on the stirrer motor for 7 seconds. */ void stir(){ //Stir the milk for 7 seconds. analogWrite(stirrer, stirSpeed); delay(10000); //Turn it off. analogWrite(stirrer, 0); } /* *Reverses the lever motor. *Precondition: Lever must be dropped. *Postcondition: Runs the motor until it reaches the TOP of the bolt. */ void liftstirrer(){ //Set the lift speed enableMotors(stirrerLiftSpeed); //Reverse the process to hide the lever-stirrer. digitalWrite(linearActuatorLead1, HIGH); digitalWrite(linearActuatorLead2, LOW); delay(800); //Turn off motor. disableMotors(); digitalWrite(linearActuatorLead1, LOW); } /* *Turns the lever motor on to drop the stirrer. *Precondition: Input and output pins must be setup. *Postcondition: Runs the motor until it reaches the BOTTOM of the lever bolt. */ void dropstirrer(){ enableMotors(stirrerDropSpeed); //Starts the lever motor. It drops for .6 seconds. digitalWrite(linearActuatorLead1, LOW); digitalWrite(linearActuatorLead2, HIGH); delay(600); //Turn off the motor. disableMotors(); digitalWrite(linearActuatorLead2, LOW); } /* *Activates the milk pump. */ void serveMilk(){ digitalWrite(milkPump, HIGH); delay(4000); //serve for three seconds. digitalWrite(milkPump, LOW); } /* *Activates the strawberry pump. */ void serveStrawberry(){ digitalWrite(strawberryPump, HIGH); delay(2000); //serve for one second. digitalWrite(strawberryPump, LOW); } /* *Reads if user press the serve button. *Return true if the switch has been pressed. */ boolean wantsMilk(){ boolean wants = false; serveSwitchState = digitalRead(serveSwitch); //reads the input. delay(1); if (serveSwitchState == 1){ //If user wants milk. wants = true; //change state to true. } return wants; //return whether user wants or not. }