Controlling Speed of Stepper Motor to execute Simple Harmonic Oscillation

I am working on a project which requires me to make a linear actuator which exceutes SHM. I am thinking of using a stepper motor and ball screw mechanism to do so. I am new to use of stepper motors and arduino in general. I wanted to know can we program arduino to vary speed of a stepper motor continuously so that it can perform SHM.

In simple terms you just need to vary the time interval between successive steps of the motor which is a fairly trivial exercise.

The complexity will be knowing what intervals are required at what time and that will depend on the overall mechanics of the system.

Have a look at this simple stepper code and stepper motor basics

...R

Something like this?

int freq ;  // DDS variables, frequency and phase
int phase = 0 ;

int i ;    // index into sine table

#define TABLE_SIZE 256
int sine_table [TABLE_SIZE] ;

void setup_sines ()  // call this once in setup
{
  for (int i = 0 ; i < TABLE_SIZE ; i++)
    sine_table [i] = (int) (sin (i * 2 * PI / TABLE_SIZE) * 32000) ;  // 15 bit sine samples
}

void step_handle ()
{
  freq = sine_table [i] ;    // get sine fast
  i = (i+1) % TABLE_SIZE ;

  int old_phase = phase ;  // do direct digital synthesis
  phase += freq ;

  if ((old_phase ^ phase) & 0x8000)  // detect sign change to trigger a step
  {
    digitalWrite (dir_pin, freq < 0) ;  // step and direction pin driving
    digitalWrite (step_pin, HIGH) ;
    delayMicroseconds (5) ;
    digitalWrite (step_pin, LOW) ;
  }
}

unsigned long ts = 0 ;
#define STEP_DELAY 500  // microseconds

void loop ()
{
  if (micros () - ts >= STEP_DELAY)  // run handle_stepper regularly (example is 2kHz)
  {
    ts += STEP_DELAY ;
    handle_stepper () ;
  }
  ... other stuff ...
}