Receiving Serial Data from Kinect to control two DC motor speeds

Hello,

I am new to arduino so please bare with me. I currently have Arduino Uno R3 and Arduino Motor Shield. I have used Processing 1.5.1 to get 3D kinect data and then sent this information over to arduino as an integer value through serial ports.

I am trying to use this integer value to set a threshold for two motors labeled A and B to move faster when the user is far away. Basically having both wheels spin forward until the user's z-distance (information sent from kinect to arduino) is under a some threshold. I do not know how to access the data to begin setting up my constraint.

my code is as follows:

// give the motor control pins names:
const int pwmA = 3;
const int pwmB = 11;
const int brakeA = 9;
const int brakeB = 8;
const int dirA = 12;
const int dirB = 13;

void setup() {

Serial.begin(9600);  //set up Serial library at 9600 bps

//Setup Channel A
pinMode(dirA, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeA, OUTPUT); //Initiates Brake Channel A pin

//Setup Channel B
pinMode(dirB, OUTPUT); //Initiates Motor Channel A pin
pinMode(brakeB, OUTPUT); //Initiates Brake Channel A pin
}

int get = 165;
int motorForward(){
 analogWrite(pwmB, get);
 analogWrite(pwmA, get);

 delay(1000);

 //Motor A forward at half speed
 digitalWrite(dirA, HIGH); //Establishes forward direction of Channel A
 digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
 //Motor B backward @ half speed
 digitalWrite(dirB, LOW); //Establishes backward direction of Channel B
 digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B

 Serial.println(get);
 delay(1000);
}

int motorBackward() {
 digitalWrite(dirA, LOW); //Establishes backward direction of Channel A
 digitalWrite(brakeA, LOW); //Disengage the Brake for Channel A
 analogWrite(pwmA, get); //Spins the motor on Channel A at half speed

 digitalWrite(dirB, HIGH); //Establishes forward direction of Channel B
 digitalWrite(brakeB, LOW); //Disengage the Brake for Channel B
 analogWrite(pwmB, get); //Spins the motor on Channel B at full speed

  Serial.println(get);
  delay(1000);

}

void loop(){
  if(Serial.available())
  motorForward();
  motorBackward();
}

I have been experimenting with code and I'd like to control both motors independently and maintain a distance from the user with this data.

I attempted to write this code, but I do not know if I am going about this the right way, please help.

void loop()
{
	for(int=0; i<=n; i++)   //accel. Motor from 0 to 255
	{ analogWrite(pwmB, i);
 	   analogWrite(pwmA, i);
	  delay(10);
}

I also do not know how to call the data sent in from serial.port some value like n in the for loop above. Thanks in advance.

Sometime after Serial.available() you are going to have to call Serial.read()....

Ok.. 'Sometime after' meaning still within my loop. Would I make this an if statement or can I go ahead and write my constraint from the read function? See below.

void loop(){
  if(Serial.available())
  motorForward();
  motorBackward();
  
  if(Serial.read()){
     for(int=0; i<=n; i++)   //accel. Motor from 0 to 255
	{ analogWrite(pwmB, i);
 	   analogWrite(pwmA, i);
	  delay(10);}
}

Spending a minute reading the reference pages for Serial.available() and Serial.read() would benefit you greatly.