// Include the Arduino Stepper Library //#include #include //defining buttons and bools #define endStopBtn 4 #define brewBtn 2 bool isAtEndstop = false; bool isBrewing = false; int brewingBtnState = 0; // define direction pin and step pin #define dirPin 26 #define stepPin 27 #define sleepPin 28 #define resetPin 29 // define microstepping signal pins #define m0 0 #define m1 7 #define m2 6 #define stepsPerRevolution 200 Servo myservo; // create servo object to control a servo int pos = 0; // variable to store the servo position void setup() { pinMode(endStopBtn, INPUT_PULLUP); pinMode(brewBtn, INPUT_PULLUP); pinMode(LED_BUILTIN, OUTPUT); myservo.attach(3, 800, 2200); // attaches the servo on pin 9 to the servo object // Declare stepper driver pins as output: pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); pinMode(sleepPin, OUTPUT); pinMode(resetPin, OUTPUT); pinMode(m0, OUTPUT); pinMode(m1, OUTPUT); pinMode(m2, OUTPUT); // initialize the serial port: Serial.begin(9600); } void loop() { //Stepper setup digitalWrite(sleepPin, HIGH); digitalWrite(resetPin, HIGH); digitalWrite(m0, LOW); digitalWrite(m1, LOW); digitalWrite(m2, HIGH); int endstopBtnState = digitalRead(endStopBtn); Serial.println(brewingBtnState); while(isAtEndstop == false){ endstopBtnState = digitalRead(endStopBtn); if(endstopBtnState == 1){ //move stepper a few steps at a time until it reaches the endstop button myservo.write(0); delay(15); digitalWrite(dirPin, LOW); for (int i = 0; i < 16*10; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } } else{ //if here then the endstop button is pressed, then move the stepper 5? steps away from button and put isAtEndstop variable to true to get out of endstop routine digitalWrite(dirPin, HIGH); // Go 10 steps slowly: for (int i = 0; i < 16*10; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } isAtEndstop = true; } } while(isBrewing == false){ // Serial.println("Press button for Tea"); brewingBtnState = digitalRead(brewBtn); Serial.println(brewingBtnState); if(brewingBtnState == 0){ Serial.println("Making Tea"); digitalWrite(dirPin, HIGH); for (int i = 0; i < 16*140; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } myservo.write(130); delay(500); myservo.write(30); delay(500); myservo.write(130); delay(500); myservo.write(30); delay(500); myservo.write(130); delay(500); myservo.write(30); delay(500); myservo.write(130); delay(5000); myservo.write(0); delay(2000); digitalWrite(dirPin, HIGH); for (int i = 0; i < 16*260; i++) { digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } delay(500); myservo.write(180); delay(2000); isBrewing = true; } } isAtEndstop = false; isBrewing = false; }