ArduinoBlue App and DC Motor Control

I am working with some high school students to create a “BattleBots” type club and we would like to use our iPhones to control our robots. We have the ArduinoBlue app installed and communicating successfully with our robot.

Our problem is controlling the DC motors speed and direction using the commands from the ArduinoBlue Joystick. We are using an L298N to drive the DC motors but need help with the code. Attached is what we have so far.

Any help is greatly appreciated.

<#include <ArduinoBlue.h>
#include <SoftwareSerial.h>
/*

  • Demo program to demonstrate the features of the app.
    */

// Define Motors
int enA = 5;
int MotorAF = 6;
int MotorAR = 7;
int enB = 10;
int MotorBF = 8;
int MotorBR = 9;

// If the throttle value is within 5 from zero, then it will be considered 0.
const int THROTTLE_ZERO_THRESHOLD = 5;
const int MIN_THROTTLE = 10;
const int STILL = 90;
const int LEFT_FORWARD_MAX = 130;
const int LEFT_BACKWARD_MAX = 50;
const int RIGHT_FORWARD_MAX = LEFT_BACKWARD_MAX;
const int RIGHT_BACKWARD_MAX = LEFT_FORWARD_MAX;
const int LEFT_FORWARD_MIN = STILL + MIN_THROTTLE;
const int LEFT_BACKWARD_MIN = STILL - MIN_THROTTLE;
const int RIGHT_FORWARD_MIN = LEFT_BACKWARD_MIN;
const int RIGHT_BACKWARD_MIN = LEFT_FORWARD_MIN;

// The bluetooth tx and rx pins must be supported by software serial.
// Visit Arduino - SoftwareSerial for unsupported pins.
// Bluetooth TX → Arduino D8
const int BLUETOOTH_TX = 2;
// Bluetooth RX → Arduino D7
const int BLUETOOTH_RX = 3;

int prevThrottle = 49;
int prevSteering = 49;
int throttle, steering, sliderVal, button, sliderId;
int Forward = 0;

SoftwareSerial bluetooth(BLUETOOTH_TX, BLUETOOTH_RX);
ArduinoBlue phone(bluetooth); // pass reference of bluetooth object to ArduinoBlue constructor.

// Setup code runs once after program starts.
void setup() {
// Start serial monitor at 9600 bps.
Serial.begin(9600);

// Start bluetooth serial at 9600 bps.
bluetooth.begin(9600);

// delay just in case bluetooth module needs time to “get ready”.
delay(100);

Serial.println(“Robot Ready!”);
}

// Put your main code here, to run repeatedly:
void loop() {
// ID of the button pressed pressed.
button = phone.getButton();

// Returns the text data sent from the phone.
// After it returns the latest data, empty string “” is sent in subsequent.
// calls until text data is sent again.
String str = phone.getText();

// Throttle and steering values go from 0 to 99.
// When throttle and steering values are at 99/2 = 49, the joystick is at center.
throttle = phone.getThrottle();
steering = phone.getSteering();

// ID of the slider moved.
sliderId = phone.getSliderId();

// Slider value goes from 0 to 200.
sliderVal = phone.getSliderVal();

// Display button data whenever its pressed.
if (button != -1) {
Serial.print("Button: ");
Serial.println(button);
digitalWrite (13, HIGH);
delay(500);
digitalWrite (13, LOW);
}

// Display slider data when slider moves
if (sliderId != -1) {
Serial.print("Slider ID: “);
Serial.print(sliderId);
Serial.print(”\tValue: ");
Serial.println(sliderVal);
}

// Display throttle and steering data if steering or throttle value is changed
if (prevThrottle != throttle || prevSteering != steering) {
Serial.print("Throttle: “); Serial.print(throttle); Serial.print(”\tSteering: "); Serial.println(steering);
prevThrottle = throttle;
prevSteering = steering;
// Read Joystick values and map to values of 0 - 255

Forward = map(throttle, 49, 130, 0, 250);
//joystick[1] = map(analogRead(JoyStick_Y_PIN), 0, 1023, 0, 255);
digitalWrite(enA, HIGH);
digitalWrite(MotorAF, Forward);
}

// If a text from the phone was sent print it to the serial monitor
if (str != “”) {
Serial.println(str);
}

// Send string from serial command line to the phone. This will alert the user.
if (Serial.available()) {
Serial.write("send: ");
String str = Serial.readString();
phone.sendMessage(str); // phone.sendMessage(str) sends the text to the phone.
Serial.print(str);
Serial.write(’\n’);
}
}

Arduino_Blue_Car_Working.ino (4.41 KB)

So what does your program actually do? You seem to have two motors but you only ever write anything to one of them. And you're doing a digitalWrite of a value you've just mapped to 0 to 250 when digitalWrite can't use analog values like that.

Have you looked at any other projects using L298N motor drivers (or the datasheet for the L298N) to see how they should be used? Googling "Arduino L298N" will find thousands of useful examples.

Steve