Conveyer Belt Speed

i have to update the speed of the conveyor belt every 1 second and i´m using analogwrite
analogwrite(pin,63);//25%speed
analogwrite(pin,63);//50%speed
analogwrite(pin,191);//75%speed
i tried with delay of 1 sec and it doesn´t work, and also i tried millis.

long antesMillis = 0;
long intervalo = 1000;
int k=0;
int x=0;
void setup(){
  
  pinMode(8,OUTPUT);//motor
  pinMode(10, INPUT);//boton
  pinMode(11, INPUT);//sensor
  
  
}
void loop(){
  if  (digitalRead(10)==LOW){
   x=1;
  }
  if  (x==1){
   unsigned long ahoraMillis = millis();
   if(ahoraMillis - antesMillis > intervalo) {
     if (k==0){
      analogWrite(8,63);//25%speed
      k=1;
     }
     else if (k==1){
      analogWrite(8,63);//50%speed
      k=2;
     }
     else if (k==2){
      analogWrite(8,191);//75%speed
      k=3;
     }

    
  
   antesMillis = ahoraMillis;
   }
  }
  else if  (digitalRead(11)==HIGH){
  analogWrite(8,0);//0%speed
  }
}

Is there a question in there?

Can we see your code, posted in code tags?

And a schematic?

When i press a button the conveyor belt starts at 25% after 1 sec 50 after 1 sec 75 then hits a sensor and stops.

Hello
Nice task.
And what is your question?

Something like:

void loop()
{
  const int interval = 1000; // 1 second
  static unsigned long timer = 0;
  const unsigned long elapsed;

  elapsed = millis() - timer;
  if(elapsed >= interval)
  {
    timer += interval;
    updateSpeed();
  }
}

?

My question is how i do that.
Increase the speed of the conveyor belt 1 sec after the button was pressed, lets say i press a button and the conveyor belt starts at 25% speed, after 1 sec it increases to 50%, after an other sec it reaches 75% and finally hits the sensor and the conveyor belt stops.

Which pins are the button and sensor connected to and how are they wired?

Pin 10 button, and sensor pin 11, the button is INPUT_PULLUP

Hello
You may post your current sketch to see how we can help?
My crystal ball is in the dish washer :grinning:

1 Like

What kind of sensor? When activated does pin 11 go HIGH or LOW?

It goes HIGH

Which pin turns the motor on and off? Is ON HIGH or LOW?

Pin 10 turns the motor on

On is low

If the button is low the motor starts

When the sensor is not activated does it need a pulldown resistor to hold the pin LOW?

You said pin 10 was the button pin.

When the pin 10 is low the motor is running

Which pin is the motor connected to?

The sensor is low when there is nothing obstructing it