Kinect to arduino - motor problem

Hey everyone

I started a project with a kinect and an arduino. My intention was to get the xvalue of a person in a room and transmit this value trougha serial port to the arduino. That works.

Now for the problem : When i receive that xvalue i want to use it to turn my stepper motor in 1 particular direction. The max turningpoints are between 0 and 180°. Basicly i want to adjust the angle based on where the person is standing. But when i run it it does it 1 time to the correct angle and the it does nothing …

Can anyone spot the problem ?

/* 
This is a test sketch for the Adafruit assembled Motor Shield for Arduino v2
It won't work with v1.x motor shields! Only for the v2's with built in PWM
control

For use with the Adafruit Motor Shield v2 
---->	http://www.adafruit.com/products/1438
*/


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);


int stepCounter = 0;
unsigned int integerValue=0;  // Max value is 65535

char readchar;
boolean doStep = true;
char incomingByte;


void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Stepper test!");

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  myMotor->setSpeed(10);  // 10 rpm   
}


 
    

void loop() 
{
  
  if(doStep == true)
  {
    checkSerial();
  }   
}

void checkSerial()
{
  if (Serial.available() > 0) 
  {   // something came across serial
     
    integerValue = 0;         // throw away previous integerValue
    while(1) 
    {            // force into a loop until 'n' is received
      incomingByte = Serial.read();
      if (incomingByte == '\n') break;   // exit the while(1), we're done receiving
      if (incomingByte == -1) continue;  // if no characters are in the buffer read() returns -1
      integerValue *= 10;  // shift left 1 decimal place
      // convert ASCII to integer, add, and shift left 1 decimal place
      integerValue = ((incomingByte - 48) + integerValue);
       
        
        if(integerValue > 0)
        {
          doStep = false;
          runMotor(integerValue);
          doStep = true;
        }
     }
    
    Serial.println(integerValue);   // Do something with the value
  }
}

void runMotor(int value)
{
          if(value  == 1)
          {
           motor(11);
          }
          else if(value == 2)
          {
           motor(21);
          }
           else if(value== 3)
          {
             motor(31);
          }
           else if(value == 4)
          {
             motor(41);
          }
           else if(value == 5)
          {
             motor(51);
          }
           else if(value == 6)
          {
             motor(61);
          }
           else if(value == 7)
          {
            motor(71);
          }
           else if(value == 8)
          {
             motor(81);
          }
           else if(value == 9)
          {
             motor(91);
          }
            else if(value == 10)
          {
             motor(100);
          }
}

void motor(int counter)
{
              if(stepCounter < counter)
                {
                   while(stepCounter < counter)
                    {
                    myMotor->step(1,FORWARD,SINGLE);
                      stepCounter = stepCounter + 1;
                        
                    }
                   
                }
                
              else if(stepCounter > counter)
                {
                   while(stepCounter > counter)
                     {
                        myMotor->step(1, BACKWARD,SINGLE);
                        stepCounter = stepCounter - 1;
                      
                     }
                
                }
}

Why are you moving the motor before the \n, that indicates the end of the packet, arrives?

If there is a charachter in the serial i put it in my integervalue and then i excecute my functions, or am I wrong ?

Then how should i do it ? if i put the IF statement outside of the while loop it doesn't do anything.

Thanks in advance

If there is a charachter in the serial i put it in my integervalue and then i excecute my functions, or am I wrong ?

Yes, you are wrong. You don't "put the value in integervalie". You add the value to integervalue.

What is happening if you enter "1234\n" is that you call the function with 1, 12, 123, and 1234. Which makes no sense to me.

if i put the IF statement outside of the while loop it doesn't do anything.

That would depend on WHERE outside the while loop you put it.

hey

I changed my code in Visual studio so it only transmits single digit numbers. But now when i try to run it nothing happens anymore … Can you see the problem ?

#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Connect a stepper motor with 200 steps per revolution (1.8 degree)
// to motor port #2 (M3 and M4)
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 2);


int stepCounter = 0;
unsigned int integerValue=0;  // Max value is 65535

char readchar;
boolean doStep = true;
char incomingByte;

//*****************************************************************************
void setup() {
  Serial.begin(9600);           
  // set up Serial library at 9600 bps


  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  myMotor->setSpeed(10);  // 10 rpm   
}
//*****************************************************************************
void loop() 
{
  
  if(doStep == true)
  {
    checkSerial();
  }   
}
//*****************************************************************************
void checkSerial()
{
  if (Serial.available() > 0) 
  {   
    // something came across serial

      integerValue = 0;     
    // throw away previous integerValue
               
      integerValue = Serial.read();

  }
  if(integerValue > 0)
        {
          doStep = false;
          runMotor(integerValue);
          doStep = true;
        }
        else if(integerValue == 0)
        {
         runMotor(0);
        }
}
//*****************************************************************************
void runMotor(int value)
{      
          
          if(value == 0)
          {
            motor(0);
          }
          else if(value  == 1)
          {
           motor(11);
          }
          else if(value == 2)
          {
           motor(22);
          }
           else if(value== 3)
          {
             motor(33);
          }
           else if(value == 4)
          {
             motor(44);
          }
           else if(value == 5)
          {
             motor(55);
          }
           else if(value == 6)
          {
             motor(66);
          }
           else if(value == 7)
          {
            motor(77);
          }
           else if(value == 8)
          {
             motor(88);
          }
           else if(value == 9)
          {
             motor(100);
          }

}
//*****************************************************************************
void motor(int counter)
{
              if(stepCounter < counter)
                {
                   while(stepCounter < counter)
                    {
                    myMotor->step(1,FORWARD,SINGLE);
                      stepCounter = stepCounter + 1;
                    }   
                }
                
              else if(stepCounter > counter)
                {
                   while(stepCounter > counter)
                     {
                        myMotor->step(1, BACKWARD,SINGLE);
                        stepCounter = stepCounter - 1;
                      
                     }
                
                }
}

Nevermind I got it, I forgot to convert the number to an ascii format.

Funny fact PaulS, a reply from you on another post helped me out. So you helped me solve it indirectly !!!

thanks man !