Let's Make Robots!

Chris the Carpenter Please read

So after doing the things you suggested the other day I have a little better understanding. My teacher and I did alittle research and came across a code you wrote  and I had some questions. it has to pertain to the rocketbot app which you wrote it for. I uploaded that code to my bot to see if i could get the app to work I was able to get the data qualifier bit to receive but the data byte shows up a block please help me out. This is the code you wrote but it doesnt work so maybe I'm thinking i have the wires all hooked up wrong on mine.

/* Android Controlled Robotshop Rover
Author - Jim Winburn > Appiphania.com
uses Chris Carpenter's RocketBrand Android Controller
Note : Directional Function only in this example.
D1 - Forward
D2 - Left
D3 - Right
D4 - Reverse
D5 - Stop
*/
#include <NewSoftSerial.h>
#define BlueSmirf_RxPin 2
#define BlueSmirf_TxPin 3

NewSoftSerial nss(BlueSmirf_RxPin,BlueSmirf_TxPin);

int E1 = 6; //M1 Speed Control
int E2 = 5; //M2 Speed Control
int M1 = 8; //M1 Direction Control
int M2 = 7; //M2 Direction Control

int leftspeed;
int rightspeed;

int i;
int qualifier;
int dataByte;

void setup()
{
//Serial.begin(9600);// on for debug
nss.begin(38400); // best baud for Motorola Droid tested

// set up motors

for(i=5;i<=8;i++)
pinMode(i, OUTPUT);

// Motor tuning - since this is a track vehicle
// and motors are never exact, they must be tuned ...
//255 is maximum speed

leftspeed = 255; //adjust to balance motors
rightspeed = 195;//adjust to balance motors

}
void loop()
{

if(nss.available()>1)
{
qualifier=nss.read();
dataByte=nss.read();
}

/* uncomment below to debug with serial monitor
Serial.print(byte(qualifier)); //will display "D" instead of "68"
Serial.print(" : ");
Serial.print(dataByte);
Serial.println("");
delay(1000);
*/

switch (qualifier)
{

case 'D':

switch (dataByte)
{

case 1: //forward

forward (leftspeed,rightspeed);
break;

case 2: //left

left (leftspeed,rightspeed);
break;

case 3: //right

right (leftspeed,rightspeed);
break;

case 4: //reverse

reverse (leftspeed,rightspeed);
break;

case 5: //stop
stop ();
break;

}
}

qualifier=0;
dataByte=0;

}
// functions

void stop(void) //Stop
{
digitalWrite(E1,LOW);
digitalWrite(E2,LOW);
}
void forward(char a,char b) //Forward
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void reverse (char a,char b) //Reverse
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,HIGH);
}
void left (char a,char b) //Left
{
analogWrite (E1,a);
digitalWrite(M1,HIGH);
analogWrite (E2,b);
digitalWrite(M2,LOW);
}
void right (char a,char b) //Right
{
analogWrite (E1,a);
digitalWrite(M1,LOW);
analogWrite (E2,b);
digitalWrite(M2,HIGH);

Comment viewing options

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

It looks pretty good actually. The serial coming in is correct --in terms of grabbing both the qualifier and the data byte. If you are running this on a mega, you can drop the nss and just use the second ("hardwire") serial connection. But hey, if it works, it shouldn't matter. All in all, it looks fine.