Vamos a usar un stepper para abrir una puerta. Idealmente usaríamos un mecanismo mas sofisticado como un brazo tipo tijera para abrir una puerta, pero en este caso usamos un simple hilo para enrollarlo con un stepper.
Requisitos:
Computadora (mac)
Arduino UNO o equivalente.
L298N driver board
Stepper 12V & 400mA bi-polar (30Ohm por fase)
Battery pack de 6 batteries AA en serie
6 baterias AA
Arduino IDE (https://www.arduino.cc/en/Main/Software)
El H-bridge que usaremos es el siguiente. Es capaz de suplir hasta 2A pero nuestro motor solo demandara 400mA:
Arduino (IoT) Video Serie: Kit de Carro Parte 5 (BT)
En este caso usaremos un HM10, un modulo BLE (Bluetooth de baja energía) que es ideal para proyectos como este por su bajo consumo energético. Antes de hacer funcionar el BLE, vamos a darle poder desde la Arduino 5V/GND para hacer parpadear la LED roja alrededor de 1 vez por segundo. Esto significa que el BLE esta listo para conectarse. Podemos verificar esto con un teléfono que tenga BLE y descubra el modulo, por lo general un Android es ideal o una laptop. Para un iPhone es mejor usar una app como BLE Scanner u otras similares.
Estos BLE pueden utilizar 5V pero para transmitir ocupan 3.3V por lo cual es necesario hacer un divisor de voltaje con 1Ω y 2Ω para conectar al Tx de la Arduino. En el video vemos las conexiones, como descubrir el modulo con un celular y como interactuar con el via Arduino usando el código.
Este es el código final:
#include "SoftwareSerial.h"// import the serial library
#include "AFMotor.h"
AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
SoftwareSerial btserial(10, 2); // RX, TX
char c=' ';
boolean NL = true;
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
Serial.println("Motor test!");
btserial.begin(9600);
motor1.setSpeed(200); // set the speed to 200/255
motor2.setSpeed(200); // set the speed to 200/255
motor3.setSpeed(200); // set the speed to 200/255
motor4.setSpeed(200); // set the speed to 200/255
}
void loop() {
// Leer bluetooth, si es 1, Adelante...
if (btserial.available()){
c=btserial.read();
Serial.write(c);
if(c=='1'){ // if number 1 pressed ....
Serial.write("Forward");
forward();
}
if(c=='2'){// if number 2 pressed ....
Serial.write("Backward");
backward();
}
if(c=='3'){// if number 3 pressed ....
Serial.write("Left");
left();
}
if(c=='4'){// if number 4 pressed ....
Serial.write("Right");
right();
}
if(c=='5'){// if number 5 pressed ....
Serial.write("BRAKE");
brake();
}
}
// Serial Monitor Write BT
if (Serial.available()){
c = Serial.read();
// do not send line end characters to the HM-10
if (c!=10 & c!=13 ) {
btserial.write(c);
}
// If there is a new line print the ">" character.
if (NL) { Serial.print("\r\n>"); NL = false; }
Serial.write(c);
if (c==10) { NL = true; }
}
}
void forward(){
Serial.println("running forward");
motor1.run(FORWARD);
motor2.run(FORWARD);
motor3.run(FORWARD);
motor4.run(FORWARD);
delay(3000);
brake();
}
void backward(){
Serial.println("running backward");
motor1.run(BACKWARD);
motor2.run(BACKWARD);
motor3.run(BACKWARD);
motor4.run(BACKWARD);
delay(3000);
brake();
}
void left(){
motor3.run(FORWARD);
motor2.run(BACKWARD);
}
void right(){
motor4.run(FORWARD);
motor1.run(FORWARD);
}
void brake(){
motor1.run(RELEASE);
motor2.run(RELEASE);
motor3.run(RELEASE);
motor4.run(RELEASE);
}
Arduino IoT Arduino (IoT): Simple Tutorial de Infrarojo (IR) Receptor/Transmisor
Arduino (IoT): Simple Tutorial de IR Receptor/Transmisor (Parte 3)
En la Parte 1 & 2 vimos como usar receptor y LED IR para leer controles y controlar LEDs de color. Ahora veremos como enviar nuestros propios códigos o señales IR desde un LED IR. Es decir, en la primera parte solo usamos el receptor IR para recibir los códigos de un control remoto. Ahora enviaremos esos códigos al dispositivo para controlarlo.
Aseguremos de conectar la LED correctamente con su pata al pin tierra y larga al resistor.
Codigo
El codigo de nuestro ejemplo sera el IRrecord de la librería:
[code]
/* record.ino Example sketch for IRLib2
* Illustrate how to record a signal and then play it back.
*/
#include <IRLibDecodeBase.h> //We need both the coding and
#include <IRLibSendBase.h> // sending base classes
#include <IRLib_P01_NEC.h> //Lowest numbered protocol 1st
#include <IRLib_P02_Sony.h> // Include only protocols you want
#include <IRLib_P03_RC5.h>
#include <IRLib_P04_RC6.h>
#include <IRLib_P05_Panasonic_Old.h>
#include <IRLib_P07_NECx.h>
#include <IRLib_HashRaw.h> //We need this for IRsendRaw
#include <IRLibCombo.h> // After all protocols, include this
// All of the above automatically creates a universal decoder
// class called “IRdecode” and a universal sender class “IRsend”
// containing only the protocols you want.
// Now declare instances of the decoder and the sender.
IRdecode myDecoder;
IRsend mySender;
// Include a receiver either this or IRLibRecvPCI or IRLibRecvLoop
#include <IRLibRecv.h>
IRrecv myReceiver(2); //pin number for the receiver
// Storage for the recorded code
uint8_t codeProtocol; // The type of code
uint32_t codeValue; // The data bits if type is not raw
uint8_t codeBits; // The length of the code in bits
//These flags keep track of whether we received the first code
//and if we have have received a new different code from a previous one.
bool gotOne, gotNew;
void setup() {
gotOne=false; gotNew=false;
codeProtocol=UNKNOWN;
codeValue=0;
Serial.begin(9600);
delay(2000);while(!Serial);//delay for Leonardo
Serial.println(F(“Send a code from your remote and we will record it.”));
Serial.println(F(“Type any character and press enter. We will send the recorded code.”));
Serial.println(F(“Type ‘r’ special repeat sequence.”));
myReceiver.enableIRIn(); // Start the receiver
}
// Stores the code for later playback
void storeCode(void) {
gotNew=true; gotOne=true;
codeProtocol = myDecoder.protocolNum;
Serial.print(F(“Received “));
Serial.print(Pnames(codeProtocol));
if (codeProtocol==UNKNOWN) {
Serial.println(F(” saving raw data.”));
myDecoder.dumpResults();
codeValue = myDecoder.value;
}
else {
if (myDecoder.value == REPEAT_CODE) {
// Don’t record a NEC repeat value as that’s useless.
Serial.println(F(“repeat; ignoring.”));
} else {
codeValue = myDecoder.value;
codeBits = myDecoder.bits;
}
Serial.print(F(” Value:0x”));
Serial.println(codeValue, HEX);
}
}
void sendCode(void) {
if( !gotNew ) {//We’ve already sent this so handle toggle bits
if (codeProtocol == RC5) {
codeValue ^= 0x0800;
}
else if (codeProtocol == RC6) {
switch(codeBits) {
case 20: codeValue ^= 0x10000; break;
case 24: codeValue ^= 0x100000; break;
case 28: codeValue ^= 0x1000000; break;
case 32: codeValue ^= 0x8000; break;
}
}
}
gotNew=false;
if(codeProtocol== UNKNOWN) {
//The raw time values start in decodeBuffer[1] because
//the [0] entry is the gap between frames. The address
//is passed to the raw send routine.
codeValue=(uint32_t)&(recvGlobal.decodeBuffer[1]);
//This isn’t really number of bits. It’s the number of entries
//in the buffer.
codeBits=recvGlobal.decodeLength-1;
Serial.println(F(“Sent raw”));
}
mySender.send(codeProtocol,codeValue,codeBits);
if(codeProtocol==UNKNOWN) return;
Serial.print(F(“Sent “));
Serial.print(Pnames(codeProtocol));
Serial.print(F(” Value:0x”));
Serial.println(codeValue, HEX);
}
Aqui lo que hacemos es que del Monitor Serial vemos que código da el botón de encender/apagar del control de la television. Eso se imprime en la pantalla del monitor y simplemente lo copiamos y lo pegamos en el mismo monitor, arriba, para ENVIAR ese códigos a través de la LED IR. Movemos el proyecto cerca del televisor y la LED IR ahora enviara ese código y controlara el televisor!