Hi
Below is an example of the code that I am testing. Any suggestions to improve the code would be welcome.
I am using microstepping 1/4.
Insted of the user define the "MillisBetweenSteps" and/or "MicrosBetweenSteps" Can I use revolutions per second and than convert that to MillisBetweenSteps? If so what would be the better way to do that?
How should I implement the End limit switch at the beginning when the equipment is turned on?
As as previously asked how can I improve speed?
const byte numChars = 32;
char receivedChars[numChars]="<P00206000U00701580022023>"; // just for test purposes
// P002 = PinStep = 2;
// 06000 = NumbOfSteps = 6000
// Direction = Dir = (U=HIGH or D=LOW);
// Direction Pin= PinDir = 007;
// 0158 = MillisBetweenSteps= 158;
// EndLimitRight = 022;
// EndLimitLeft= 023;
int MicrosBetweenSteps = 1; // microseconds (just for test probably will be added to receivedChars variabel)
boolean newData = true;
void setup() {
// Steps
pinMode(2, OUTPUT); // Step of Nema1
pinMode(4, OUTPUT); // Step of Nema2
pinMode(6, OUTPUT); // Step of Nema3
pinMode(8, OUTPUT); // Step of Nema4
pinMode(10, OUTPUT); // Step of Nema5
// Direction
pinMode(3, OUTPUT); // Direction of Nema1
pinMode(5, OUTPUT); // Direction of Nema2
pinMode(7, OUTPUT); // Direction of Nema3
pinMode(9, OUTPUT); // Direction of Nema4
pinMode(11, OUTPUT); // Direction of Nema5
// EndLimitRight and EndLimitLeft
pinMode(22, INPUT); // Pin Up of the end switch
pinMode(23, INPUT); // Pin Down of the end switch
Serial.begin(115200);
Serial.println("<Arduino is ready>");
}
void loop() {
RecvData();
if (newData == true)
{
parseData();
newData = false;
}
}
void RecvData()
{
// function recvWithStartEndMarkers by Robin2 of the Arduino forums
// See http://forum.arduino.cc/index.php?topic=288234.0
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;
// original code like this:
// while (Serial.available() > 0 && newData == false) {
// rc = Serial.read();
// only for demostration purposes
//----------------------------------
for (int n = 0; n < sizeof(receivedChars); n++)
{
rc = receivedChars[n];
//----------------------------------
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
void parseData()
{
if (receivedChars[0] == 'P' ) // control the motors
{
byte PinStep = convertToNumber(1, 3); // get the step motor Pin
int NumbOfSteps = convertToNumber(4, 5); // get the number of steps
int Dir;
if ( receivedChars[9] == 'U') {
Dir = HIGH; // direction
}
if ( receivedChars[9] == 'D') {
Dir = LOW; // direction
}
byte PinDir = convertToNumber(10, 3); // get the direcion motor Pin
int MillisBetweenSteps = convertToNumber(13, 4); // MillisBetweenSteps
byte EndLimitRight = convertToNumber(17, 3); // get the end limit Top Pin
byte EndLimitLeft = convertToNumber(20, 3); // get the end limit Bottom Pin
RunMotor (PinDir, Dir, NumbOfSteps, PinStep, MillisBetweenSteps, EndLimitRight, EndLimitLeft);
Message(PinStep, PinDir, Dir, NumbOfSteps, MillisBetweenSteps, EndLimitRight, EndLimitLeft);
}
}
void RunMotor (byte PinDir, int Dir, int NumbOfSteps, byte PinStep, int MillisBetweenSteps, byte EndLimitRight, byte EndLimitLeft)
{
digitalWrite(PinDir, Dir); // define the direction of rotation (up=anti-clockwise or down=clockwise )
for (int n = 0; n < NumbOfSteps; n++)
{
if (digitalRead(EndLimitRight) == 0 & digitalRead(EndLimitLeft) == 0) // if EndLimits not active
{
digitalWrite(PinStep, HIGH);
delayMicroseconds(MicrosBetweenSteps); // this line is probably unnecessary
digitalWrite(PinStep, LOW);
delayMicroseconds(MillisBetweenSteps);
}
else
{
break;
}
}
}
void Message(byte PinStep, byte PinDir, int Dir, int NumbOfSteps, int MillisBetweenSteps, byte EndLimitRight, byte EndLimitLeft)
{
String Str;
Serial.print("PinStep = "); Serial.println(PinStep);
Serial.print("PinDir = "); Serial.println(PinDir);
if (Dir == 1) Str = "UP"; else Str = "Down";
Serial.print("Direction = "); Serial.println(Str);
Serial.print("NumbOfSteps = "); Serial.println(NumbOfSteps);
Serial.print("PinEndLimitRight = "); Serial.println(EndLimitRight);
Serial.print("PinEndLimitLeft = "); Serial.println(EndLimitLeft);
Serial.println("------------------------------------");
}
// this should be possible to do in a much better way...
int convertToNumber( byte startPos, byte Numb)
{
unsigned int tmp = 0;
if (Numb == 5 ) // if 5
{
tmp = (receivedChars[startPos] - 48) * 10000;
tmp = tmp + (receivedChars[startPos + 1] - 48) * 1000;
tmp = tmp + (receivedChars[startPos + 2] - 48) * 100;
tmp = tmp + (receivedChars[startPos + 3] - 48) * 10;
tmp = tmp + receivedChars[startPos + 4] - 48;
}
else if (Numb == 4) // if 4
{
tmp = (receivedChars[startPos] - 48) * 1000;
tmp = tmp + (receivedChars[startPos + 1] - 48) * 100;
tmp = tmp + (receivedChars[startPos + 2] - 48) * 10;
tmp = tmp + receivedChars[startPos + 3] - 48;
}
// if 3 digits
else if (Numb == 3) // if 3
{
tmp = (receivedChars[startPos] - 48) * 100;
tmp = tmp + (receivedChars[startPos + 1] - 48) * 10;
tmp = tmp + receivedChars[startPos + 2] - 48;
}
return tmp;
}
Than you in advance for any help.