With button, I adjust the speed of the DC motor. Every time I press the button,

I want that if I press the button once, the motor runs at a speed, if I press it another time, it goes faster and so on up to 4 button presses. This is the sketch

int PinMotore = 10;
int NumDiVolte = 0;
int PulsPin = 52;
int StatoPuls = 0;


void setup() {
  Serial.begin(9600);
  pinMode(PinMotore, OUTPUT);
  pinMode(PulsPin, INPUT);
}

void loop() {
  StatoPuls = digitalRead(PulsPin);

  if (StatoPuls == HIGH) {
    if (NumDiVolte < 5) {
      NumDiVolte = NumDiVolte + 1;
    }
  }
  else {
    NumDiVolte = 0;
  }

  if (NumDiVolte == 1) {
    analogWrite(PinMotore, 63)  ;
    Serial.println("Velocità impostata a = 1");
  }

  if (NumDiVolte == 2) {
    analogWrite(PinMotore, 126) ;
    Serial.println("Velocità impostata a = 2");

  }

  if (NumDiVolte == 3) {
    analogWrite(PinMotore, 189) ;
    Serial.println("Velocità impostata a = 3");
  }

  if (NumDiVolte == 4) {
    analogWrite(PinMotore, 252);
    Serial.println("Velocità impostata a = 4");
  }

}

If I press the button, it starts all the “if” and the variable (as seen in the serial monitor) splashes from 1 to 4 in an instant.

“NumDiVolte” is the variable that indicates the number of times I press the button

the led then in a moment goes from a low brightness to a high brightness (instantly !!!)

it is as if when I press the button, arduino starts all the “if”.

You need to make it so that each new press only counts as one: right now, loop() is so fast that it counts many times while you have the button held for a "human instant" which is a long time in Arduino terms.

Study the state change detect tutorial.

Or in otherwords your program logic needs to be:

if and only if the button was not pressed the last time I looked at it and is pressed now, then I update the counter.

You also need debounce logic.

In this case u can just use delay(100) for this, and then check again if the button is still HIGH. When you need a constant loop, you should take a look at the "blink without delay" example and make an alternitif for delay.

But delay should do it here.

  if (NumDiVolte == 1) {
    analogWrite(PinMotore, 63)  ;
    Serial.println("Velocità impostata a = 1");
  }

  if (NumDiVolte == 2) {
    analogWrite(PinMotore, 126) ;
    Serial.println("Velocità impostata a = 2");

  }
...
..

or

    analogWrite(PinMotore, NumDiVolte*64-1) ;
    Serial.print("Velocità impostata a = ");
    Serial.println(NumDiVolte);

It looks like if your button is not pressed it turns off the motor. Did you intend that? That means that even when you get the motor running, the moment you stop pressing the button it will turn off the motor.

ok thanks, i'm a beginner and i don't understand much. Is someone kind enough to modify my sketch?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got you button wired the the Controller.
It looks like, from the code, that your button is wired between the digital input pin and 5V.
Do you have a 10K resistor from the digital input pin to gnd.

The reason is when the button is not pressed the digital input pin does not go LOW, you have to make it go LOW, that is what the 10K resistor does.

Tom... :slight_smile: