
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) Video Serie: Kit de Carro Parte 5 (BT) […]