//
#define btn0p 10
#define btn1p 9
#define btn2p 8
//
#define DIR 5
#define STEP 4
//piso
int const P0 = 0;
int const P1 = 200;
int const P2 = 400;
//
const int vel = 5; //velocidad del motor
//
int PAA;
void setup() {
// put your setup code here, to run once:
//stepper
pinMode(DIR, OUTPUT);
pinMode(STEP, OUTPUT);
//botones
pinMode(btn0p, INPUT_PULLUP);
pinMode(btn1p, INPUT_PULLUP);
pinMode(btn2p, INPUT_PULLUP);
// poner un sensor y hacerlo girar hasta que toque el sensor
PAA = 0;
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(btn2p) == LOW) {
PAA = Mover(P2, PAA);
} else if(digitalRead(btn1p) == LOW) {
PAA = Mover(P1, PAA);
} else if(digitalRead(btn0p) == LOW) {
PAA = Mover(P0, PAA);
}
}
//si hago una funcion de calibrado para usar en setup se guardaran las variables?
int Mover(int PD, int PAA) { //PAA = Mover("Piso destino", PAA)
int PM;
if (PD > PAA){
//subir (Piso destino - piso actual)
PM = PD - PAA;
Subir(PM);
} else if (PD < PAA){
//Bajar (Piso actual - piso destino)
PM = PAA - PD;
Bajar(PM);
}
return PD;
}
void Subir(int PM) {
//cerrar puertas
digitalWrite(DIR, HIGH);
for (int i = 0; i < PM; i++){
digitalWrite(STEP, HIGH);
delay(vel);
digitalWrite(STEP, LOW);
delay(vel);
}
//abrir y esperar
}
void Bajar(int PM) {
//cerrar puertas
digitalWrite(DIR, LOW);
for (int i = 0; i < PM; i++){
digitalWrite(STEP, HIGH);
delay(vel);
digitalWrite(STEP, LOW);
delay(vel);
}
//abrir y esperar
}
P2
P1
P0