don't overflow serial buffer
[Arduino] / StepperSerialTest / StepperSerialTest.ino
1 // Adafruit Motor shield library
2 // copyright Adafruit Industries LLC, 2009
3 // this code is public domain, enjoy!
4
5 #include <AFMotor.h>
6
7 // Connect a stepper motor with 48 steps per revolution (7.5 degree)
8 // to motor port #2 (M3 and M4)
9 AF_Stepper motor(48, 2);
10
11 void setup() {
12   Serial.begin(9600);           // set up Serial library at 9600 bps
13   Serial.println("Stepper test!");
14
15   motor.setSpeed(10);  // 10 rpm   
16 }
17
18 void loop() {
19 //  Serial.println("Single coil steps");
20 //  motor.step(100, FORWARD, SINGLE); 
21 //  motor.step(100, BACKWARD, SINGLE); 
22
23   if (Serial.available()) {
24     int in = Serial.read();
25     Serial.println(in, DEC);
26     if ( in == 102 ) { // f
27       Serial.println("Double coil steps forward");
28       motor.step(48, FORWARD, DOUBLE);
29     } else if ( in == 98 ) { // b
30       Serial.println("Double coil steps backward");
31       motor.step(48, BACKWARD, DOUBLE);
32     } else if ( in >= 49 && in <= 57 ) {
33       int speed = ( in - 48 ) * 10;
34       Serial.print("speed ");
35       Serial.print(speed, DEC);
36       Serial.println(" rpm");
37       motor.setSpeed( speed );
38     }
39   }
40
41 //  Serial.println("Interleave coil steps");
42 //  motor.step(100, FORWARD, INTERLEAVE); 
43 //  motor.step(100, BACKWARD, INTERLEAVE); 
44
45 //  Serial.println("Micrsostep steps");
46 //  motor.step(100, FORWARD, MICROSTEP); 
47 //  motor.step(100, BACKWARD, MICROSTEP); 
48 }