Programming Arduino and stepper

Hello all,

I found a program to make the stepper motor rotate a stepper 360 degrees in 24 hrs located on one of the forums.

//28BYJ-48 Stepper Motor
//Arduino Stepper Library runs in 4 step control signal sequence 11.25 degrees per step / 32 steps per one revolution of the internal motor shaft
//Gear reduction ratio: 1 to 63.68395
// Steps per one revolution (internal motor shaft) * Gear reduction ratio = Actual number of steps per revolution = 32 * 63.68395 = 2037.8864 in 4 step control signal sequence

//steps per degree = 2037.8864/360= 5.660795555555556
//rotation of earth = 0.4166666666666667 degrees/sec 
//steps motor needs to take per sec = 5.660795555555556 * 0.4166666666666667 = 2.358664814814815
//24 hours in day --> 60 sec/min * 60 min/hr * 24 hrs = 86400 seconds in a day
//Time earth takes to rotate 360 degrees/number of steps motor takes to rotate 360 degrees = 86400/2037.8864 = 42.3968676566 seconds/step


//stepper library
#include <Stepper.h>

//ms stands for motor steps
//#define is used to assign a name for a constant value i.e. in this case mspr stands for actual number of motor steps per revolution
#define mspr 2037.8864

//mip stands for motor input pin
//output pins 12,11,10,9 on Arduino UNO R3 attach to input pins 2,7,10,15 respectively on stepper motor driver L293D
int mip2 = 12;
int mip7 = 11;
int mip10 = 10;
int mip15 = 9;

//command tells the Arduino stepper library which pins are connected to motor controller
//first parameter in parenthesis is number of actual steps that occur per revolution
Stepper stepper(mspr, mip2, mip7, mip10, mip15);  

void setup()
{
  
//outputs Arduino UNO R3 pin outputs into stepper motor inputs
  pinMode(mip2, OUTPUT);
  pinMode(mip7, OUTPUT);
  pinMode(mip10, OUTPUT);
  pinMode(mip15, OUTPUT);

}

//repeats commands written below
void loop()
{ 
//number of steps motor takes and time delay between next step. 
//time delay and step simulate earth rotating 360 degrees in 24 hours which is simulated by the number of steps the motor takes to rotate 360 degrees in 24 hours. 
   stepper.step(1);
   delay(42396.8676566); // 360 degree in 24 hours

I want to modify this to only rotate 180 degrees then rest back to beginning and loop. I don't have much experience in C language for the Arduino. I'm thinking ill need a if then statement but not sure how to go about it. Any guidance would help.

Need to work on some smaller projects, first. Get some of the sample programs that come with the Arduino IDE to work for you. Then you will have a bit more experience.


delay(42396.8676566); // 360 degree in 24 hours

delay( ) uses integers :woozy_face:

delay() function only takes unsigned long type.

I will check them out

Then this would need to be rounded up to 42397?

Yes, for one day, then rounded down for the next day, if you want to make your clock self correcting,

ok ill make note of that

You can use microseconds() to get almost three more digits of precision. If you use 'delay' for timing you also have to account for the time taken by every other instruction in loop(). Better to use the BlinkWithoutDelay technique.

const unsigned long MicrosecondsPerStep = 42396868ul;

unsigned long previousMicros = 0;

void loop()
{
  unsigned long currentMicros = micros();

  if (currentMicros - previousMicros >= MicrosecondsPerStep)
  {
    previousMicros += MicrosecondsPerStep;
    stepper.step(1);
  }
}