Driving a stepper motor with ISR. Issues explained in comment.

*NOTE CODE TWO LARGE, SEE ADDITIONAL POST
Hey all, I’ve taken over a project that was already written by someone else. A lot of it is commented out by him (anything I comment will be CAPATALIZED). The point of the project is to run these stepper motors using pwm… We’re using LCD shield. The problem I have is that I would like to have 4 different RPM possibilities using the ANALOG(0). You’ll see he has a timer interrupt before his main running code and I’ve tried to implement an IF statement before and after to test out if it’s possible to use the “if (analogRead(0) < 50)”, which corresponds to the “RIGHT” button and I get a mess.

#include <LiquidCrystal.h>

/*
 * Hardware specs
 * 
 * stepper motors, 200 steps per revolution
 * will always run at 1/8 step selection on controllers, gives 1600 steps per revolution,  hard wire these
 * maximum input signal frequency is 500,000 Hz 
 * 
 * 
 * Desired functionality
 * 
 * direction control (CW/CCW) for each bank of (3) steppers
 * speed control for each bank of (3) steppers
 * rotation count for each bank of (3) steppers
 * Stop, go, pause, reset counters
 * all of the above functionality but for (ALL) 12 motors on single box at the same time.
 * minimum 0.1 rev/min (between 0 and 20 rpm.. above 20 rpm, increment 1 rpm)  maximum 90 rev/min
 * 
 */



// Variable definitions *********

char bank1_dir_pin[3] = "A1";
char bank2_dir_pin[3] = "A2";
char bank3_dir_pin[3] = "A3";
char bank4_dir_pin[3] = "A4";
int bank1_step_pin = 2;
int bank2_step_pin = 3;
int bank3_step_pin = 11;
int bank4_step_pin = 12;



float desired_rpm_bank1 = 2.5; // 8 rpm is max speed
float desired_rpm_bank2 = 5;
float desired_rpm_bank3 = 1;
float desired_rpm_bank4 = 0.5;



int rpm_bank1 = (int) desired_rpm_bank1;
int rpm_bank2 = (int) desired_rpm_bank2; //J ADDED
int rpm_bank3 = (int) desired_rpm_bank3; //J ADDED
int rpm_bank4 = (int) desired_rpm_bank4; //J ADDED


float mot_steps_per_rev = 200; // steps/revolution  (CHECK THIS ON MOTOR DATASHEET)
float microstep_scaler = 8; // half step  (MUST MAKE MS1 INACTIVE (low/open) AND MS2 ACTIVE (high/5v))  (both active high in relaity, no step scaling.
float steps_per_rev = mot_steps_per_rev * microstep_scaler;
float sec_per_min = 60;
float clk_pulses_per_step = 2;

float clock_freq = (desired_rpm_bank1*clk_pulses_per_step*steps_per_rev/sec_per_min)*100; // Hz      max pwm frequency  = clock frequency/2 
int prescaler = 1;
int match_register = (16000000/(prescaler*clock_freq))-1;


float clock_counter_bank1 = 0;
float clock_counter_bank2 = 0;
float clock_counter_bank3 = 0;
float clock_counter_bank4 = 0;

float step_counter_bank1 = 0;
float step_counter_bank2 = 0;
float step_counter_bank3 = 0;
float step_counter_bank4 = 0;

float prev_clock_counter_bank1 = 0;
float prev_clock_counter_bank2 = 0;
float prev_clock_counter_bank3 = 0;
float prev_clock_counter_bank4 = 0;

int revolution_counter_bank1 = 0;
float revolution_counter_bank2 = 0;
float revolution_counter_bank3 = 0;
float revolution_counter_bank4 = 0;

float clock_pulse_per_state_change_bank1 = 0;
float clock_pulse_per_state_change_bank2 = 0;
float clock_pulse_per_state_change_bank3 = 0;
float clock_pulse_per_state_change_bank4 = 0;

float clock_pulse_per_rev_bank1 = 0;
float clock_pulse_per_rev_bank2 = 0;
float clock_pulse_per_rev_bank3 = 0;
float clock_pulse_per_rev_bank4 = 0;

int bank1_state = 0;
int bank2_state = 0;
int bank3_state = 0;
int bank4_state = 0;

// End Variable definitions ***********




// Lcd setup
//LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7); // Remove one
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  // Remove one



// Start Setup **********
void setup() 
{

  

// calculate number of clock pulses to change state (do this in a function only once until pause or stop happens)
clock_pulse_per_state_change_bank1 = (1/((desired_rpm_bank1*steps_per_rev*clk_pulses_per_step)/(sec_per_min)))*clock_freq;
clock_pulse_per_state_change_bank2 = (1/((desired_rpm_bank2*steps_per_rev*clk_pulses_per_step)/(sec_per_min)))*clock_freq;
clock_pulse_per_state_change_bank3 = (1/((desired_rpm_bank3*steps_per_rev*clk_pulses_per_step)/(sec_per_min)))*clock_freq;
clock_pulse_per_state_change_bank4 = (1/((desired_rpm_bank4*steps_per_rev*clk_pulses_per_step)/(sec_per_min)))*clock_freq;


// calculates how many clock pulses per one revolution (for counter)
clock_pulse_per_rev_bank1 = (sec_per_min*clock_freq)/desired_rpm_bank1;
clock_pulse_per_rev_bank2 = (sec_per_min*clock_freq)/desired_rpm_bank2;
clock_pulse_per_rev_bank3 = (sec_per_min*clock_freq)/desired_rpm_bank3;
clock_pulse_per_rev_bank4 = (sec_per_min*clock_freq)/desired_rpm_bank4;
// Internal timer interrupt******************************************************
 cli();//stop interrupts
 
 //set timer1 interrupt
   TCCR1A = 0;// set entire TCCR1A register to 0                                 OP: I HAVE NO IDEA HOW ALL THIS IS WORKING OR WHERE IT'S BEING CALLED
   TCCR1B = 0;// same for TCCR1B
   TCNT1  = 0;//initialize counter value to 0
   // set compare match register for 1hz increments
   OCR1A = match_register;// = (16*10^6) / (1*4800) - 1 (must be <65536)
   // turn on CTC mode
   TCCR1B |= (1 << WGM12);
   // Set CS10 and CS12 bits for 1 prescaler
   TCCR1B |= (0 << CS12) | (1 << CS10);  
   // enable timer compare interrupt
   TIMSK1 |= (1 << OCIE1A); 
 sei();//allow interrupts
 
 //End interrupt setup ***********************************************************
 
 pinMode(2, OUTPUT); // clock output 1       ORANGE
 pinMode(3, OUTPUT); // direction output 1   BLUE
 pinMode(4, OUTPUT);
 pinMode(5, OUTPUT);
 pinMode(6, OUTPUT);
 pinMode(7, OUTPUT);
 pinMode(8, OUTPUT);
 pinMode(9, OUTPUT);
 pinMode(10, OUTPUT);
 pinMode(11, OUTPUT);
 pinMode(12, OUTPUT);
 pinMode(13, OUTPUT);  
 pinMode(A0, INPUT);  // input from shield buttons (resistor ladder)                     WHERE I'D LIKE TO USE THE 4 DIFFERENT OPTIONS FOR RPM
 pinMode(A1, OUTPUT);
 pinMode(A2, OUTPUT);
 pinMode(A3, OUTPUT);
 pinMode(A4, OUTPUT);
 pinMode(A5, OUTPUT);

 Serial.begin(250000); // begin sereal communication    (can we use this with shield?)

 // begin lcd and print
 lcd.begin(16, 2); // start the library
 //lcd.setCursor(3,0); // set cursor to line 0, column 3
 //lcd.print("message 1"); // print message, 1st char in line 0, col 4

digitalWrite(3, HIGH); // direction 1 (high = CW) DO NOT RUN REVERSE
digitalWrite(10, HIGH); // backlight on

}
// End setup **********************************************************************

// Start loop *********************************************************************
void loop() 
{
 if (Serial.available() > 0) {
} 
}
// End loop ***********************************************************************

//Start timer1 interrupt function 5000 Hz **********************************************
ISR(TIMER1_COMPA_vect)
{
clock_counter_bank1++;

if(clock_counter_bank1 >= 100)
{
  clock_counter_bank1 = 0;
  step_counter_bank1 = step_counter_bank1 + 18.6; //I changed to "1" = 5 rpm. "0.5" = 2.5 rpm. "18.6" = 93 rpm.
  if(bank2_state == 0)
       {
         bank1_state = 1;
         digitalWrite(2, HIGH);
       }
     else
       {
         bank1_state = 0;
         digitalWrite(2, LOW);
       }
     if (step_counter_bank1 > steps_per_rev)
     {
       step_counter_bank1 = 0;
       revolution_counter_bank1++;
       Serial.println(revolution_counter_bank1);
       lcd.setCursor(0,0); // set cursor to line 0, column 3
       lcd.print("Count: "); // print message, 1st char in line 0, col 4
       lcd.setCursor(10,0); // set cursor to line 0, column 3
       lcd.print(revolution_counter_bank1);
       lcd.setCursor(0,1); // set cursor to line 0, column 3
       lcd.print("RPM: ");
       lcd.setCursor(5,1); // set cursor to line 0, column 3
       lcd.print( desired_rpm_bank1);
       lcd.setCursor(11,1);
       lcd.print(clock_counter_bank1);
     }
}
/*
 if(step_counter_bank1 <= steps_per_rev_bank1) // count up until one revolution has happened.
   {
     clock_counter_bank1++;
   }
   else // one revolution has occured
     {
       clock_counter_bank1 = 0;
       revolution_counter_bank1++;
       prev_clock_counter_bank1 = clock_counter_bank1;
       //Serial.print(revolution_counter_bank3);
       //Serial.print("  ");
       //Serial.println(millis());
     }

 if(clock_counter_bank1 >= prev_clock_counter_bank1+clock_pulse_per_state_change_bank1)  // if current clock counter is greater than prev clock counter plus clck pulse per stste change, then state change.
   {
     prev_clock_counter_bank1 = clock_counter_bank1;    
   }
*/
}
//End timer1 interrupt function 5000 Hz ***********************************************

{Please, use Code Tags! </> on the menu}

Where's the start of the setup() function?

Probably in the other post that you're just supposed to know about...

The correct way to post code that's too large, is to add it as an attachment. Please read the forum introductions to learn more.

Please do not EVER post your code broken up over multiple threads. Read the sticky post at the top of the forum about how to properly post your code. Ideally, you post it in-line using code tags - like you did! If it is too large, attach it to the post. Nobody is going to hunt around for a couple of different threads to piece together your code to see how to help.

Note - you can edit your original post here and attach your code.

It's been suggested in the other thread.

Also, please don't post code as two separate pieces that have to be joined together. If the program is too long to include in a Post then add the .ino file as an attachment.

...R

Topics merged

Thank you moderator. Going further I'll make sure I attach when the data is too large. Having said that should I just delete this and redo it with an attachment. Worried my questions will be overlooked now...

justconmac:
Thank you moderator. Going further I’ll make sure I attach when the data is too large. Having said that should I just delete this and redo it with an attachment. Worried my questions will be overlooked now…

There is no need to delete anything. Just add the complete program as an attachment to your next Reply.

It would probably also help if you remind us what the problem is.

…R

combined code attached.

I would suggest you trim this down to do just 1 motor pin. I would also suggest you learn about arrays rather than multiple variables with ‘1’, ‘2’, ‘3’, etc.

In your ISR, you can not use Serial.print() or and lcd() functions. Interrupts are off during a ISR and those functions need them to function. Your ISR needs to be as small as possible. If you do want to print things out, you need to set a flag in the ISR and then react to that inside your loop().

I do not see anywhere in your code your attempt at analogRead().

All of your variables are float but they really are just integers. You should make your variables the type that matches their use.

code.ino (8.13 KB)

Thanks for the patience. Here’s a recap. I’ve inherited this program for stepper motor driver. Currently it appears to only working at one specific option for RPM. I would like to use the analog(0) of the LCD shield as four possible RPM’s. Select button; Up button; Down button; Right button. The junior engineer who wrote it also fairly straightforwardly laid out his intention and commented on a good bit. Anything in UPPERCASE are my comments. It appears the main code is running within an ISR() and I’m not able to implement these four IF statement options of LCD Shield buttons. If(analogRead(0) < 50) - Right button, etc. for the rest of the buttons.

Gas_Stepper_Driver.ino (8.75 KB)

The program is already working as is with the Serial.print() and LCD() functions working so I'm not sure I understand that.

Also to be even more clear when I say those four IF statement options, I mean a copy of the main bit of stepper motor driver code within each IF statements, just with a different RPM..

OP’s code with the big comment at the top removed and a little other shortening of comments

#include <LiquidCrystal.h>

// Variable definitions ***

char bank1_dir_pin[3] = "A1";
char bank2_dir_pin[3] = "A2";
char bank3_dir_pin[3] = "A3";
char bank4_dir_pin[3] = "A4";
int bank1_step_pin = 2;
int bank2_step_pin = 3;
int bank3_step_pin = 11;
int bank4_step_pin = 12;



float desired_rpm_bank1 = 2.5; // 8 rpm is max speed
float desired_rpm_bank2 = 5;
float desired_rpm_bank3 = 1;
float desired_rpm_bank4 = 0.5;



int rpm_bank1 = (int) desired_rpm_bank1;
int rpm_bank2 = (int) desired_rpm_bank2; //OP ADDED
int rpm_bank3 = (int) desired_rpm_bank3; //OP ADDED
int rpm_bank4 = (int) desired_rpm_bank4; //OP ADDED


float mot_steps_per_rev = 200; // steps/revolution  (CHECK THIS ON MOTOR DATASHEET)
float microstep_scaler = 8; // half step  (MUST MAKE MS1 INACTIVE (low/open) AND MS2 ACTIVE (high/5v))  (both active high in relaity, no step scaling.
float steps_per_rev = mot_steps_per_rev * microstep_scaler;
float sec_per_min = 60;
float clk_pulses_per_step = 2;

float clock_freq = (desired_rpm_bank1*clk_pulses_per_step*steps_per_rev/sec_per_min)*100; // Hz      max pwm frequency  = clock frequency/2 
int prescaler = 1;
int match_register = (16000000/(prescaler*clock_freq))-1;


float clock_counter_bank1 = 0;
float clock_counter_bank2 = 0;
float clock_counter_bank3 = 0;
float clock_counter_bank4 = 0;

float step_counter_bank1 = 0;
float step_counter_bank2 = 0;
float step_counter_bank3 = 0;
float step_counter_bank4 = 0;

float prev_clock_counter_bank1 = 0;             //THE ORGINAL CREATOR OF THIS PROGRAM MUST'VE HAD PLANS FOR THESE UNCALLED ON DECLARATIONS
float prev_clock_counter_bank2 = 0;
float prev_clock_counter_bank3 = 0;
float prev_clock_counter_bank4 = 0;

int revolution_counter_bank1 = 0;
float revolution_counter_bank2 = 0;
float revolution_counter_bank3 = 0;
float revolution_counter_bank4 = 0;

float clock_pulse_per_state_change_bank1 = 0;
float clock_pulse_per_state_change_bank2 = 0;
float clock_pulse_per_state_change_bank3 = 0;
float clock_pulse_per_state_change_bank4 = 0;

float clock_pulse_per_rev_bank1 = 0;
float clock_pulse_per_rev_bank2 = 0;
float clock_pulse_per_rev_bank3 = 0;
float clock_pulse_per_rev_bank4 = 0;

int bank1_state = 0;
int bank2_state = 0;
int bank3_state = 0;
int bank4_state = 0;

// End Variable definitions ***

// Lcd setup
//LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7); // REMOVE ONE
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  // REMOVE ONE



// Start Setup ****
void setup() 
{

  

// calculate number of clock pulses to change state (do this in a function only once until pause or stop happens)
clock_pulse_per_state_change_bank1 = (1/((desired_rpm_bank1*steps_per_rev*clk_pulses_per_step)/(sec_per_min)))*clock_freq;
clock_pulse_per_state_change_bank2 = (1/((desired_rpm_bank2*steps_per_rev*clk_pulses_per_step)/(sec_per_min)))*clock_freq;
clock_pulse_per_state_change_bank3 = (1/((desired_rpm_bank3*steps_per_rev*clk_pulses_per_step)/(sec_per_min)))*clock_freq;
clock_pulse_per_state_change_bank4 = (1/((desired_rpm_bank4*steps_per_rev*clk_pulses_per_step)/(sec_per_min)))*clock_freq;


// calculates how many clock pulses per one revolution (for counter)
clock_pulse_per_rev_bank1 = (sec_per_min*clock_freq)/desired_rpm_bank1;
clock_pulse_per_rev_bank2 = (sec_per_min*clock_freq)/desired_rpm_bank2;
clock_pulse_per_rev_bank3 = (sec_per_min*clock_freq)/desired_rpm_bank3;
clock_pulse_per_rev_bank4 = (sec_per_min*clock_freq)/desired_rpm_bank4;



  // Internal timer interrupt***
  
  cli();//stop interrupts
  
  //set timer1 interrupt
    TCCR1A = 0;// set entire TCCR1A register to 0                         //I'M STARTING TO UNDERSTAND THESE TIMER INTERRUPTS
    TCCR1B = 0;// same for TCCR1B
    TCNT1  = 0;//initialize counter value to 0
    // set compare match register for 1hz increments
    OCR1A = match_register;// = (16*10^6) / (1*4800) - 1 (must be <65536)
    // turn on CTC mode
    TCCR1B |= (1 << WGM12);
    // Set CS10 and CS12 bits for 1 prescaler
    TCCR1B |= (0 << CS12) | (1 << CS10);  
    // enable timer compare interrupt
    TIMSK1 |= (1 << OCIE1A);
  
  sei();//allow interrupts
  
  //End interrupt setup ***
  
  pinMode(2, OUTPUT); // clock output 1       ORANGE
  pinMode(3, OUTPUT); // direction output 1   BLUE
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);  
  pinMode(A0, INPUT);  // input from shield buttons (resistor ladder)
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);


  
  Serial.begin(250000); // begin sereal communication    (can we use this with shield?)


  // begin lcd and print
  lcd.begin(16, 2); // start the library
  //lcd.setCursor(3,0); // set cursor to line 0, column 3
  //lcd.print("message 1"); // print message, 1st char in line 0, col 4

digitalWrite(3, HIGH); // direction 1 (high = CW) DO NOT RUN REVERSE
digitalWrite(10, HIGH); // backlight on

}
// End setup ***


// Start loop ***
void loop() 
{
  if (Serial.available() > 0) {
  
}

  
}
// End loop ***














//Start timer1 interrupt function 5000 Hz **********************************************



ISR(TIMER1_COMPA_vect)                            //MAIN PROGRAM. THIS IS WHERE I'D LIKE THE OPTION OF FOUR DIFFERENT IF STATEMENTS ALLOWING US TO ENTER 4 DIFFERENT RPM STATES
{                                                 

clock_counter_bank1++;

if(clock_counter_bank1 >= 100)                    //THIS WOULD BE ACHIEVED BY USING THIS MAIN CODE AND A DIFFERENT RPM FOR EACH ONE WITHIN THE 4 IF STATEMENTS
{
   clock_counter_bank1 = 0;
   step_counter_bank1 = step_counter_bank1 + 18.6; //"2"= 10 RPM. "1" = 5 RPM. "0.5" = 2.5 RPM. "18.6" = 93 RPM.
   if(bank1_state == 0)
        {
          bank1_state = 1;
          digitalWrite(2, HIGH);
        }
      else
        {
          bank1_state = 0;
          digitalWrite(2, LOW);
        }

      if (step_counter_bank1 > steps_per_rev)
      {
        step_counter_bank1 = 0;
        revolution_counter_bank1++;
        Serial.println(revolution_counter_bank1);
        lcd.setCursor(0,0); // set cursor to line 0, column 3
        lcd.print("Count: "); // print message, 1st char in line 0, col 4
        lcd.setCursor(10,0); // set cursor to line 0, column 3
        lcd.print(revolution_counter_bank1);
        lcd.setCursor(0,1); // set cursor to line 0, column 3
        lcd.print("RPM: ");
        lcd.setCursor(5,1); // set cursor to line 0, column 3
        lcd.print( desired_rpm_bank1);
        lcd.setCursor(11,1);
        lcd.print(clock_counter_bank1);
      }

}
/*
  if(step_counter_bank1 <= steps_per_rev_bank1) // count up until one revolution has happened.
    {
      clock_counter_bank1++;
    }
    else // one revolution has occured
      {
        clock_counter_bank1 = 0;
        revolution_counter_bank1++;
        prev_clock_counter_bank1 = clock_counter_bank1;
        //Serial.print(revolution_counter_bank3);
        //Serial.print("  ");
        //Serial.println(millis());
      }

  if(clock_counter_bank1 >= prev_clock_counter_bank1+clock_pulse_per_state_change_bank1)  // if current clock counter is greater than prev clock counter plus clck pulse per stste change, then state change.
    {
      prev_clock_counter_bank1 = clock_counter_bank1;
      
     
    }

*/

}
//End timer1 interrupt function 5000 Hz ****

…R

There should NO lcd statements in an ISR. They need interrupts to work and interrupts are off. Also they are slow and the code in an ISR should complete very quickly. 100 microsecs would a long time.

I think the way to get your speed options to work is to have the ISR derive its speed from a variable that is updated in the main part of the program.

...R

+1 on what Robin said. If your ISR is running at 5000Hz then the ISR must execute in less than 200us and preferably much less.

This is more like what you may need.

#include <LiquidCrystal.h>

/*
   Hardware specs

   stepper motors, 200 steps per revolution
   will always run at 1/8 step selection on controllers, gives 1600 steps per revolution,  hard wire these
   maximum input signal frequency is 500,000 Hz


   Desired functionality

   direction control (CW/CCW) for each bank of (3) steppers
   speed control for each bank of (3) steppers
   rotation count for each bank of (3) steppers
   Stop, go, pause, reset counters
   all of the above functionality but for (ALL) 12 motors on single box at the same time.
   minimum 0.1 rev/min (between 0 and 20 rpm.. above 20 rpm, increment 1 rpm)  maximum 90 rev/min

*/

// Variable definitions ***********************************************************


float desired_rpm_bank1 = 2.5; // 8 rpm is max speed

int rpm_bank1 = (int) desired_rpm_bank1;

const int mot_steps_per_rev = 200; // steps/revolution  (CHECK THIS ON MOTOR DATASHEET)
const int microstep_scaler = 8; // half step  (MUST MAKE MS1 INACTIVE (low/open) AND MS2 ACTIVE (high/5v))  (both active high in relaity, no step scaling.
const int steps_per_rev = mot_steps_per_rev * microstep_scaler;
const int sec_per_min = 60;
const int clk_pulses_per_step = 2;

float clock_freq = (desired_rpm_bank1*clk_pulses_per_step*steps_per_rev / sec_per_min) * 100; // Hz      max pwm frequency  = clock frequency/2
const int prescaler = 1;
unsigned int match_register = (16000000 / (prescaler*clock_freq)) - 1;

float clock_counter_bank1 = 0;
float step_counter_bank1 = 0;
int revolution_counter_bank1 = 0;
float clock_pulse_per_state_change_bank1 = 0;

float clock_pulse_per_rev_bank1 = 0;

int bank1_state = 0;

float currentRPM, prevRPM;
volatile bool report_flag = false;

// End Variable definitions ******************************************************

// Lcd setup
//LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7); // REMOVE ONE
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  // REMOVE ONE

// Start Setup ********************************************************************
void setup()
{
  // calculate number of clock pulses to change state (do this in a function only once until pause or stop happens)
  clock_pulse_per_state_change_bank1 = (1 / ((desired_rpm_bank1 * steps_per_rev * clk_pulses_per_step) / (sec_per_min))) * clock_freq;

  // calculates how many clock pulses per one revolution (for counter)
  clock_pulse_per_rev_bank1 = (sec_per_min * clock_freq) / desired_rpm_bank1;


  // Internal timer interrupt******************************************************

  cli();//stop interrupts

  //set timer1 interrupt
  TCCR1A = 0;// set entire TCCR1A register to 0                         //I'M STARTING TO UNDERSTAND THESE TIMER INTERRUPTS
  TCCR1B = 0;// same for TCCR1B
  TCNT1  = 0;//initialize counter value to 0
  // set compare match register for 1hz increments
  OCR1A = match_register;// = (16*10^6) / (1*4800) - 1 (must be <65536)
  // turn on CTC mode
  TCCR1B |= (1 << WGM12);
  // Set CS10 and CS12 bits for 1 prescaler
  TCCR1B |= (0 << CS12) | (1 << CS10);
  // enable timer compare interrupt
  TIMSK1 |= (1 << OCIE1A);

  sei();//allow interrupts

  //End interrupt setup ***********************************************************

  pinMode(2, OUTPUT); // clock output 1       ORANGE
  pinMode(3, OUTPUT); // direction output 1   BLUE
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(A0, INPUT);  // input from shield buttons (resistor ladder)
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);

  Serial.begin(250000); // begin sereal communication    (can we use this with shield?)


  // begin lcd and print
  lcd.begin(16, 2); // start the library
  //lcd.setCursor(3,0); // set cursor to line 0, column 3
  //lcd.print("message 1"); // print message, 1st char in line 0, col 4

  digitalWrite(3, HIGH); // direction 1 (high = CW) DO NOT RUN REVERSE
  digitalWrite(10, HIGH); // backlight on
}
// End setup **********************************************************************

// Start loop *********************************************************************
void loop()
{
  int reading = analogRead(A0);

  if ( reading < 50 ) {
    currentRPM = 2.5;
  }
  else if ( reading < 100 ) {
    currentRPM = 3.0;
  }
  else if ( reading < 150) {
    currentRPM = 4.5;
  }
  else {
    currentRPM = 6.0;
  }

  if ( currentRPM != prevRPM ) {
    // update speed
    clock_freq = (currentRPM * clk_pulses_per_step * steps_per_rev / sec_per_min) * 100; // Hz      max pwm frequency  = clock frequency/2
    match_register = (16000000 / (prescaler * clock_freq)) - 1;
    OCR1A = match_register;// = (16*10^6) / (1*4800) - 1 (must be <65536)
    desired_rpm_bank1 = currentRPM;
  }
  prevRPM = currentRPM;

  if ( report_flag ) {
    Serial.println(revolution_counter_bank1);
    lcd.setCursor(0, 0); // set cursor to line 0, column 3
    lcd.print("Count: "); // print message, 1st char in line 0, col 4
    lcd.setCursor(10, 0); // set cursor to line 0, column 3
    lcd.print(revolution_counter_bank1);
    lcd.setCursor(0, 1); // set cursor to line 0, column 3
    lcd.print("RPM: ");
    lcd.setCursor(5, 1); // set cursor to line 0, column 3
    lcd.print( desired_rpm_bank1);
    lcd.setCursor(11, 1);
    lcd.print(clock_counter_bank1);
    report_flag = false;
  }


}
// End loop ***********************************************************************


//Start timer1 interrupt function 5000 Hz **********************************************
ISR(TIMER1_COMPA_vect)                            //MAIN PROGRAM. THIS IS WHERE I'D LIKE THE OPTION OF FOUR DIFFERENT IF STATEMENTS ALLOWING US TO ENTER 4 DIFFERENT RPM STATES
{
  clock_counter_bank1++;

  if (clock_counter_bank1 >= 100)                   //THIS WOULD BE ACHIEVED BY USING THIS MAIN CODE AND A DIFFERENT RPM FOR EACH ONE WITHIN THE 4 IF STATEMENTS
  {
    clock_counter_bank1 = 0;
    step_counter_bank1 = step_counter_bank1 + 18.6; //"2"= 10 RPM. "1" = 5 RPM. "0.5" = 2.5 RPM. "18.6" = 93 RPM.
    if (bank1_state == 0)
    {
      bank1_state = 1;
      digitalWrite(2, HIGH);
    }
    else
    {
      bank1_state = 0;
      digitalWrite(2, LOW);
    }

    if (step_counter_bank1 > steps_per_rev)
    {
      step_counter_bank1 = 0;
      revolution_counter_bank1++;
      report_flag = true;
    }

  }
  /*
    if(step_counter_bank1 <= steps_per_rev_bank1) // count up until one revolution has happened.
      {
        clock_counter_bank1++;
      }
      else // one revolution has occured
        {
          clock_counter_bank1 = 0;
          revolution_counter_bank1++;
          prev_clock_counter_bank1 = clock_counter_bank1;
          //Serial.print(revolution_counter_bank3);
          //Serial.print("  ");
          //Serial.println(millis());
        }

    if(clock_counter_bank1 >= prev_clock_counter_bank1+clock_pulse_per_state_change_bank1)  // if current clock counter is greater than prev clock counter plus clck pulse per stste change, then state change.
      {
        prev_clock_counter_bank1 = clock_counter_bank1;


      }

  */
}
//End timer1 interrupt function 5000 Hz ************************************************

Robin2:
OP's code with the big comment at the top removed and a little other shortening of comments

OP didn't actually come out and say it, but I inferred from the context .......... Somebody actually got PAID to write that code?

Correct, an ELDP, if you're not familiar basically a engineering intern within our company. I am merely a novice looking to improve my skills and knowledge base by taking some time over the next couple months to improve in C++. Specifically this code attached to the fixture which we rarely use. Hence the time I have to spend sparingly on it.

Also BLH, that does work, however I need to stay latched in that speed after button is pressed. Think I have an idea how to approach that. What is confusing to me is how the ISR relates to the main loop. I always thought ISR were for breaking out of something, resetting if you will.