///////////////////////////////////////////////////////////////////////////////////////
//Terms of use
///////////////////////////////////////////////////////////////////////////////////////
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.
///////////////////////////////////////////////////////////////////////////////////////
//This program can be used to drive a linear actuator, driven by a stepper motor and two limit switches.
//tutorial:
//When you turn on the circuit, the engine initializes until it reaches the red microswitch and waits
//for you to press the yellow start button. when the yellow button is pressed, the motor will run
//in the opposite direction until it reaches the green limit switch and stops to wait for the
//operator to reinitialize by pressing the yellow button.
//Utilities:
//Sensor-operated door.
//pet feeder.
//elevator door.
//etc etc
//
// ----------------------------------------------------------------
// S E T T I N G S
// ----------------------------------------------------------------
//limitSwitch
#define SW1 12
#define SW2 11
boolean b_limitSW1=0;
boolean b_limitSW2=0;
//Boton
#define BTN 4
boolean b_BOTON;
//stepper
#define dirPin 2
#define stepPin 3
const int stepsPerRevolution = 50;
const int MICROSEG = 200000;
// ----------------------------------------------------------------
// F U N C T I O N S
// ----------------------------------------------------------------
void INICIALIZAR() {
Serial.println ("******************************************");
Serial.println("INITIALIZING, You must press SW2 (RED)");
while ((!b_limitSW2)) {
REVERSA();
b_limitSW2 = digitalRead(SW2);
}
b_limitSW2=LOW;
}//cierra INICIALIZAR
void ADELANTE() {
// Set motor direction counterclockwise
digitalWrite(dirPin, HIGH);
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(MICROSEG);
digitalWrite(stepPin, LOW);
delayMicroseconds(MICROSEG);
}
}// cierra ADELANTE
void REVERSA() {
// Set motor direction clockwise
digitalWrite(dirPin, LOW);
// Spin motor slowly
for(int x = 0; x < stepsPerRevolution; x++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(MICROSEG);
digitalWrite(stepPin, LOW);
delayMicroseconds(MICROSEG);
}
}// cierra REVERSA
void STOP() {
// Spin motor slowly (PARAR)
digitalWrite(stepPin, LOW);
} // cierra STOP
// ----------------------------------------------------------------
// S E T U P
// ----------------------------------------------------------------
void setup()
{
Serial.begin(115200);
// Declare pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(SW2, INPUT); //b_limitSW2
pinMode(BTN, INPUT); // b_BOTON
pinMode(SW1, INPUT); //b_limitSW1
}
// ----------------------------------------------------------------
// L O O P
// ----------------------------------------------------------------
void loop()
{
//yield();
INICIALIZAR(); // El motor reversa hasta que llega al microSwitche Rojo
Serial.println("Waiting for you to press the YELLOW start button");
while ((!b_BOTON)) { // Queda en espera del boton Amarillo
b_BOTON = digitalRead(BTN); // Lee el puerto del boton, si es alto, sale del bucle
}
b_BOTON=LOW; // Estado anterior del boton antes de ser oprimido
Serial.println("Waiting for you to press the stop button SW1 (GREEN)");
while ((!b_limitSW1)) { // Queda en espera del microSwitche Verde.
ADELANTE(); // Mientras llega al switche limite, se mueve hacia adelante.
b_limitSW1 = digitalRead(SW1);
}
b_limitSW1=LOW; // Estado anterior de boton antes de ser oprimido
STOP(); // El motor se detiene.
Serial.println("Waiting for you to press the YELLOW boot button, to initialize again.");
while ((!b_BOTON)) { // Queda en espera del boton amarillo para inicializar
b_BOTON = digitalRead(BTN);
}
b_BOTON=LOW; // Estado anterior de boton antes de ser oprimido.
}