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 stepServo myservo1;
void setup(){
Serial.begin(9600);
Serial.println("Setup...");
pinMode(13, OUTPUT);
i2c_init(); //Initialise the i2c bus
PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups
myservo1.attach(9);
stepper.setSpeed(400);
}void loop(){
stepper.runSpeed();
int dev = 0x5A<<1;
int data_low = 0;
int data_high = 0;
int pec = 0;
int pos = 0;
i2c_start_wait(dev+I2C_WRITE);
i2c_write(0x07);// read
i2c_rep_start(dev+I2C_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();
i2c_stop();//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: ");
Serial.println(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 readingfor(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'
delay(1000);
}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