Sorry guys, I was at work, this the code
* Carrito controlado mediante aplicacion movil echa en android studio
*/
#include <SoftwareSerial.h>
#include <Servo.h>
#include "pitches.h"
SoftwareSerial miBT(2, 3); //Instancia la conexion al bluetooth - PIN 2 a TX y PIN 3 a RX
int LED1B = 13;
int LED2B = 12;
int LED1R = 7;
int LED2R = 8;
int ZumbadorPin = 11;
// The melody array
int melody[] = {
NOTE_FS5, NOTE_FS5, NOTE_D5, NOTE_B4, NOTE_B4, NOTE_E5,
NOTE_E5, NOTE_E5, NOTE_GS5, NOTE_GS5, NOTE_A5, NOTE_B5,
NOTE_A5, NOTE_A5, NOTE_A5, NOTE_E5, NOTE_D5, NOTE_FS5,
NOTE_FS5, NOTE_FS5, NOTE_E5, NOTE_E5, NOTE_FS5, NOTE_E5
};
// The note duration, 8 = 8th note, 4 = quarter note, etc.
int durations[] = {
8, 8, 8, 4, 4, 4,
4, 5, 8, 8, 8, 8,
8, 8, 8, 4, 4, 4,
4, 5, 8, 8, 8, 8
};
// determine the length of the arrays to use in the loop iteration
int songLength = sizeof(melody)/sizeof(melody[0]);
//Entrada de los motores
int motor1A = 5; // INPUT 1
int motor1B = 6; // INPUT 2
int motor2A = 10; // INPUT 3
int motor2B = 9; // INPUT 4
int distance = 100;
Servo servo_motor;
// Dato recibido atraves de la aplicacion movil
char data = 'S';
void setup()
{
pinMode(LED1B, OUTPUT); // LED como salida
pinMode(LED2B, OUTPUT);
pinMode(LED1R, OUTPUT);
pinMode(LED2R, OUTPUT);
pinMode(ZumbadorPin, OUTPUT);
//Definimos las salidas del motor
pinMode(motor1A , OUTPUT);
pinMode(motor1B , OUTPUT);
pinMode(motor2A , OUTPUT);
pinMode(motor2B , OUTPUT);
Serial.begin(9600); //Inicia la comunicación en el monitor serial a 9600 Baudios
miBT.begin(9600); // inicialmente la comunicacion serial a 9600 Baudios (velocidad de convencional)
Serial.println("Módulo conectado");
}
void loop()
{
//Esperamos hasta que haya un Stream de datos
if(miBT.available() > 0){
data = miBT.read();
Serial.println(data);
}
switch (data) {
case 'F':
avanzar(); // Forward | AVANZAR
break;
case 'B':
retroceder(); // Backward | RETROCEDER
break;
case 'L':
izquierda(); //Left | IZQUIERDA
break;
case 'R':
derecha(); //Right | DERECHA
break;
case 'S':
detener(); //Stop | DETENER
break;
case 'G':
giror(); // Forward left | AVANZAR IZQUIERDA
break;
case 'I':
giroi(); // Forward right | AVANZAR DERECHA
break;
case 'H':
giroatrasi(); // Backwards left | RETROCEDER IZQUIERDA
break;
case 'J':
giroatrasr(); // Backwards rigt | RETROCEDER DERECHA
break;
case 'W':
digitalWrite(LED1B, HIGH);
digitalWrite(LED2B, HIGH);
break;
case 'w':
digitalWrite(LED1B, LOW);
digitalWrite(LED2B, LOW);
break;
case 'U':
digitalWrite(LED1R, HIGH);
digitalWrite(LED2R, HIGH);
break;
case 'u':
digitalWrite(LED1R, LOW);
digitalWrite(LED2R, LOW);
break;
case 'V':
sonar();
break;
case 'v':
break;
}
delay(20);
}
void avanzar(){
// Forward | AVANZAR
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
}
void retroceder(){
// Backward | RETROCEDER
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
}
void derecha(){
//Right | DERECHA
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2B, LOW);
analogWrite(motor2A, 100);
}
void giror(){
//Right | DERECHA
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, LOW);
analogWrite(motor2B, 100);
}
void izquierda(){
//Left | IZQUIERDA
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
analogWrite(motor2A, 100);
}
void giroi(){
//Right | DERECHA
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, HIGH);
digitalWrite(motor2A, HIGH);
analogWrite(motor2A, 100);
}
void giroatrasi(){
//Right | DERECHA
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2B, LOW);
analogWrite(motor2A, 100);
}
void giroatrasr(){
//Right | DERECHA
digitalWrite(motor1A, HIGH);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
analogWrite(motor2B, 100);
}
void detener(){
//Stop | DETENER
digitalWrite(motor1A, LOW);
digitalWrite(motor1B, LOW);
digitalWrite(motor2A, LOW);
digitalWrite(motor2B, LOW);
}
void sonar() {
// Iterate through both arrays
// Notice how the iteration variable thisNote is created in the parenthesis
// The for loop stops when it is equal to the size of the melody array
for (int thisNote = 0; thisNote < songLength; thisNote++){
// determine the duration of the notes that the computer understands
// divide 1000 by the value, so the first note lasts for 1000/8 milliseconds
int duration = 1000/ durations[thisNote];
tone(11, melody[thisNote], duration);
// pause between notes
int pause = duration * 1.3;
delay(pause);
// stop the tone
noTone(11);
}
}
The thing is, in the app, once I press "W" it won't accept any other commands until I stop the W.
Some notations are in spanish because im spanish, if you don't understand something, just ask!