adafruit motor shield and stepper motors

I am using the adafruit motor shield with two stepper motors and sending serial messages to the arduino from Max/MSP

so far, both motors will run at the same time but I am still confused as to how it is functioning.
When I send a number like 50 or 10 it will spin both steppers one rotation, 200 steps.
these steppers are added as 200 Step motors, this should only give a quarter turn.

when I send a 100 or 200 both motors will spin one and a half turns.
this would be equal to 300 steps.

maybe you can help me understand why this is happening?

thanks,
-T

#include <AFMotor.h>


AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);

int val;                           // variable to store Serial data from MAX/MSP

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  motor1.setSpeed(100);     // set the speed to 100/255
motor2.setSpeed(100);
}



void loop() {
  
if (Serial.available() > 0){         // Check serial buffer for characters
   val = Serial.read();

for (val=0; val<100; val++) {
motor1.step(1, FORWARD, SINGLE); 
motor2.step(1, FORWARD, SINGLE);
}

}


}

I think I need to print out the Val. or So I am told. Can anyone clarify what this means and how to do it?

woah, noob question fo' sho'.

Please disregard that last post, Its embarrassing.

but at least I am learning, thats gotta count for something.

it is printing out the ascii values of Val. anyone know why?

You'll need to do something like:

Serial.print (val, DEC);

to get the value in decimal.

You'll need to do something like:

Code: Serial.print (val, DEC);

to get the value in decimal.

I tried this, I am still getting 49 48 48 printed. I think that it is printing these values because it thinks they are not ascii.

I am almost certain that it is moving the stepper motors 49 48 48 steps.

I tested this by sending a 22 ascii digits would convert this to 50 50 the stepper moved a half turn as expected.

This shows, or at least I think it does, that the serial.Print is not the issue. It is an ASCII value before the print. am i wrong?

Is this still in your code?

if (Serial.available() > 0){         // Check serial buffer for characters
   val = Serial.read();

for (val=0; val<100; val++) {
motor1.step(1, FORWARD, SINGLE);
motor2.step(1, FORWARD, SINGLE);
}

What do you expect it to do? (look carefully)

these steppers are added as 200 Step motors

What does this mean?

It it this line in your code:-

val = Serial.read();

That is reading the value in as an ASCII value. If you want this as a decimal value then you will have to convert it "by hand". val = val & 0x0f will turn a single ASCII number into a real number. If you need a two digit number then:-

if (Serial.available() > 1){ // Check serial buffer for characters val1 = Serial.read(); val2 = Serial.read(); val = ((val1 & 0x0f) * 10) + (val2 & 0x0f); }

Of course that assumes you always type in a two digit number and there is no error checking for non numeric inputs.

Mike, it's simpler than that ;)

The values that you get from Serial.read will be ASCII codes, yes. If you're sending a number like "100" (decimal), you'll get the digit ASCII codes, as you saw, 49 48 48. If you collect those digits into a string (character array, and add a terminating '\0'), you can use atoi() to convert the ASCII string to an integer.

Could you show us the whole sketch, as you have it now?

Is this still in your code?

Code:
if (Serial.available() > 0){ // Check serial buffer for characters
val = Serial.read();

for (val=0; val<100; val++) {
motor1.step(1, FORWARD, SINGLE);
motor2.step(1, FORWARD, SINGLE);
}

What do you expect it to do? (look carefully)

Quote:
these steppers are added as 200 Step motors

What does this mean?

This is my current code:

#include <AFMotor.h>


AF_Stepper motor1(200, 1);
AF_Stepper motor2(200, 2);

int val;                           // variable to store Serial data from MAX/MSP
int stp;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
  motor1.setSpeed(100);     // set the speed to 100/255
motor2.setSpeed(100);
}



void loop() {
  
if (Serial.available() > 0){         // Check serial buffer for characters
   val = Serial.read();

for (stp=0; stp<val; stp++) {
motor1.step(1, FORWARD, SINGLE); 
motor2.step(1, FORWARD, SINGLE);
}



      
Serial.print(val,DEC);
Serial.println(" ");

}


 }

I am using the adafruit motor shield, its library requires the stepper motors to be declared with the amount of steps required for one rotation.

OK, in that case, Mike's comments about ASCII and integers holds.

48 = 0x30 = '0' (ASCII zero) 49 = 0x31 = '1' (ASCII one)

is the clue.

if (Serial.available() > 1){ // Check serial buffer for characters val1 = Serial.read(); val2 = Serial.read(); val = ((val1 & 0x0f) * 10) + (val2 & 0x0f); }

Of course that assumes you always type in a two digit number and there is no error checking for non numeric inputs.

I will be putting in numbers from 1 to 500 it is important that It can adjust accordingly. What is the error if checking for non numeric inputs, what does this even mean?

Simpler to use “atoi”, otherwise, using the code above you’ll have to include leading zeroes (and as written, only works 0…99)

What is the error if checking for non numeric inputs, what does this even mean?

It means checking each character input:

if ((char >= ‘0’) && (char <= ‘9’))

If you don’t do this, and you input, say a letter O (ASCII 0x4F) instead of a zero, then your numeric conversion will fail or give the wrong result.

See:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1242113551

There's a nice example here which was linked to from the French thread you found: http://www.pobot.org/Interface-ligne-de-commande-pour.html (Google translated here but the code is not readable: http://translate.google.com/translate?u=http%3A//www.pobot.org/Interface-ligne-de-commande-pour.html&hl=en&langpair=fr|en&tbb=1&ie=ISO-8859-1 ).

In that example you would replace the lines that say Serial.print("> servo gauche : "); with a command to step the appropriate motor by "value" steps.

Andrew

Google translated here Why? Apart from the prompts, it's in English!

Google translated here Why? Apart from the prompts, it's in English!

I meant the article, but the code is more useful anyway.

Andrew

I will be putting in numbers from 1 to 500 it is important that It can adjust accordingly.

So in order to know when the number is complete and not just part way through you have to look for a carriage return character. Then you know when to stop gathering data.
Sort of like this:-

val = 0; temp = 0;
while(temp != 0x0d) {
// read inputs and accumulate the value
if (Serial.available() > 0){ // Check serial buffer for characters
temp = Serial.read();
if(( temp >= 0x30) & ( temp <= 0x39)) { val = (temp & 0x0f) + (val * 10) }
} }