stepper motor toroid winder w/turns count

Hello all, I am working on building a toroid winder which I am using a arduino and a 28byj-48.
It takes 4 full revolutions of the stepper motor to equal one turn on the winder itself. I wanted to be able to send the amounts of turns to the arduino via serial connection since I do not have a arduino display yet. What I have currently works however if I want say 200 turns (1,638,400 steps) it stops at only 1 or 2 turns. Any clues as to what I am doing wrong here?

// Arduino stepper motor control code

#include <Stepper.h> // Include the header file

// change this to the number of steps on your motor
#define STEPS 32
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64 //2048

// create an instance of the stepper class using the steps and pins
Stepper stepper(STEPS, 8, 10, 9, 11);

int val = 0;

void setup() {
Serial.begin(9600);
stepper.setSpeed(1000);
}

void loop() {

if (Serial.available()>0)
{
val = Serial.parseInt();
stepper.step(val);
Serial.println(val); //for debugging
}

}

Which Arduino board are you using?

I have several of those steppers. I would recommend using a different method to measure each full turn. The ones I have seem to skip steps rather frequently with tension applied. I can rotate ten full turns one way. Then reverse direction 10 full turns. Sometimes the motor would be off by 1/4 turn or more. It could be partly due to the cheap driver boards that came with mine though.

pert: Which Arduino board are you using?

Arduino UNO

On the Uno, the maximum value a variable of the type int can store is 32767: https://www.arduino.cc/reference/en/language/variables/data-types/int/

1,638,400 steps Probably want to use unsigned long for that high of a count.

if (Serial.available()>0)

Serial coming in at 9600 comes in pretty slow.

More than likely the sketch is responding to the first byte that comes in, that's why it stops so quick.

Change the part about reading to wait for more characters before acting on a number.

pert: On the Uno, the maximum value a variable of the type int can store is 32767: https://www.arduino.cc/reference/en/language/variables/data-types/int/

In that case I guess I would have to account for the gear ratio, thus 8,192 steps to equal 1 turn.

I guess what I could do is set it to make 8,192 steps and use the loop function to account for the turns. Any thoughts on how I could implement this?

CrossRoads:
More than likely the sketch is responding to the first byte that comes in, that’s why it stops so quick.

Serial.parseInt() is a blocking function with a default timeout duration of 1000 ms. That’s plenty enough time for the full number to come through.

bond007: Any thoughts on how I could implement this?

Despite the name, the return type of Serial.parseInt() is long, which can hold a maximum value of 2147483647. So as far as getting the number of steps from Serial Monitor to your Arduino board, all you need to do is change the type of the val variable to long.

However, there is also the issue that the type of stepper.step()'s parameter is int. So you can't pass a value of greater than 32767 to that function. You'll need to modify your code so that it calls stepper.step() multiple times to trigger more steps than 32767.

I fixed it using the for command and replacing the value of <n as <val. This allows me to set the number of loops (turns) via serial.

// Arduino stepper motor control code

#include <Stepper.h> // Include the header file

// change this to the number of steps on your motor
#define STEPS 32
#define STEPS_PER_OUTPUT_REVOLUTION 32 * 64 //2048

// create an instance of the stepper class using the steps and pins
Stepper stepper(STEPS, 8, 10, 9, 11);

int val = 0;
int i;
void setup() {
Serial.begin(9600);
stepper.setSpeed(1000);
}

void loop(){

if (Serial.available()>0)
{
val = Serial.parseInt();
for(i=0; i < val; i++)
stepper.step(8192);
Serial.println(val); //for debugging
}

}