Help with Science Fair Project Code.

Edited due to cross posting(Sorry about that ^.^)

Dear members of the Arduino Forums,
I am making a bluetooth controlled robot for my schools science fair and while programming the Arduino Uno, I have ran into some complications. I would be extremely grateful if anybody could assist me.
First off the parts that I am using are the following:

http://www.amazon.com/gp/product/B006RBK9ZW/ref=oh_details_o02_s00_i00?ie=UTF8&psc=1
I am using this app on my android phone to communicate with the Bluetooth Serial Slave:
https://play.google.com/store/apps/details?id=com.inex.BlueStickControl&hl=en this app sends the following codes to the bluetooth serial slave : Screenshot - 4e2c8be179f06630a736672d19f5249a - Gyazo. However whenever I open up my serial monitor I will see things like 08, and 02 instead of the listed codes. I am fairly new to the world of electronics and this is my first project ever.
I used this tutorial to learn how my motor shield works:
http://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/[/spoiler]
And I consulted the arduino.cc website for information on how to use the newping library. Now, my problem: My code doesn't work for some reason, based on all of the research that I have done it should be working just fine, but for some reason when I use the application to send bluetooth commands the motors don't move. The same is true for when I place something close to the distance sensor. I am deeply sorry if there is some novice error in the code as I am also quite new to the world of programming as well.
Code:

char Direction;// This variable has a global scope and will determine the direction.
#define BT_RX_PIN 16
#define BT_TX_PIN 17
#include <NewPing.h>
#include <SoftwareSerial.h>
#define TRIGGER_PIN  14
#define ECHO_PIN     15
#define MAX_DISTANCE 200
NewPing DistanceSensor(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
SoftwareSerial BTSerial(BT_RX_PIN, BT_TX_PIN);
int close = DistanceSensor.ping_cm();
void setup() {
  //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
  Serial.begin(9600); // starts serial communication at 9600 baud
}

void loop(){
  if (Serial.available()) // Check for serial input
{ Direction = Serial.read(); // If there is input set the Direction variable to that value.
}
if(Direction == '0x38')
{
  //Motor A forward @ full speed
  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

  delay(3000);

  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(9, HIGH);  //Engage the Brake for Channel B
}

 if (Direction == '0x32')
 {

  //Motor A backward @ full speed
  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 half speed

  //Motor B forward @ full speed
  digitalWrite(13, LOW); //Establishes forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);   //Spins the motor on Channel B at full speed

  delay(3000);

  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(9, HIGH);  //Engage the Brake for Channel B
 }
  DistanceSensor.ping_cm(); //Ping, and set close variable.
  if (close < 10); // If close <10cm move backwards.
  {
     //Motor A backward @ full speed
  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 
  //Turn Right

  //Motor B backward @ full 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 full speed

  }

}

Thank you so very much for your time.

Schnitter,

Welcome! So are you a middle schooler or maybe high schooler? I judge at my regional science fair but haven't seen any arduino projects. Good post! Got all the info necessary for help.

I will take a stab at the bluetooth controller code issue. You ARE seeing the right values. The 0X30 is hexadecimal value, in decimal it is 48, or if your serial monitor interprets it as a character, it corresponds to the character "0". 0X34 is character "4", etc. Google ASCII code you will find reference. In your arduino code, you can do

if (iDirection=="0") do stop stuff.

You have a 3 second delay in your main loop, not a good thing. Your robot will go in one direction and ignore your control during the delay. Reduce it to say 500 millisecond for now.

MOVED:

You are doing the same thing I did for my college seminar project. Mine was made with Legos, my own motor driver, a UNO Rev3 and a Samsung Galaxy tablet.

This is an older version, but it relys on single characters to control the robot. I also have the better version, but we will get to that later.

/*
simple control test
*/

volatile int gear = 150; //default speed First Gear

volatile char val;         // variable to receive data from the serial port

byte M1L = 3;              // PWM left drive motor 
byte M2L = 5;              // PWM left drive motor
byte M1R = 9;              // PWM right drive motor
byte M2R = 6;              // PWM right drive motor
byte ledpin = 13;          // LED connected to pin 2 (on-board LED)

/*IMPORTANT*/
//M1 => High & M2 => Low: forward movement
//M1 => Low & M2 => High: Backward movement

void setup()
{
  pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
  pinMode(M1L, OUTPUT);                                
  pinMode(M1R, OUTPUT);
  pinMode(M2L, OUTPUT);                                
  pinMode(M2R, OUTPUT);
  Serial.begin(9600);       // start serial communication at 115200bps
  
  Serial.println("Commands, Press * to bring up help menu");
}

void loop() {
  if( Serial.available() )       // if data is available to read
  { 
    digitalWrite(ledpin, HIGH);
    val = Serial.read();
    volatile int skewGear = gear/2;
    if(val == '1')
    {
      Serial.println("First Gear ");
      gear = 150;
    }
    else if (val == '2')
    { 
      Serial.println("Second Gear ");
      gear = 200;
    }
    else if (val == '3')
    { 
      Serial.println("Third Gear ");
      gear = 255;
    }


    if( val == 'W' || val == 'w' )//forwards               
    {
      Serial.println("Moving Forwards"); 
      //      Left Motor     :     Right Motor
      analogWrite(M1L, gear); 
      analogWrite(M1R, gear);
      digitalWrite(M2L, LOW); 
      digitalWrite(M2R, LOW);                 
    }
    else if( val == 'S' || val == 's' )//backwards              
    {
      Serial.println("Moving Backwards");
      digitalWrite(M1L, LOW); 
      digitalWrite(M1R, LOW);
      analogWrite(M2L, gear); 
      analogWrite(M2R, gear);                   
    }
    else if( val == 'A' || val == 'a' )//Left         
    {
      Serial.println("LEFT");
      digitalWrite(M1L, LOW); 
      analogWrite(M1R, gear);
      analogWrite(M2L, gear); 
      digitalWrite(M2R, LOW);                      
    }
    else if( val == 'D' || val == 'd' )              
    {
      Serial.println("RIGHT");   
      analogWrite(M1L, gear);
      digitalWrite(M1R, LOW);
      digitalWrite(M2L, LOW); 
      analogWrite(M2R, gear);                 
    }
    else if( val == 'T' || val == 't' )              
    {
      Serial.println("Skew Left Forward");   
      analogWrite(M1L, skewGear); 
      analogWrite(M1R, gear);
      digitalWrite(M2L, LOW); 
      digitalWrite(M2R, LOW);                  
    }
    else if( val == 'Y' || val == 'y' )              
    {
      Serial.println("Skew Right Forward");  
      analogWrite(M1L, gear); 
      analogWrite(M1R, skewGear); 
      digitalWrite(M2L, LOW); 
      digitalWrite(M2R, LOW);                  
    }
    else if( val == 'G' || val == 'g' )              
    {
      Serial.println("Skew Left Backward");   
      digitalWrite(M1L, LOW); 
      digitalWrite(M1R, LOW);
      analogWrite(M2L, skewGear); 
      analogWrite(M2R, gear);                  
    }
    else if( val == 'H' || val == 'h' )              
    {
      Serial.println("Skew Right Backward");   
      digitalWrite(M1L, LOW); 
      digitalWrite(M1R, LOW);
      analogWrite(M2L, gear); 
      analogWrite(M2R, skewGear);                  
    }
    else if(val == ' ' || val == 'x' || val == 'X'){  
      digitalWrite(M1L, LOW); 
      digitalWrite(M1R, LOW); 
      digitalWrite(M2L, LOW); 
      digitalWrite(M2R, LOW);
    }
    else if(val == '*'){
      Help();
    }    
  }
  else{
    digitalWrite(ledpin, LOW);
  }
}

void Help() {
  Serial.println("W = Forward");
  Serial.println("S = Backward");
  Serial.println("A = Left");
  Serial.println("D = Right");
  Serial.println("T = Skew Forward Left");
  Serial.println("Y = Skew Forward Right");
  Serial.println("G = Skew Backward Left");
  Serial.println("H = Skew Backward Right");
  Serial.println("Space Bar = All stop");
}

He has the same post in Programming Questions.

if (close < 10); // If close <10cm move backwards.

Why the ;

:stuck_out_tongue:

No cross posting!

Topics merged.

Sorry about that. I am in 10th grade at the moment! I did that post in the programming section for a focus on the code, and this post here for a more hardware related approach. Now that I think about it that was a tad stupid. Please forgive me!

Don't worry about it, you are not the first, nor will you be the last ...

The logic here is wrong:

if (Serial.available()) // Check for serial input
{ Direction = Serial.read(); // If there is input set the Direction variable to that value.
}
if(Direction == '0x38')
{ //...

All the if (Direction=='8') etc. should be inside the first if that reads Serial. WIth your current code, if arduino receives one command once, say '8', it will do the if (Direction...) and then next loop cycle it will do it again, since the Direction will still be '8'. Putting these if statements inside the if that reads serial port will make these actions only run if there is a new command.

liudr:
The logic here is wrong:

if (Serial.available()) // Check for serial input

{ Direction = Serial.read(); // If there is input set the Direction variable to that value.
}
if(Direction == '0x38')
{ //...




All the if (Direction=='8') etc. should be inside the first if that reads Serial. WIth your current code, if arduino receives one command once, say '8', it will do the if (Direction...) and then next loop cycle it will do it again, since the Direction will still be '8'. Putting these if statements inside the if that reads serial port will make these actions only run if there is a new command.

Oh, that explains a lot. I was trying to fix that problem a moment ago and I decided to come here to see if anybody had posted something else! Thank you so very much, i'll re factor my code now.

You are welcome. Usually your school teacher is involved in your science fair project. I just want to know how you got to know about Arduino, through your teacher, or someone else?

I remember teaching myself C at your age. It was not easy and I couldn't do much beyond printing stuff on the computer screen. Arduino certainly changed a lot of what teens can do these days.

liudr:
You are welcome. Usually your school teacher is involved in your science fair project. I just want to know how you got to know about Arduino, through your teacher, or someone else?

I remember teaching myself C at your age. It was not easy and I couldn't do much beyond printing stuff on the computer screen. Arduino certainly changed a lot of what teens can do these days.

I learned about the Arduino Uno via youtube, I was mesmerized by it's ability to bring projects like this into fruition and I decided to get one and use it in my science fair project as a primer to the world of electronics. So far it's been awesome.

Good for you! Please spread the word of arduino to your friends and teachers. I'm sure the science fair will be an awesome way to spread the word.

This may be of some help.

I'm almost golden! There's just a few kinks that I can't sort out(I've been sitting here for five hours and have school in two). The bluetooth doesn't display any information from the apps i'm using to the serial display anymore, and after the ultrasonic sensor does it's work the motors won't stop moving. Aye, I don't have proper control of the motors and the one thing that does isn't using them the way i'd like. Oh, the irony.

volatile char Direction;// This variable has a global scope and will determine the direction.
volatile int distance = 0;
#define trigPin 7
#define echoPin 6
#include <SoftwareSerial.h>
#define BT_RX_PIN 16
#define BT_TX_PIN 17
SoftwareSerial BTSerial(BT_RX_PIN, BT_TX_PIN);
void setup() {
  //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 B pin
  pinMode(8, OUTPUT); //Initiates Brake Channel B pin
  Serial.begin(9600); // Normal Serial
  BTSerial.begin(9600); // Serial for bluetooth
}

void loop(){
  if (BTSerial.available() > 0) // Check and see if there is serial input
{ Direction = Serial.read(); // If there is input set the Direction variable to that value.
{
if(Direction == '1')
{
 //Motor A backward @ full speed
  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 @ full 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 full speed
  delay(500);
  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(8, HIGH);  //Engage the Brake for Channel B
}
 if (Direction == '2')
 {
  
  //Motor A forward @ full speed
  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 forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);    //Spins the motor on Channel B at full speed
  delay(500);
  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(8, HIGH);  //Engage the Brake for Channel B

  
 }
 if (Direction == '3')
 {
   //Turning Left
  
  //Motor A forward @ half speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 123);   //Spins motor at about half speed
  //Motor B Forward @ full speed
  digitalWrite(13, HIGH);  //Establishes forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);    //Spins the motor on Channel B at full speed
  delay(500);
  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(8, HIGH);  //Engage the Brake for Channel B

  
 }
  if (Direction == '4')
 {
   //Turning Right
  
  //Motor A forward @ half speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 225);   //Spins motor at full speed
  //Motor B Forward @ full speed
  digitalWrite(13, HIGH);  //Establishes forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 123);    //Spins the motor on Channel B at half speed
  delay(500);
  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(8, HIGH);  //Engage the Brake for Channel B

  
 }
}
}
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
 
digitalWrite(trigPin, HIGH);
// delayMicroseconds(1000); - Removed this line
delayMicroseconds(10); // Added this line
digitalWrite(trigPin, LOW);
 
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
if(distance < 20)
{
    //Motor A backward @ full speed
  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 @ full speed
  digitalWrite(13, LOW); //Establishes backward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 123);    //Spins the motor on Channel B at about half speed
  delay(500);
  analogWrite(11, 225); //Spins the motor on Channel B at full speed
  delay(500);
  digitalWrite(8, HIGH);
  digitalWrite(9, HIGH);
  
}
}

You are aware that the only time you get measurement is when your not sending anything over Bluetooth. Make the "ping" section a function that you can call when you need it, instead of how you have it now.

Also if your currently using the serial monitor to test the code, then why are you checking if the Bluetooth is sending something when your reading from the serial monitor?

if (BTSerial.available() > 0) // Check and see if there is serial input
{ Direction = Serial.read(); // If there is input set the Direction variable to that value.
{ <= unneeded bracket, get rid of it and its closing counterpart.

HazardsMind:
You are aware that the only time you get measurement is when your not sending anything over Bluetooth. Make the "ping" section a function that you can call when you need it, instead of how you have it now.

Also if your currently using the serial monitor to test the code, then why are you checking if the Bluetooth is sending something when your reading from the serial monitor?

if (BTSerial.available() > 0) // Check and see if there is serial input
{ Direction = Serial.read(); // If there is input set the Direction variable to that value.
{ <= unneeded bracket, get rid of it and its closing counterpart.

Thanks for the tip! I thought that the distance sensor inputs might screw up those from the app on my phone...that's why I added the BTSerial. Was I incorrect in my thought process?

So wait, when the robot does not receive a signal from the app, it is supposed to rely on the sensor? If that is the case, then you had it correct.

But this still doesn't seem correct. With this part here, you won't be able to get any data unless the bluetooth app is actually sending something. However, like I said, if you want to test it with the serial monitor then you need change BTSerial to just Serial.

if (BTSerial.available() > 0) // Check and see if there is serial input
{ Direction = Serial.read(); // If there is input set the Direction variable to that value.