#include <Servo.h>
#include <AccelStepper.h>
#define dirPin 12
#define stepPin 13
#define motorInterfaceType 1
Servo myservo; // create servo object to control a servo
#define servoPin1 3 //~
#define pushButtonPin 2
int led = 5; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
int angle = 179; // initial angle for servo (beteen 1 and 179)
int angleStep = 1;
int stepperMotorPosition = 0;
bool raisePlatform = false; // declares a boolean to check its ok to raise platform
const int numberOfPins = 4; // change this to fit the number of steps per revolution
// initialize the stepper library on pins 8 through 11:
//Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
//AccelStepper myStepper(numberOfPins, 8, 9, 10, 11);
AccelStepper myStepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
void setup() {
myStepper.setCurrentPosition(0);
// set the speed at 30 rpm:
myStepper.setMaxSpeed(30); //set max speed the motor will turn (steps/second)
//myStepper.setSpeed(100);
myStepper.setAcceleration(10); //set acceleration (steps/second^2)
// declare pin 5 to be an output:
pinMode(led, OUTPUT);
// initialize the serial port:
Serial.begin(9600); // setup serial
myservo.attach(servoPin1); // attaches the servo on pin 3 to the servo object
pinMode(pushButtonPin, INPUT_PULLUP);
//Serial.println("Servo Button ");
}
void loop() {
if (digitalRead(pushButtonPin) == LOW) { //switch is ON
analogWrite(led, brightness); //begin LED dimming code
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 100) {
fadeAmount = -fadeAmount;
}
// wait for 20 milliseconds to see the dimming effect
delay(20);
//end LED dimming
if (angle == 0) { //if door is fully open raise platform
raisePlatform = true;
myStepper.moveTo(100);
myStepper.setMaxSpeed(100);//set desired speed of stepper motor
myStepper.run();
stepperMotorPosition = myStepper.currentPosition();
//Serial.println("raising platform ");
//Serial.println(stepperMotorPosition);
//Serial.println(stepperMotorPosition);
}
if (angle > 0 && angle <= 180) {
angle = angle - angleStep;
if (angle < 0) {
angle = 0;
}
else {
myservo.write(angle); // move the servo to desired angle: open doors
raisePlatform = false; //Do not raise platform until door is fully open
}
}
//this part to switch off LEDs once doors are open
if (stepperMotorPosition == 100) {
brightness = 0;
}
//end of turning OFF LEDs
delay(20); // waits for the servo to get there
}
if (digitalRead(pushButtonPin) == HIGH) { //switch is OFF
if (angle >= 179) {
analogWrite(led, 0); //begin LED dimming code
}
else {
analogWrite(led, brightness); //begin LED dimming code
}
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 100) {
fadeAmount = -fadeAmount;
}
// wait for 20 milliseconds to see the dimming effect
delay(20);
myStepper.moveTo(0);
myStepper.setMaxSpeed(100);//set desired speed of stepper motor
myStepper.run();
stepperMotorPosition = myStepper.currentPosition();
//Serial.println("lowering platform ");
//Serial.println(stepperMotorPosition);
//Serial.println("Switch is OFF");
//Serial.println(stepperMotorPosition);
// Servo button demo by Robojax.com
if (stepperMotorPosition == 0) {
if (angle >= 0 && angle <= 180) {
angle = angle + angleStep;
if (angle > 180) {
angle = 179; //was set to 180 should be same as starting position
}
else {
myservo.write(angle); // move the servo to desired angle
}
}
}
delay(20); // waits for the servo to get there, to keep same speed this has to be the total
//of dimming LED delay and Servo delay when switch is in ON position
}
}