// VitaFab 2019 by Arash, Gleb, Marjo, Perttu and Lukasz // FabLab Oulu #include // Servo library #include //store position of box // Variables const int buttonPosition = 15; // proximity button const int buttonPin = 14; //button pin const int stePin = 4; // stepping const int dirPin = 5; // direction HIGH = LEFT || LOW = RIGHT const int sleePin = 6; // stepper sleep mode int buttonState = 0; // ignite the process int servopos = 0; // servo position Servo myservo; // create servo object to control a servo ////////////////////// SETUP //////////////////////////////////////////////////////////// void setup() { myservo.attach(3); // attaches the servo on pin 3 to the servo object pinMode(buttonState, INPUT_PULLUP); // set button as input pinMode(buttonPin, INPUT_PULLUP); // set button as input pinMode(stePin, OUTPUT); pinMode(dirPin, OUTPUT); pinMode(sleePin, OUTPUT); // checkSteps(); } ////////////////////// SETUP //////////////////////////////////////////////////////////// void loop() { checkSteps(); buttonState = digitalRead(buttonPin); if (buttonState == LOW) { runProcedure(); } } void checkSteps() { digitalWrite(sleePin, HIGH); digitalWrite(dirPin, HIGH); while (digitalRead(15) == 1) { digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); } digitalWrite(sleePin, LOW); } void runProcedure() { digitalWrite(sleePin, HIGH); digitalWrite(dirPin, LOW); for (int x = 0; x < 18400; x++) { digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); } digitalWrite(sleePin, LOW); for (servopos = 0; servopos <= 180; servopos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree myservo.write(servopos); // tell servo to go to position in variable 'servopos' delay(15); // waits 15ms for the servo to reach the position } for (servopos = 180; servopos >= 0; servopos -= 1) { // goes from 180 degrees to 0 degrees myservo.write(servopos); // tell servo to go to position in variable 'servopos' delay(15); // waits 15ms for the servo to reach the position } digitalWrite(sleePin, HIGH); digitalWrite(dirPin, HIGH); for (int x = 18400; x > 0; --x) { digitalWrite(stePin, HIGH); delayMicroseconds(200); digitalWrite(stePin, LOW); delayMicroseconds(150); } digitalWrite(sleePin, LOW); }