Last active
November 26, 2019 19:14
-
-
Save ceiborg/f5efffcd8ea3fc97203a682820533566 to your computer and use it in GitHub Desktop.
PIR + motor 28BYJ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| #############%@ | |
| #### #########% | |
| ## #########& | |
| #% ########## | |
| ### ############ | |
| ################### ceiborg.com | |
| ################### tecnotextiles | |
| ################## | |
| ################# | |
| ############## | |
| ## | |
| # | |
| */ | |
| //Código: Mover 2 motores 28BYJ cuando el modulo sensor Hc-sr501 PIR Infrarojo detecta movimiento | |
| // Declarar PIR | |
| const int PIR = 2; // el OUT del sensor lo ponemos en el pin 2 | |
| int pir_lectura = 0; // declaramos una variable entera que vale 0 cuando se inicializa | |
| // motor 28BYJ con driver UNL2003 -> del driver a los pines de la placa | |
| const int motorPin1 = A5; // 28BYJ48 In1 | |
| const int motorPin2 = A4; // 28BYJ48 In2 | |
| const int motorPin3 = A3; // 28BYJ48 In3 | |
| const int motorPin4 = A2; // 28BYJ48 In4 | |
| // segundo motor 28BYJ con driver UNL2003 -> del driver a los pines de la placa | |
| const int motorPin1_2 = 3; // 28BYJ48 In1 | |
| const int motorPin2_2 = 9; // 28BYJ48 In2 | |
| const int motorPin3_2 = 10; // 28BYJ48 In3 | |
| const int motorPin4_2 = 11; // 28BYJ48 In4 | |
| int motorSpeed = 1200; //variable para fijar la velocidad | |
| int stepCounter = 0; // contador para los pasos | |
| int stepsPerRev = 4076; // pasos para una vuelta completa | |
| //secuencia media fase | |
| const int numSteps = 8; | |
| const int stepsLookup[8] = { B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001 }; | |
| void setup() | |
| { | |
| //declaramos pines como salida MOTOR 1 | |
| pinMode(motorPin1, OUTPUT); | |
| pinMode(motorPin2, OUTPUT); | |
| pinMode(motorPin3, OUTPUT); | |
| pinMode(motorPin4, OUTPUT); | |
| //declaramos pines como salida MOTOR 2 | |
| pinMode(motorPin1_2, OUTPUT); | |
| pinMode(motorPin2_2, OUTPUT); | |
| pinMode(motorPin3_2, OUTPUT); | |
| pinMode(motorPin4_2, OUTPUT); | |
| // Inicialización PIR | |
| pinMode(PIR, INPUT); // Configurar pir como entrada o INPUT | |
| Serial.begin(9600); // Configurar el puerto serial a 9600 por si queremos monitoriar | |
| } | |
| void loop(){ | |
| //PIR | |
| pir_lectura = digitalRead(PIR); //leer el pin del sensor de movimiento PIR | |
| Serial.println (pir_lectura); // si queremos visualizar la lectura | |
| // | |
| if (pir_lectura >0){ //cuando el PIR detecta movimiento ejecuta que un motor gire para un lado y el otro para el lado opuesto y luego a la inversa. | |
| for (int i = 0; i < stepsPerRev ; i++) | |
| { | |
| clockwise(); | |
| delayMicroseconds(motorSpeed); | |
| } | |
| for (int i = 0; i < stepsPerRev ; i++) | |
| { | |
| anticlockwise(); | |
| delayMicroseconds(motorSpeed); | |
| } | |
| delay(50); | |
| } | |
| } | |
| void clockwise() //definimos que va a hacer clockwise | |
| { | |
| stepCounter++; | |
| if (stepCounter >= numSteps) { | |
| stepCounter = 0; | |
| } | |
| setOutput(stepCounter); | |
| setOutput2(stepCounter); | |
| } | |
| void anticlockwise() //definimos que va a hacer anticlockwise | |
| { | |
| stepCounter--; | |
| if (stepCounter < 0) { | |
| stepCounter = numSteps - 1; | |
| } | |
| setOutput(stepCounter); | |
| setOutput2(stepCounter); | |
| } | |
| void setOutput(int step) | |
| { | |
| digitalWrite(motorPin1, bitRead(stepsLookup[step], 0)); | |
| digitalWrite(motorPin2, bitRead(stepsLookup[step], 1)); | |
| digitalWrite(motorPin3, bitRead(stepsLookup[step], 2)); | |
| digitalWrite(motorPin4, bitRead(stepsLookup[step], 3)); | |
| } | |
| void setOutput2(int step) | |
| { | |
| digitalWrite(motorPin1_2, bitRead(stepsLookup[step], 3)); | |
| digitalWrite(motorPin2_2, bitRead(stepsLookup[step], 2)); | |
| digitalWrite(motorPin3_2, bitRead(stepsLookup[step], 1)); | |
| digitalWrite(motorPin4_2, bitRead(stepsLookup[step], 0)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment