Hey
I have some trouble with the accelstepper library.
I am using two BYJ48 Stepper Motors(4 wires), they are controlled by a ULN2003 driver and they are both powered with an alternative power source. I am trying to make the steppers move one step at a time, but i can't figure out how. This is the code i am working on:
#include <AccelStepper.h>
#define HALFSTEP 8
#include <Stepper.h>
// motor pins
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
#define motorPin5 3
#define motorPin6 4
#define motorPin7 5
#define motorPin8 6
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
AccelStepper stepper2(HALFSTEP, motorPin5, motorPin7, motorPin6, motorPin8);
int steps1 = 0;
int steps2 = 0;
int trigpin = 51;
int echopin = 53; //Pin geluidssensor en klok
//Define the amount of steps per centimeter moved
#define spc 180
boolean objects[100][100];
//1 = 1mm on the map
int position[] = {0, 0};
//Ultasonic 1
float pingtime1;
float distanceFront;
int trigpin1 = 52;
int echopin1 = 53;
//Ultasonic 2
float pingtime2;
float distanceRight;
int trigpin2 = 50;
int echopin2 = 51;
float SoundSpeed = 34300; // in m/s
void setup() {
Serial.begin(9600);
pinMode(trigpin1, OUTPUT);
pinMode(echopin1, INPUT);
pinMode(trigpin2, OUTPUT);
pinMode(echopin2, INPUT);
stepper1.setMaxSpeed(1500);
stepper1.setSpeed(500);
stepper2.setMaxSpeed(1500);
stepper2.setSpeed(-500);
}//end setup
void loop(){
distanceF(); //measure the distance to object
distanceR();
moveCar(distanceR, distanceF); //move te car
}
void moveCar(int xAmount, int yAmount) {
int yDistance = yAmount * spc; //distance = distancefront / SPC
for (int i = 0; i < yDistance; i++) { // i = 0 and counts till the stepper has moved the required steps
stepper1.step4(long -1); //so for example measures 500 cm, calculates the required stepps and moves that
stepper2.step4(long 1);
position[1]++; // to keep track of the position
if (xAmount > 10) { // after y distance we need to move in the x direction,
int xDistance = xAmount * spc; // measure if there is space to move to the right, calculate the required amount of steps.
// if the x amount is smaller than 10 it will not move to the right
//turn 90 degrees
for (int i =0; i< 180; i++) { //theoretical amount of steps to move to turn 90 degrees
stepper1.step4(long 1);
stepper2.step4(long 1);
}
for (int i = 0; i < yDistance; i++) { //the vehicle has turned 90 degrees, so we have to use the Ultra Sound that is mounted at the front
stepper1.step4(long -1);
stepper2.step4(long 1);
position[0]++;
}
}
void distanceF(){
digitalWrite(trigpin1, LOW);
delay(50);
digitalWrite(trigpin1, HIGH);
delay(50);
digitalWrite(trigpin1, LOW);
pingtime1 = pulseIn(echopin1, HIGH);
pingtime1 = pingtime1 / 1000000;
distanceFront = (SoundSpeed * pingtime) / 2;
}
void distanceR(){
digitalWrite(trigpin2, LOW);
delay(50);
digitalWrite(trigpin2, HIGH);
delay(50);
digitalWrite(trigpin2, LOW);
pingtime2 = pulseIn(echopin2, HIGH);
pingtime2 = pingtime2 / 1000000;
distanceRight = (SoundSpeed* pingtime) / 2;
}
Its measuring the distance in two directions. The front and the right. The goal is to move the steppers one step at a time to make a map.
my source for the functions is: AccelStepper: AccelStepper.h Source File
If someone knows whats wrong with my code? I would appreciate all the help.
greats mike