Moding a vacuum

hi my name is Jacob and i am a beginner with arduino i am trying to code an arduino to change the speed of a vacuum motor using an esc with a pushbutton and i want 4 leds to show me if its on or off and what power it is (power 1 = 25%, power 2 = 50%...) and when I hold the button for 2 seconds it turns off (put the power to 0)
so i need help for the code

Welcome to the forum

What code have you written so far ?

We can't see your code.

/*********************
Simple toggle switch
Created by: P.Agiakatsikas
*********************/
int key = 0;
int button = 2;

int led_1 = 8;
int led_2 = 9;
int led_3 = 10;
int led_4 = 11;

int status = 0;

void setup(){
pinMode(led_1, OUTPUT);
pinMode(led_2, OUTPUT);
pinMode(led_3, OUTPUT);
pinMode(led_4, OUTPUT);
pinMode(button, INPUT_PULLUP); // set the internal pull up resistor, unpressed button is HIGH
status = 0;
Serial.begin(9600);
Serial.print(status);
}

void loop(){
key = digitalRead(button);
if (status > 0) {
digitalWrite (led_1, true);
}else{
digitalWrite (led_1, false);
}
if (status > 1) {
digitalWrite (led_2, true);
}else{
digitalWrite (led_2, false);
}
if (status > 2) {
digitalWrite (led_3, true);
}else{
digitalWrite (led_3, false);
}
if (status > 3) {
digitalWrite (led_4, true);
}else{
digitalWrite (led_4, false);
}

if(key == LOW )
{
delay(100);
status ++;
}
if(status > 4) {
status = 1;
}else{
}

Serial.print(status);
}`

the probleme i have is if i hold the button it keep incresing the number

Please post your code here using code tags when you do

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

/*********************
Simple toggle switch
Created by: P.Agiakatsikas
*********************/
int key = 0;
int button = 2;

int led_1 = 8;
int led_2 = 9;
int led_3 = 10;
int led_4 = 11;

int status = 0;

void setup(){
pinMode(led_1, OUTPUT);
pinMode(led_2, OUTPUT);
pinMode(led_3, OUTPUT);
pinMode(led_4, OUTPUT);
pinMode(button, INPUT_PULLUP); // set the internal pull up resistor, unpressed button is HIGH
status = 0;
Serial.begin(9600);
Serial.print(status);
}

void loop(){
  key = digitalRead(button);
if (status > 0) {
  digitalWrite (led_1, true);
}else{
  digitalWrite (led_1, false);
}
if (status > 1) {
  digitalWrite (led_2, true);
}else{
  digitalWrite (led_2, false);
}
if (status > 2) {
  digitalWrite (led_3, true);
}else{
  digitalWrite (led_3, false);
}
if (status > 3) {
  digitalWrite (led_4, true);
}else{
  digitalWrite (led_4, false);
}

if(key == LOW )
  {
    delay(100);
    status ++;
  }
if(status > 4) {
  status = 1;
}else{
}

Serial.print(status);
}

You need to detect when the button becomes pressed rather than when it is pressed
See the StateChangeDetection example in the IDE

so affter chnage the ledPin to LedPin_1 the led stay on when reset

/*
  State change detection (edge detection)

  Often, you don't need to know the state of a digital input all the time, but
  you just need to know when the input changes from one state to another.
  For example, you want to know when a button goes from OFF to ON. This is called
  state change detection, or edge detection.

  This example shows how to detect when a button or button changes from off to on
  and on to off.

  The circuit:
  - pushbutton attached to pin 2 from +5V
  - 10 kilohm resistor attached to pin 2 from ground
  - LED attached from pin 13 to ground through 220 ohm resistor (or use the
    built-in LED on most Arduino boards)

  created  27 Sep 2005
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/StateChangeDetection
*/

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin_1 = 8;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin_1, OUTPUT);
  digitalWrite(ledPin_1, LOW);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(100);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;


  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (buttonPushCounter % 4 == 0) {
    digitalWrite(ledPin_1, HIGH);
  } else {
    digitalWrite(ledPin_1, LOW);
  }

}

In your original sketch you used INPUT_PULLUP and detected LOW as a button press, which is correct

The code in post #9 uses INPUT and detects HIGH as a button press

So, how is the input wired ?

i put a ressister to gruond and use a wire at 5v for trigger

ok now its working like i whent now i juste need i whay to detec for how long the button is pressed so if its over 2000ms it set to 0 and if its between 100 and 2000 is set to 1

I hope this is a 12 volt automobile vacuum!

Save the value of millis() when the button becomes pressed then you can check how long it has been pressed by comparing the start time with the current value of millis() then do what you want

yes it is its a cheap 50$ CAD vacuum at 3s 11.1 and it whant to overvolt to 4s 14.5v

can you make it ? i am not sure how to do it

i code more whit ue4 blueprite

i fuond a code for long and short press detection and i add so it turn on when short and off when its long put affter adding serial.print in long press the les stop working. i am so confuse

/*
 * Created by ArduinoGetStarted.com
 *
 * This example code is in the public domain
 *
 * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-button-long-press-short-press
 */

// constants won't change. They're used here to set pin numbers:
const int BUTTON_PIN = 2; // the number of the pushbutton pin
const int SHORT_PRESS_TIME = 2000; // 1000 milliseconds
const int LONG_PRESS_TIME  = 2000; // 1000 milliseconds

// Variables will change:
int lastState = LOW;  // the previous state from the input pin
int currentState;     // the current reading from the input pin
unsigned long pressedTime  = 0;
unsigned long releasedTime = 0;
int Led_pin_1 = 8;


void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(Led_pin_1, OUTPUT);
}

void loop() {
  // read the state of the switch/button:
  currentState = digitalRead(BUTTON_PIN);

  if(lastState == HIGH && currentState == LOW)        // button is pressed
    pressedTime = millis();
  else if(lastState == LOW && currentState == HIGH) { // button is released
    releasedTime = millis();

    long pressDuration = releasedTime - pressedTime;

    if( pressDuration < SHORT_PRESS_TIME )
      Serial.println("on");
      digitalWrite(Led_pin_1, HIGH);

    if( pressDuration > LONG_PRESS_TIME )
      Serial.println("off");
      digitalWrite(Led_pin_1, LOW);
  }
  delay(100);

  // save the the last state
  lastState = currentState;
}

You have to fully analyze a program to understand it, and you have to fully analyze the problem that it solves if you don't. Take some time on it, realize that using other peoples code depends on understanding it too, so the documentation is important.

For inspiration, try looking at some code that does work, like a library or some tutorial code.

I think you forgot a couple of pairs of {}