I left MS2 floating, reads zero volts. This works OK:
const int dirPinX = 13;
const int stepPinX = 12;
const int dirPinY = 8;
const int stepPinY = 7;
void setup()
{
pinMode(stepPinX, OUTPUT);
pinMode(dirPinX, OUTPUT);
pinMode(stepPinY, OUTPUT);
pinMode(dirPinY, OUTPUT);
}
void loop()
{
stepX();
stepY();
}
void stepX()
{
digitalWrite(stepPinX, HIGH);
delayMicroseconds(5);
digitalWrite(stepPinX, LOW);
delayMicroseconds(50);
}
void stepY()
{
digitalWrite(stepPinY, HIGH);
delayMicroseconds(5);
digitalWrite(stepPinY, LOW);
delayMicroseconds(50);
}
Here is the program that runs too slow:
/*
Uno
*/
const int dirPinX = 13;
const int stepPinX = 12;
const int dirPinY = 8;
const int stepPinY = 7;
int resetXcount = 0, resetYcount = 0, stepXcount = 0, stepYcount = 0;
//float deltaTheta = PI/8000; // Original
float deltaTheta = PI / 8000; // Adjust this when gear ratio changes
float A = 200; // Amplitude - scaling (stretching/shifting)
float deltaX = 0, deltaY = 0, theta = 0;
float x_1 = 0, x_2 = 0, y_1 = 0, y_2 = 0;
//----------------------------ONCE----------------------------
void setup()
{
pinMode(stepPinX, OUTPUT);
pinMode(dirPinX, OUTPUT);
pinMode(stepPinY, OUTPUT);
pinMode(dirPinY, OUTPUT);
deltaX = 0; //(dirPinX, LOW); // left
deltaY = 0; //(dirPinY, LOW); // towards
for (int i = 0; i < 4200; i++)
{
stepX();
stepY();
digitalWrite(stepPinX, LOW);
digitalWrite(stepPinY, LOW);
delayMicroseconds(50);
}
}
//----------------------------LOOP----------------------------
void loop()
{
// 1: Circle
x_1 = cos(theta) * A * 100;
x_2 = cos(theta + deltaTheta) * A * 100;
y_1 = 0.9 * sin(theta) * A * 100; // Original
y_2 = 0.9 * sin((theta + deltaTheta)) * A * 100; //
drawLocus();
theta += deltaTheta;
}
void drawLocus()
{
deltaX += (x_2 - x_1) * 100;
if (abs(deltaX) > 100) // && abs(cos(theta)) < 0.9
{
stepX();
if (deltaX < 0)
{
deltaX += 100;
stepXcount++;
}
else
{
deltaX -= 100;
stepXcount--;
}
}
deltaY += (y_2 - y_1) * 100;
if (abs(deltaY) > 100) // && abs(sin(theta)) < 0.95
{
stepY();
if (deltaY < 0)
{
deltaY += 100;
stepYcount++;
}
else
{
deltaY -= 100;
stepYcount--;
}
}
digitalWrite(stepPinX, LOW);
digitalWrite(stepPinY, LOW);
delayMicroseconds(50);
}
void stepX()
{
if (deltaX < 0) // Set motor direction clockwise
digitalWrite(dirPinX, HIGH);
else
digitalWrite(dirPinX, LOW);
digitalWrite(stepPinX, HIGH);
delayMicroseconds(5);
}
void stepY()
{
if (deltaY < 0) // Set motor direction clockwise
digitalWrite(dirPinY, HIGH);
else
digitalWrite(dirPinY, LOW);
digitalWrite(stepPinY, HIGH);
delayMicroseconds(5);
}
Any suggestions?