Help combining two simple codes: Bricktronics (Lego NXT shield) and Bluetooth

Hi everyone. I'm new to Arduino and I'm trying to make a Lego based robot that I can control with Arduino. I want to use Bricktronics to connect a Lego motor to Arduino. Below is two sample codes I'm using and the last code is a sort of combination of the two.

This is the code to connect an HC-06 Bluetooth module to the Arduino to control the LED connected to pin 13. Using the app Blueterm for Android or nBlueterm for iPhone, typing the letter "a" into the terminal will activate the LED. I have confirmed that this code works.

When I type "a", the TX LED on the Arduino board blinks indicating that the board has received the "a" input from the phone and the pin 13 LED lights up.

void setup()
{
    Serial.begin(9600);
    pinMode(13, OUTPUT);
}

void loop()
{
    if (Serial.available() > 0) {
        char ch = Serial.read();
        Serial.print("Received: ");
        Serial.println(ch);
        if (ch == 'a') {
            digitalWrite(13, HIGH);
        }
        else {
            digitalWrite(13, LOW);
        }
    }
}

Here is the sample code to control a single motor attached to the Bricktronics shield.

#include <Wire.h>
#include <Bricktronics.h>

//Bricktronics Example: SingleMotor
//http://www.wayneandlayne.com/bricktronics
//This example uses a LEGO NXT Motor.

//This example starts the motor at an intermediate speed, then speeds it up to full speed, and does the same in reverse.

//This example uses a motor, so it needs more power than a USB port can give.  We really don't recommend running motors off of USB ports--they will be slow and sluggish, other things won't quite work right, things can get hot--it's just not a good idea.  Use an external power supply that provides between 9V and 7.2V, and can provide at least 600 mA per motor (1A preferably).  Two options that work really well are a 9V wall adapter (2.1mm plug, center positive) or a 6xAA battery pack.

//Connect a motor to Motor Port 1.

Bricktronics brick = Bricktronics();
Motor m = Motor(&brick, 1);

void setup()
{
    Serial.begin(115200);
    Serial.println("Hello world.");
    brick.begin();
    m.begin();
}

void loop() 
{
    while (true)
    {
        Serial.println("Going forward.");
        m.set_speed(75);
        delay(1000);
        m.set_speed(255);
        delay(500);
        Serial.println("Going in reverse.");
        m.set_speed(-75);
        delay(1000);
        m.set_speed(-255);
        delay(500);
        
    }
}

Here, Bricktronics Downloads | Wayne and Layne, you can download the Bricktronics library with examples (I used the first link). Inside is a variety of examples for using Bricktronics but I'm using the SingleMotor one and I have confirmed it works.

Now, what I want to do is combine the two codes such that when I input letter "a" on my phone into Blueterm, the NXT motor will start rotating like it does in the above SingleMotor code. Here is my attempt.

#include <Wire.h>
#include <Bricktronics.h>

Bricktronics brick = Bricktronics();
Motor m = Motor(&brick, 1);

void setup()
{
    Serial.begin(9600);
    Serial.begin(115200);
    pinMode(13, OUTPUT);
    Bricktronics brick = Bricktronics();
    Motor m = Motor(&brick, 1);
}

void loop()
{
    if (Serial.available() > 0) {
        char ch = Serial.read();
        Serial.print("Received: ");
        Serial.println(ch);
        if (ch == 'a') {
            digitalWrite(13, HIGH);
            Serial.println("Going forward.");
            m.set_speed(75);
            delay(1000);
            m.set_speed(255);
            delay(500);
            Serial.println("Going in reverse.");
            m.set_speed(-75);
            delay(1000);
            m.set_speed(-255);
            delay(500);  
        }
        else {
            digitalWrite(13, LOW);
        }
    }
}

This, however does not work. When I type the letter "a", nothing happens. The TX LED lights up anytime I press a key on the phone so I know that Arduino is getting a signal. However, the motor doesn't rotate or react at all. I've also tried to the serial.begin from 9600 to 115200 which is used in the second code whereas the first uses 9600. Again, the TX LED blinks but the motor does nothing.

void loop() 
{
    while (true)
    {

Running an infinite loop inside an infinite loop is stupid beyond description. I would not use a library supplied by someone that thought such foolishness was necessary.

void setup()
{
    Serial.begin(9600);
    Serial.begin(115200);
    pinMode(13, OUTPUT);
    Bricktronics brick = Bricktronics();
    Motor m = Motor(&brick, 1);
}

Why are you creating a second instance of each class in setup()? Both of those instances go away immediately.

Does the app that sends data to the Arduino with bluetooth device receive responses? If so, does the app show that the Arduino received the letter with the combined code?

It's difficult to see from the schematic whether or not the brick shield uses the serial pins or pin 13. If it uses any of D1, D1, or D13, your use of them too will cause problems.

PaulS:

void loop() 

{
    while (true)
    {



Running an infinite loop inside an infinite loop is stupid beyond description. I would not use a library supplied by someone that thought such foolishness was necessary.



void setup()
{
    Serial.begin(9600);
    Serial.begin(115200);
    pinMode(13, OUTPUT);
    Bricktronics brick = Bricktronics();
    Motor m = Motor(&brick, 1);
}



Why are you creating a second instance of each class in setup()? Both of those instances go away immediately.

Does the app that sends data to the Arduino with bluetooth device receive responses? If so, does the app show that the Arduino received the letter with the combined code?

It's difficult to see from the schematic whether or not the brick shield uses the serial pins or pin 13. If it uses any of D1, D1, or D13, your use of them too will cause problems.

Well both codes use a different Serial.begin so I was thinking that it might be necessary to use both of them somehow.

Also, for the first app responds to the first Bleutooth code and displayed any letter when pressed. For my "code", that doesn't happen so I know something is amiss.

And since you say its stupid to be using an infinite loop, how would you change the code in the library? Is removing while (true) enough?

Also, here is some design and theory on the Bricktroics shield.

https://www.wayneandlayne.com/projects/bricktronics-shield/design/

According to them, 0 and 1 as well as pin 13 are not used.

Bump