Getting steppermotor to work with my present program.

Hello, I am using an Arduino Uno with an Adafruit V1 motor shield. I added the shield to use the stepper motor. My program uses a temperature sensor and a servo to do a task, this part works very well. I also used a separate motor to do another task, but needed a much slower rotation to do it right, therefore the stepper motor. My problem is integrating the stepper programming into my existing sensor/servo programming. When I combined the stepper program with the sensor/servo program, the stepper will not run. Any help would be appreciated. Here is my code:

#include <i2cmaster.h>
#include <Servo.h>
#include <AccelStepper.h>
#include <AFMotor.h>

AF_Stepper motor2(200, 2);
// you can change these to DOUBLE or INTERLEAVE or MICROSTEP!
void forwardstep() {
motor2.onestep(BACKWARD, DOUBLE);
void backwardstep() {
motor2.onestep(FORWARD, DOUBLE);
AccelStepper stepper(forwardstep, backwardstep); // use functions to step

Servo myservo1;

void setup(){
pinMode(13, OUTPUT);
i2c_init(); //Initialise the i2c bus
PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups

void loop(){
int dev = 0x5A<<1;
int data_low = 0;
int data_high = 0;
int pec = 0;
int pos = 0;

// read
data_low = i2c_readAck(); //Read 1 byte and then send ack
data_high = i2c_readAck(); //Read 1 byte and then send ack
pec = i2c_readNak();

//This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps
double tempFactor = 0.02; // 0.02 degrees per LSB (measurement resolution of the MLX90614)
double tempData = 0x0000; // zero out the data
int frac; // data past the decimal point

// This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
tempData = (double)(((data_high & 0x007F) << 8) + data_low);
tempData = (tempData * tempFactor)-0.01;

float celcius = tempData - 273.15;
float fahrenheit = (celcius*1.8) + 32;

Serial.print("Fahrenheit: ");

delay(250); // wait a half second before printing again

//on start up, the servo position is at 0, then servo goes to 90 position loading a case
//in the annealing position against the motor wheel.

if ( fahrenheit > 120.00 ) //when temperature reads greater than 110 degrees

{ digitalWrite(13, HIGH); //set the LED on
delay(250); //wait 1/2s for next temp reading

for(pos = 0; pos<=90; pos += 1) // goes from 0 degrees to 90 degrees
// in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable ‘pos’

else (fahrenheit < 120,00); //when temperature reads less than 110 degrees

{digitalWrite(13, LOW); } // set the LED off
for(pos=90; pos>=0; pos-=1) // goes from 90 degrees to 0 degrees
//in steps of 1 degree
myservo1.write(pos); // tell servo to go to position in variable ‘pos’
delay(1000); // waits 2s for the servo to reach the position
} //this delay is to allow the time for the next
//case drop into position

Posting your code in quote tags rather than code tags has screwed it up, turned much of it into italics and put a couple of smileys in it along the way

Please repost it in code tags in order to make it readable.

The IDE quote for forum thing is no good. Above the forum edit window and row of smileys is the # button that makes code tags. Past your code inside of those, it will be readable.

Also Arduino float and double are the same thing, 32-bit floating point good to 6 places. You need better, look into fixed-point integer with 32-bit of 64-bit integers.

Have you figured out how to get the stepper motor to run they way you want in a short sketch that has nothing else in it?


AF_Stepper motor2(200, 2);

What do these arguments mean? How do you have the stepper connected to the shield?

@PaulS First arg is #step/rev, 2nd arg is motor one or two. (the pin configution is hardwired as it is a shield)

@66harley, original poster: I notice you have two stepper libraries - you use the AccelStepper library to implement the acceleration profile which uses the AF library to do the step. "Interesting".

The AccelLibrary works if you call runSpeed in the loop() - which you do - but it must be called "as frequently as possible" (quote from the AccelStepper .H file). In your loop() you have a delay(1000) which means the runSpeed() is only called once a second. Hence the fastest your stepper can move is one step per second.

Do not use delay(). Wrap the code that must only run once a second with a timer routine, like the example "blink without delay" and/or read Avoid delay and/or read Blink two leds without delay. Did I give the impression that using delay() is bad? Yes? Good.

@PaulS First arg is #step/rev, 2nd arg is motor one or two. (the pin configution is hardwired as it is a shield)

I knew that. The question was designed to make OP think about which pins the stepper should be connected to, and have him/her tell us about it.