using of timer interrupt by taking its' time to execute from serial monitor

Hi friends,
I want to get timer interrupt's execution time from serially available data in the following program. I tried but unable to solve it.
Please help me.

#include <TimerOne.h>
#include <Stepper.h>
const byte interruptPin = 2;
const int stepsPerRevolution = 200;
volatile int motorSpeed = 0;
Stepper myStepper(stepsPerRevolution, 3,5,9,10);

void setup()
{
  Serial.begin(9600);
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), hardware_interrupt, CHANGE);
  Timer1.initialize(20000000); // in microseconds (1000000*t second)
  Timer1.attachInterrupt(timer_interrupt);
}

void hardware_interrupt()
{
  motorSpeed=0;
}

void timer_interrupt()
{
  motorSpeed=0;
}

void loop()
{
   if(Serial.available()>0)
   {
   motorSpeed = 100;
   myStepper.setSpeed(motorSpeed);
   myStepper.step(1);
   }
}

Not sure I get the role of your interrupt...

Do I understand your question is really "how can I enter and recognize a value typed on the serial monitor, so that I can use it in my code to do something?"

Answer would then be in this tutorial Serial Input Basics


Please correct your post above and add code tags around your code:
[code]`` [color=blue]// your code is here[/color] ``[/code].

It should look like this:// your code is here
(Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

parikshitsaha:
Timer1.initialize(20000000); // in microseconds (1000000*t second)

I am interested to give this the value inside "Timer1.initialize" from serial monitor.
if am going to put it inside "void loop" Timer interrupt is not executing.
And if am going to do serial.read from "void setup" then it'll not run real-time always without doing hardware reset.
Please help me solve this out.

Do I get it right you want to do something every 2 seconds ? You don’t need a timer nor an interrupt for that... just use millis()

Do you want to read the serial just once in setup or do you want to be able to change the period also whilst the loop is looping?

J-M-L:
Do I get it right you want to do something every 2 seconds ?

yes but 2sec is not always fixed. i'm going to give it realtime while executing and from serial monitor it'll get it whether its 2sec or 5sec or something else.

J-M-L:
Do you want to read the serial just once in setup or do you want to be able to change the period also whilst the loop is looping?

I want to be able to change the period also whilst the loop is looping.

J-M-L:
You don’t need a timer nor an interrupt for that... just use millis()

See this

...
const byte interruptPin = 2;
...
  pinMode(interruptPin, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(interruptPin), hardware_interrupt, CHANGE);
 ...
void hardware_interrupt()
{
  motorSpeed=0;
}
...

My code has also hardware interrupt along with the timer interrupt. millis(), as well as hardware interrupt both are not working same time.

parikshitsaha:
My code has also hardware interrupt along with the timer interrupt. millis(), as well as hardware interrupt both are not working same time.

There is no problem with millis() and correctly programmed interrupts.

Serial use interrupts too... and if your typical timing is 2s or 5s then not sure there is a need for interrupts (unless the loop takes a reeaaaaaaalllly long time to execute)

Use the serial tutorial pointed above to add getting a message from the console and use atol() to convert that to a number when you got an entry

What do you plan to do in the interrupt ?.

J-M-L:
What do you plan to do in the interrupt ?.

My plan, as well as the code, is for running a motor for a specific duration of time (time is taken from serial monitor). and in the meanwhile if I pressed the interrupt switch it'll be going to be stopped else it'll run for the serially read time duration & get stopped.

I don’t see any need for interrupt. Just have the loop listen for serial input and button event and check millis() if the motor is running

Thank you. I was having hardware issue