This is the code to control 2 motors with a Bluetooth module the problem is that when the Serial receives a letter, let's say "s" it stays inside the loop forever and the motors keep on moving, what I want is that when the Bluetooth serial communication I want my motors to move just when the "s" is being kept pressed, like a push button or a remote control. Do you have any solutinos that worked for you? Suggestions on what to try?
Thanks in advance!
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor.
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
*/
#include <SoftwareSerial.h>// import the serial library
//bluetooth config
SoftwareSerial Genotronex(9, 10); // RX, TX
int ledpin=13; // led on D13 will show blink on / off
int ledpin2 = 12;
int BluetoothData; // the data given from Computer
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
//motors port config
int motor1a=2;
int motor1b=3;
int motor2a=4;
int motor2b=5;
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Genotronex.begin(9600);
Genotronex.println("Bluetooth On please press 1 or 0 blink LED ..");
Serial.begin(9600);
pinMode(13, OUTPUT);
pinMode(motor1a, OUTPUT);
pinMode(motor1b, OUTPUT);
pinMode(motor2a, OUTPUT);
pinMode(motor2b, OUTPUT);
myservo.attach(11); // attaches the servo on pin 9 to the servo object
}
// the loop routine runs over and over again forever:
void loop() {
//recibir bits bluetooth
if (Genotronex.available())
{
BluetoothData=Genotronex.read();
}
if (BluetoothData == 's')
{
//atras
digitalWrite(motor2a, HIGH);
digitalWrite(motor2b, LOW);
digitalWrite(motor1a, HIGH);
digitalWrite(motor1b, LOW);
}
else if (BluetoothData == 'w')
{
//frente
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, HIGH);
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, HIGH);
}
else if (BluetoothData == 'a')
{
//izquierda
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, HIGH);
digitalWrite(motor1a, HIGH);
digitalWrite(motor1b, LOW);
}
else if (BluetoothData == 'd')
//derecha
{
digitalWrite(motor2a, HIGH);
digitalWrite(motor2b, LOW);
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, HIGH);
}
else
{
digitalWrite(motor1a,LOW);
digitalWrite(motor1b,LOW);
digitalWrite(motor2a,LOW);
digitalWrite(motor2b,LOW);
}
// print out the value you read:
delay(1); // delay in between reads for stability
}