I am trying to get two steppers to move at the same time and using the modulus function to activate if statements, so they will start and finish at the same time for two steppers moving different amounts.
unfortunately the code seems to run exceptionally slow, will this be due to the % function? any suggestions on how to speed this up?
int dir1= 6;
int dir2=6;
int step1=7;
int step2=8;
void setup() {
pinMode(dir1, OUTPUT);
pinMode(dir2, OUTPUT);
pinMode(step1, OUTPUT);
pinMode(step2, OUTPUT);
Serial.begin(250000);
}
void loop() {
unsigned int stepin1 = 3200;
unsigned int stepin2 = 6400;
unsigned long go = 0;
unsigned long multiStep=stepin1*stepin2;
Serial.println (multiStep);
for (int i=0; i<=multiStep; i++){
go += 1 ;
Serial.println(go);
if (i % stepin1 == 0){
digitalWrite(step1, HIGH);
delayMicroseconds(500);
digitalWrite(step1, LOW);
delayMicroseconds(500);
Serial.println("went")
}
if (i % stepin2 == 0){
step2=1;
delayMicroseconds(500);
step2=2;
delayMicroseconds(500);
Serial.println("well");
}
}
}
Some parts of your code does not make any sense so I must assume that you have posted partial code which makes it hard to help. Since "stepin*" never changes, it makes no sense to use the modulus operator on them. You are assigning values which are never used to "step2". "dir*" are unused values. Serial communication is not fast, so all calls to "Serial.print*()" may slow down the code.
The modulus operator is indeed slow. However if you use millis() rather than delay() there will probably be plenty of time to accommodate the modulus operator.
This is indeed proof of concept to see if the code works.
The idea is that the motors will start and finish at the same time. To stick with integer maths I am multiplying the number of steps for both motors and then activating the if statement only when 'i' is equal to a multiple of the number of steps required for the motor.
In this way the first motor will only activate every 3200 iterations and the second every 6400 iterations.
I haven't used dir as I was trying to trouble shoot this part of the code, so I took that part out.
I am seeing the code proceeding every 1 second or so.
I will have a look at the milis() function, but I do need a break between the HIGH and LOW to activate a step.
Paultn12:
The idea is that the motors will start and finish at the same time. To stick with integer maths I am multiplying the number of steps for both motors and then activating the if statement only when 'i' is equal to a multiple of the number of steps required for the motor.
In this way the first motor will only activate every 3200 iterations and the second every 6400 iterations.
Modulus isn't that slow and probably isn't your problem.
However, one way to get extra speed is just to set a variable to 3200 and let it count down. Decrementing a variable by 1 is fast. Checking if a variable is zero is also fast. When it hits zero, do what you have to do and re-set it to 3200.
I'd suspect that the problem might be your baud rate of 250000 . It's a weird value that I have never seen before. Pick something from the drop-down list in the serial monitor.
PaulMurrayCbr:
I'd suspect that the problem might be your baud rate of 250000 . It's a weird value that I have never seen before. Pick something from the drop-down list in the serial monitor.
According to the Atmega 328 datasheet 250,000 baud will work perfectly - but I have not tried it myself as I usually use 500,000 baud with my Python programs and the minicom terminal program. I have no idea if it works with the Arduino serial monitor program.