need help to add timing in serial. #Arduino code

i was working with stepper motor 28BYJ-48 with arduino uno… i am using if conditional function with serial read and cases. i just want display duration of code that run in serial monitor . example “case c” moves the stepper to 90 degree. it takes 1 second from 0 deg to 90 deg and so the “case s” . i want to display the time that taken to move 0 deg to 90 deg in the serial monitor .

[code]#include <CheapStepper.h>
CheapStepper stepper (8,9,10,11); 
boolean moveClockwise = true;
void setup() 
{
  stepper.setRpm(15);
  Serial.begin(9600);
   }
void loop() {
if (Serial.available() > 0) 
{
  int inByte = Serial.read();
   switch (inByte)
   {
      case 'c':
      moveClockwise = false;
      stepper.moveDegrees (moveClockwise, 90);
      break;
      
      case 's':
      moveClockwise = true;
      stepper.moveDegrees (moveClockwise, 90);
      
   }
}
}

[/code]

balajiatcurious: i just want display duration of code that run in serial monitor .

I don't understand. What code do you want to display the duration for?

If you want to measure the time for a block of code save the value of micros() before and after the block and the difference between the two values will be the time taken.

...R

It's not clear what you mean by "display duration of code". You'll need to provide a detailed description if you want our help.

You might be interested to know that in Arduino IDE 1.8.8 the Serial Monitor has a "Show timestamp" feature you can enable by checking the box next to it.

Robin2: I don't understand. What code do you want to display the duration for?

If you want to measure the time for a block of code save the value of micros() before and after the block and the difference between the two values will be the time taken.

...R

i just updated . i just need the time taken to move the stepper 0 deg to 90 deg.

balajiatcurious: example "case c" moves the stepper to 90 degree. it takes 1 second from 0 deg to 90 deg and so the "case s" . i want to display the time that taken to move 0 deg to 90 deg in the serial monitor .

Sorry, but I still don't understand. You say the stepper takes 1 second to move 90 deg so the answer to your question seems to be

Serial.println(1);

In your program you have set the RPM to 15 in this line

 stepper.setRpm(15);

If you change it to this

byte stepperSpeed = 15;
void setup() 
{
  stepper.setRpm(stepperSpeed);
  // etc

then you can use the variable stepperSpeed in calculations. For example

unsigned long millisecsPerRev = 1000 * 60 / stepperSpeed; 
unsigned long millisecsPerStep = millisecsPerRev / stepsPerRev;

...R