// Include the Arduino Stepper Library //#include #include #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 // twelve servo objects can be created on most boards int pos = 0; // variable to store the servo position // Number of steps per output rotation //const int stepsPerRevolution = 200; // Create Instance of Stepper library //Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); 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 // set the speed at 60 rpm: //myStepper.setSpeed(60); // 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); // Set 1/16 microstepping 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); // tell servo to go to position in variable 'pos' delay(15); //myStepper.step(-10); // Set the spinning direction clockwise: digitalWrite(dirPin, LOW); // Go -10 steps slowly: for (int i = 0; i < 16*10; i++) { // These four lines result in 1 step: digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } /* //blinking light while stepper is not at endstop digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) delay(100); // wait for a second digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW delay(100); */ } 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 //light is fully on when the endstop button is pressed //digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) //myStepper.step(10); digitalWrite(dirPin, HIGH); // Go 10 steps slowly: for (int i = 0; i < 16*10; i++) { // These four lines result in 1 step: 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"); //myStepper.step(140); digitalWrite(dirPin, HIGH); // Go 140 steps slowly: for (int i = 0; i < 16*140; i++) { // These four lines result in 1 step: 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); // tell servo to go to position in variable 'pos' delay(2000); //myStepper.step(260); digitalWrite(dirPin, HIGH); // Go 260 steps slowly: for (int i = 0; i < 16*260; i++) { // These four lines result in 1 step: digitalWrite(stepPin, HIGH); delayMicroseconds(1000); digitalWrite(stepPin, LOW); delayMicroseconds(1000); } delay(500); myservo.write(180); delay(2000); isBrewing = true; } } isAtEndstop = false; isBrewing = false; /* for (pos = 90; pos <= 160; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree Serial.println("moving servo"); myservo.write(50); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } // step one revolution in one direction: Serial.println("clockwise1"); myStepper.step(150); delay(1000); for (pos = 90; pos <= 160; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree Serial.println("moving servo again"); myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for (pos = 90; pos <= 160; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } // step one revolution in one direction: Serial.println("clockwise2"); myStepper.step(293); delay(1000); for (pos = 90; pos <= 160; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } */ }