I can't test this, so if it does not work, play around with it and get it to work by learning from your mistakes.
Code:
#include <SoftwareSerial.h>
#define rxPin 5
#define txPin 6
// Sukuriamas naujas servo objektas
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
int qualifier;
int dataByte;
void setup()
{
// Apra6omi tx, rx išėjimai:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
//SoftwareSerial aprašymas
Serial.begin(9600);
mySerial.begin(9600); // BT modulis
//====================================================
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin
}
void loop() // Vykdymas be pristabdymų
{
if (Serial.available()) // from USB
mySerial.write(Serial.read());
if(mySerial.available()>1) // from Bluetooth
{
qualifier=mySerial.read();
dataByte=mySerial.read();
mySerial.print("qualifier = ");
mySerial.println(qualifier);
mySerial.print("dataByte = ");
mySerial.println(dataByte);
if ( qualifier == 68)
{
if (dataByte == 1)
{
forward();
}
if (dataByte == 2)
{
left();
}
if (dataByte == 3)
{
right();
}
if (dataByte == 4)
{
backward();
}
if (dataByte == 5)
{
stop();
}
}
}
}
void stop(){
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(9, HIGH); //Engage the Brake for Channel B
}
void forward(){
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, HIGH); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}
void backward(){
digitalWrite(12, LOW); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, LOW); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}
void left(){
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, LOW); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}
void right(){
digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, HIGH); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}
#define rxPin 5
#define txPin 6
// Sukuriamas naujas servo objektas
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);
int qualifier;
int dataByte;
void setup()
{
// Apra6omi tx, rx išėjimai:
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
//SoftwareSerial aprašymas
Serial.begin(9600);
mySerial.begin(9600); // BT modulis
//====================================================
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin
}
void loop() // Vykdymas be pristabdymų
{
if (Serial.available()) // from USB
mySerial.write(Serial.read());
if(mySerial.available()>1) // from Bluetooth
{
qualifier=mySerial.read();
dataByte=mySerial.read();
mySerial.print("qualifier = ");
mySerial.println(qualifier);
mySerial.print("dataByte = ");
mySerial.println(dataByte);
if ( qualifier == 68)
{
if (dataByte == 1)
{
forward();
}
if (dataByte == 2)
{
left();
}
if (dataByte == 3)
{
right();
}
if (dataByte == 4)
{
backward();
}
if (dataByte == 5)
{
stop();
}
}
}
}
void stop(){
digitalWrite(9, HIGH); //Engage the Brake for Channel A
digitalWrite(9, HIGH); //Engage the Brake for Channel B
}
void forward(){
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, HIGH); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}
void backward(){
digitalWrite(12, LOW); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, LOW); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}
void left(){
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, LOW); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}
void right(){
digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
//Motor B backward @ half speed
digitalWrite(13, HIGH); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at half speed
}