void loop() { int modeType = 1; // This number increases by multiple of 2 each through the while loop.. // ..to identify our step mode type. while (modeType<=8){ // loops the following block of code 4 times before repeating . digitalWrite(DIR, LOW); // Set the direction change LOW to HIGH to go in opposite direction digitalWrite(MS1, MS1_MODE(modeType)); // Set state of MS1 based on the returned value from the MS1_MODE() switch statement. digitalWrite(MS2, MS2_MODE(modeType)); // Set state of MS2 based on the returned value from the MS2_MODE() switch statement. digitalWrite(SLEEP, HIGH); // Set the Sleep mode to AWAKE. int i = 0; // Set the counter variable. while(i<(modeType*200)) // Iterate for 200, then 400, then 800, then 1600 steps. // Then reset to 200 and start again. { digitalWrite(STEP, LOW); // This LOW to HIGH change is what creates the.. digitalWrite(STEP, HIGH); // .."Rising Edge" so the easydriver knows to when to step. delayMicroseconds(1600/modeType); // This delay time determines the speed of the stepper motor. // Delay shortens from 1600 to 800 to 400 to 200 then resets i++; } modeType = modeType * 2; // Multiply the current modeType value by 2 and make the result the new value for modeType. // This will make the modeType variable count 1,2,4,8 each time we pass though the while loop. delay(500); } digitalWrite(SLEEP, LOW); // switch off the power to stepper Serial.print("SLEEPING.."); delay(1000); Serial.print("z"); delay(1000); Serial.print("z"); delay(1000); Serial.print("z"); delay(1000); Serial.println(""); digitalWrite(SLEEP, HIGH); Serial.println("AWAKE!!!"); // Switch on the power to stepper delay(1000); }
void EasyStepper::stepWithEasing(int numOfSteps, int type) { switch(type) { // Sine wave case 0: if((unsigned long)(micros() - previous_timer) >= 500) { previous_timer = micros(); // Run motor if(stepsRemaining > 0) { // Is it time to microstep yet? if(stepsRemaining == numOfSteps/_easingDivider && microStepping == 0) { stepsRemaining *= 8; digitalWrite(mMs1, MS1_MODE(8)); digitalWrite(mMs2, MS2_MODE(8)); microStepping = 1; } else if(stepsRemaining > numOfSteps/_easingDivider) { if((timer+1) % _maxSpeed == 0) { digitalWrite(mStep, LOW); digitalWrite(mStep, HIGH); stepsRemaining--; } } else { if((timer+1) % (currentSpeed/8) == 0) { digitalWrite(mStep, LOW); digitalWrite(mStep, HIGH); stepsRemaining--; currentSpeed = map(stepsRemaining, numOfSteps/_easingDivider, 1, _maxSpeed, _minSpeed); } } } } break; } }
void EasyStepper::setMicrostepping(int fraction) { MS1_MODE(fraction); MS2_MODE(fraction); }