Hello to all out there.
When it's done, it will be a simple cleaning robot.
But until then, I need a little help.
Components so far:
1x Arduino Leonardo
2x 28BYJ-48 5V stepper motors (datasheet)
2x 2 ULN2003A stepper motor driver
1x HC-SR04 ultrasonic (datasheet)
5V power supply
and some Stuff to keep it all together.
I am tryng to merge NewPing and CustomStepper examples together,
but get an odd behaviour. If the objekt is near to the sonsor around 10cm,
the robot moves as it is sopposed to.
If the distance is greater the motors don't run evenly.
Any Ideas how to fix this? Thank you.
//join ultrasonic HC-SR04 and 2 steppermotors driven by 2 ULN2003A.
// ---------- included libraries ------------
#include <CustomStepper.h>
#include <NewPing.h>
// ---------- hardware pin defines -----------
int triggerPin = 3; // select the pin for ultrasonic trigger
int echoPin = 5; // select the pin for echo
// ---------- variable initialization -----------
int delayTime = 0; //variable that holds the delay time in milliseconds
int scaling = 1;
unsigned int uS = 0; // holds the time it took for the pulse to be recived
unsigned int distance = 0; // holds the distance in centimeters
int maxValue = 200; // in centimeter
int minValue = 0; // in centimeter
int maxDistance = 200; // in centimeters
int turnrate = 270; // turnrate of robot
int turndistance = 20; // distance when the robor shuld turn in cm
// ---------- library initialization -----------
//Full constructor, just the first 4 parameters are necessary, they are the pins connected to the motor,
//the others are optional, and default to the following below
//the 5th paramater is the steps sequence, where the 1st element of the array is the number of steps
//it can have a maximum of 8 steps
//the 6th parameter is the SPR (Steps Per Rotation)
//the 7th parameter is the RPM
//the 8th parameter is the rotation orientation
CustomStepper stepper1(13, 12, 11, 10, (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);
CustomStepper stepper2(A2, A3, A4, A5, (byte[]){8, B1000, B1100, B0100, B0110, B0010, B0011, B0001, B1001}, 4075.7728395, 12, CW);
NewPing sonar(triggerPin, echoPin, maxDistance);
void setup() {
Serial.begin(9600);
//motor
//sets the RPM
stepper1.setRPM(12);
stepper2.setRPM(12);
//sets the Steps Per Rotation, in is case it is 64 * the 283712/4455 annoying ger ratio
//for my motor (it works with float to be able to deal with these non-integer gear ratios)
stepper1.setSPR(4075.7728395);
stepper2.setSPR(4075.7728395);
}
void loop() {
// Input
uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
distance = uS / US_ROUNDTRIP_CM; // convert time to distance
// Debugging
Serial.print("uS value: "); Serial.println(uS);
Serial.print("Distance (cm): "); Serial.println(distance);
// Processing
//Scaling
delayTime = map (distance, minValue, maxValue, 200, 1023);
Serial.print ("Delay in milliseconds: "); Serial.println (delayTime);
// Modes
// None - put new modes here
//Motor
//when a command is finished it the isDone will return true, it is important to notice that
//you can't give one command just after another since they don't block the execution,
//which allows the program to control multiple motors
moveforward();
}
//Motor functions
void turnleft()
{
stepper1.setDirection(CCW);
stepper2.setDirection(CW);
stepper1.rotateDegrees(turnrate);
stepper2.rotateDegrees(turnrate);
}
void turnright()
{
stepper1.setDirection(CW);
stepper2.setDirection(CCW);
stepper1.rotateDegrees(turnrate);
stepper2.rotateDegrees(turnrate);
}
void moveforward()
{
stepper1.setDirection(CCW);
stepper2.setDirection(CCW);
stepper1.rotate();
stepper2.rotate();
}