Millis() function

I'm new at arduino stuff

there is a lot of guide about millis function but i can not apply this function to my code.

here it is

int rpm_sensor = 8;
int motor = 3;
unsigned long start_time = 0;
unsigned long end_time = 0;
int steps=0;
unsigned int steps_old=0;
float temp=0;
float rpm=0;
float oldrpm=0;
int valve=11;

void setup()
{
Serial.begin(9600);
pinMode(motor, OUTPUT);
pinMode(valve, OUTPUT);
pinMode(rpm_sensor,INPUT_PULLUP);

}
void loop(){

start_time=millis();
end_time=start_time+1000;
while(millis()<end_time)
{
if(digitalRead(rpm_sensor))
{
delayMicroseconds(2);

if(digitalRead(rpm_sensor));
{
steps=steps+1;
while(digitalRead(rpm_sensor));
}

}

}
//temp=steps-steps_old;
temp=steps;
steps=0;
steps_old=steps;
rpm=(temp/20)*60;
Serial.println (rpm);

if(rpm>200)digitalWrite(motor,LOW);

if(rpm>200) digitalWrite (valve,HIGH);

if(rpm<80) digitalWrite (valve,LOW);
if(rpm<70)digitalWrite(motor,HIGH);
}

I'm working on a project which is brake simulation. there is vfd controller, rpm sensor and pneumatic valve

the code is working perfectly btw but i need a little delay when the motor turns off and valve opens i know i can't use delay function here

im confused i tried to apply arduinos blinkwithoutdelay example but i cant

sorry for my English :confused:

How long is the required "little" delay ?

I note that in your code you have written blocking code using millis() with a blocked period of 1 second, so adding a short delay() to the code may not actually be a problem. Where exactly in the code do you want the "little" delay ?

i need a 2 second delay. sorry i didn't imply its value

i need a 2 second delay

Where and when exactly ?

It sounds like a state machine could be implemented to do what you want. Please describe exactly what you want the program to do, in what order and how long each step should take.

if(rpm>200)digitalWrite(motor,LOW);

if(rpm>200) digitalWrite (valve,HIGH);

if(rpm<80) digitalWrite (valve,LOW);
if(rpm<70)digitalWrite(motor,HIGH);

at the top the when the motor gets low valve gets high simultaneously due to rpm value. i need 2 second delay between when the motor stop and valve opens.

when the rpm reaches 200 motor will LOW >> 2 second delay >> valve gets HIGH.

i will explain the system

it is a brake simulation 3 phase motor connected to wheel mechanism with brake when the motor started it reaches 200rpm and motor will stop, simultaneously valve gets high which is pneumatic valve and apply brake to brake disc then rpm decreases when it is 80 valve gets LOW, when it is 70 motor gets HIGH. It is in a loop

we are measuring the brake discs temperature.

i need 2 second delay between motor gets LOW and valve gets HIGH because safety precaution

i need 2 second delay between motor gets LOW and valve gets HIGH

  if (rpm > 200)
  {
    digitalWrite(motor, LOW);
    delay(2000);
    digitalWrite (valve, HIGH);
  }

Why do you need to use millis() if that is what you are thinking ?

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R