Let's Make Robots!

Help with programing ASAP

I have the dagu rover 5 chassis with 2 encoders and the dagu 4 channel motor controller and the redback spider atmega 1280. I purchased the rocketbot app for android to control my robot. I was able to hit the forward button and make my bot move forward but that is it it wont stop it wont move left or right or even  please help asap I have a week and 2 days before my capstone for ITT Tech here is what i have so far for the code. I really just need it to be real basic.

// Included for serial communication
#include <SoftwareSerial.h>
#define motorA 9 //Direction Pin for Left Motor
#define motorSpeedA 10 //Speed Pin for Left Motor


#define motorB 12 //Direction Pin for Right Motor
#define motorSpeedB 11 //Speed Pin for Right Motor

 
// Define pins you're using for serial communication
// for the BlueSMiRF connection
#define TXPIN 1
#define RXPIN 0
 
// Create an instance of the software serial object
SoftwareSerial BlueSerial(RXPIN, TXPIN);
 
// Main application entry point
void setup()
{
  pinMode(motorA, OUTPUT);
  pinMode(motorB, OUTPUT);

  pinMode(motorSpeedA, OUTPUT);
  pinMode(motorSpeedB, OUTPUT);
    // Define the appropriate input/output pins
    pinMode(RXPIN, INPUT);
    pinMode(TXPIN, OUTPUT);
 
    // Begin communicating with the bluetooth interface
    Serial.begin(9600);
    BlueSerial.begin(9600);
 
    // Say we are starting the serial com
    Serial.println("Serial start!");
    BlueSerial.println("Serial start!");
}
 
// Main application loop
void loop()
{
    // Wait for command-line input
    if(Serial.available() > 0)
    {
        // Read off all bytes
        BlueSerial.print( Serial.read(), BYTE );
         analogWrite(motorSpeedA,255);
  analogWrite(motorSpeedB,255);   
  forward(); 
  backward();
  leftSpin();
  rightSpin();
 
    }
}
void forward()
{
   //forward
 
  digitalWrite(motorA,HIGH);
  digitalWrite(motorB,HIGH);
}

void backward()
{
    
 
  digitalWrite(motorA,LOW);
  digitalWrite(motorB,LOW); 
}

void leftSpin()
{
 
  digitalWrite(motorA,LOW);
  digitalWrite(motorB,HIGH);
}

void rightSpin()
{
 
  digitalWrite(motorA,HIGH);
  digitalWrite(motorB,LOW);
}


void allStop()
{
 
  digitalWrite(motorA,LOW);
  digitalWrite(motorB,LOW);

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Chris the Carpenter's picture

First, get rid of the BT unit, get rid of the phone. You only need a arduino and the serial monitor.

Start with sending an "A" (65) from the serial monitor to the Arduino. When the arduino receives this "A", blink a LED.

Next, recode so when you send a "B" blink a different LED. 

Learn how "switch case" works and use it in the above code. LIke this:

Switch <serial byte coming in>

case "A"

blink the first LED

case "B"

blink the second LED

When you get this working, try receiving 2 bytes, a letter then a number. Write your code so when you get an "A" and say, a "3", your first LED will blink 3 times. If you can get this far, you are 90% there. --Assuming this works...

Read the datasheet for your Bluesmirf and find the default baud rate. I think it is 115,200 but I could be wrong. I would suggest changing your code to match the bluesmirf setting instead of trying to reconfigure the bluesmirf itself.

Now, as it happens, the App uses a Letter/Number combination to send data, so assuming that you got your blink thing going, the app should work with your "A"/"B" test code just as it sits. The aux buttons will send an "A" then a number so you should be able to hit any of the aux buttons and have your LED blink from there.

In terms of wiring, you need to familiarize yourself with the 2 serial connections on that Mega --For the serial monitor, you would be using serial 0. If you use this connection for the App, be sure you are indeed connected to those pins. Once connected, power up your mega and the BT unit, pair and connect and try to send something. By now, you should have a better understanding of how a serial connection works and will be better able to iron out any of the wrinkles.

 

 

Chris the Carpenter's picture

It seems like this might not be an issue with the app, or the platform but with some fairly basic concepts with code. It seems that you may have jumped to step 487 before learning steps 1 through 10. I don't say this to be mean, I say this because it may be hard to convey the issues with this code without these basic concepts being understood. As an example, is all 4 routines, forward, backward and both spins all stacked up in your main loop. Ok, let's get into the meat of this --I'm going to start at the top and work down.

It seems you are trying to use both the regular serial and an instance of softserial on the same pins. Your regular "hardwired" serial is on pins 0 and 1, and you also have your soft serial attached to those same pins. I am unclear if you are trying to use the bluesmirf with the soft serial connection or not. I am also unclear as to if you are trying to debug using the serial monitor at the same time. --Again, both are trying to use the same pins so there is going to be a problem no matter what.

In the setup, you are using a line feed at the end of your serial "prints" --This is not a huge issue, but the phone will probably try to "speak" the carrage return. Remove these unless one of them is going to the serial monitor.

In the main loop, you have a serial read inside a serial print. This is not bad, but it will do nothing in terms of controlling the robot. All you are doing is bouncing whatever is coming in, back to the sender. Actually, it is coming in on one serial connection, then going out on the second. (back to the same pin problem) Also, you are only reading one of the 2 bytes coming in. Every send from the app comes in 2 bytes --a qualifier and then a data byte. I.e. the "forward" button would send D and then 1. D is the qualifer (direction) and 1 is which direction. An aux button would send A then the number of the button pushed. You need to receive both bytes. It would be something like this:

If there is any serial available, read it and stick it in 2 byte variables, qualifier and data.
Switch case qualifer

Case "A"
if data is a 1 do this
if data is a 2 do that
if data is a 3 do something else

Case "D"
if data is a 1 then drive one way
if data is a 2 then drive another way
if idata is a 3 then...

As it stands now, your main loop goes like this:

if there is any serial data available, send it back 
Set the speed of the motors (this only really needs to be done once unless it changes)
go forward 
immediately go backward
immediately spin left
immediately spin right
loop

To be honest, I don't know where to go from here. If you look at the arduino example code here you can see an example of how to receive the 2 bytes, use a switch case to determine what the qualifier is, then use the "data byte" to do something.

I am going to leave it at that, I am sorry if I sorta threw a lot at you there, please let me know if I was unclear anywhere, or if I under explained anything. Better yet, let me know if anything I said made any sense at all. :)

We can go from there. 

TinHead's picture

First change this to the correct pins for the bluesmirf (the pins where you have it connected), 1 and 0 are the first serial and it is used for programming:

#define TXPIN 1
#define RXPIN 0

Then this:

  if(Serial.available() > 0)
    {
        // Read off all bytes
        BlueSerial.print( Serial.read(), BYTE );
        analogWrite(motorSpeedA,255);
        analogWrite(motorSpeedB,255);    
       forward();  
       backward();
       leftSpin();
       rightSpin();

    }

The above is just a mess what you're doing there is something like:

if something is received on serial then set both speeds to maximum (that's why it goes crazy forward), the you simply call forward, backward leftspin rightspin. This won't likley do what you want right? Oh yeah and there is no point in writing back that byte to the phone, unless CTC's app expects that.

So what you need is to wait for a special character the phone sends when you press forward for example and then call the function which does that. Now CTC can tell you which character get's sent for what move, and you should handle that in a switch case. And yes a switch case is expalined here: http://arduino.cc/en/Reference/SwitchCase (cause I know you're gonna ask :) ) 

lumi's picture

As I understand your code is not working at all. First you need to change the code to run your motors, since it needs more than these two lines:

for forward use this:

digitalWrite(motorAF, HIGH); //Motor A forward

digitalWrite(motorBF, HIGH);  //Motor B forward

digitalWrite(motorAR, LOW);  //Motor A reverse

digitalWrite(motorBR, LOW);  //Motor B reverse

Trys this and it should work.

In your loop is nothing what check the received data to tell the motors what to do. After your serial command you need to take the received data and use this to call your functions.