using pullstime

Do you think that it would be a good idea to change the value of buttonPushCounter somewhere in the program ?

maybe like this?

if (buttonPushCounter % 1 == 1) {
m1.attach(9);
m1.writeMicroseconds(myPulsetime + pulseIncrement);
delay(1);
} else {
m1.writeMicroseconds(1000);
delay(1);

if (buttonPushCounter % 2 == 2) {
m1.attach(9);
m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement);
delay(1);
} else {
m1.writeMicroseconds(1000);
delay(1);

if (buttonPushCounter % 3 == 3) {
m1.attach(9);
m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement + pulseIncrement);
delay(1);
} else {
m1.writeMicroseconds(1000);
delay(1);

if (buttonPushCounter % 4 == 4) {
m1.attach(9);
m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement + pulseIncrement + pulseIncrement);
delay(1);
} else {
m1.writeMicroseconds(1000);
delay(1);

}
}
}
}
}

or maybe this

int buttonPushCounter = buttonstate; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0;

I am beginning to think that you are pretending not to understand on purpose.

In order for the value written to m1 to vary depending on how many times the button has been pushed you must count the button pushes, perhaps by adding one to it every time the button becomes pressed. That is presumably what the buttonPushCounter variable is for judging by its name.

Where in you latest code suggestions do you add one to buttonPushCounter when the button becomes pressed ?

Once that has been sorted out we can tackle the rest of your code which is at best clumsy and repetitive at at its worse just plain wrong.

Look I know that I am bad, so this going to take a long time because sometimes the language you use is proper expert even if simplified, for me it is still complicated, sorry
would this do it?

#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
const int mypulsebase =  (1000);
const int pulseIncrement = (250);
const int myPulsetime =  mypulsebase;


int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

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

}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonPushCounter+1) {
    m1.attach(9);
    m1.writeMicroseconds(myPulsetime + pulseIncrement);
    delay(1);
  } else {
    m1.writeMicroseconds(1000);
    delay(1);



    if (buttonPushCounter+2) {
      m1.attach(9);
      m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement);
      delay(1);
    } else {
      m1.writeMicroseconds(1000);
      delay(1);



      if (buttonPushCounter+3) {
        m1.attach(9);
        m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement + pulseIncrement);
        delay(1);
      } else {
        m1.writeMicroseconds(1000);
        delay(1);


        if (buttonPushCounter+4) {
          m1.attach(9);
          m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement + pulseIncrement + pulseIncrement);
          delay(1);
        } else {
          m1.writeMicroseconds(1000);
          delay(1);





        }
      }
    }
  }
}

Matyk:
would this do it?

It compiles without error or warning. Does it do what you want it to do?
I don't see any place where 'buttonPushCounter' is set to anything other than 0.

void loop() {
  buttonState = digitalRead(buttonPin);
  if (buttonPushCounter+1) {
    m1.attach(9);
    m1.writeMicroseconds(myPulsetime + pulseIncrement);
    delay(1);
  } else {
...
  }
}

Since buttonState is set but not used and buttonPushCounter is always 0 this is equivalent to:

void loop() {
    m1.attach(9);
    m1.writeMicroseconds(1250);
    delay(1);
}

I don't think that will do what you wanted.

it compiles without an error, but I dont know how I would make it add the button push counter, would this do it?

int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0;

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

}

void loop() {
buttonState = digitalRead(buttonPin);

if (buttonPushCounter == 1) {
m1.attach(9);
m1.writeMicroseconds(myPulsetime + pulseIncrement);
delay(1);
} else {
m1.writeMicroseconds(1000);
delay(1);

if (buttonPushCounter == 2) {
m1.attach(9);
m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement);
delay(1);
} else {
m1.writeMicroseconds(1000);
delay(1);

if (buttonPushCounter == 3) {
m1.attach(9);
m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement + pulseIncrement);
delay(1);
} else {
m1.writeMicroseconds(1000);
delay(1);

if (buttonPushCounter == 4) {
m1.attach(9);
m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement + pulseIncrement + pulseIncrement);
delay(1);
} else {
m1.writeMicroseconds(1000);
delay(1);

}
}
}
}
}

Please can you learn to do one simple thing to make life easier for those of us who are trying to help you. It has nothing to do with coding so don't panic

Next time you post any code, Auto Format it in the IDE first then copy it and paste it to your post, select it and click on the </> icon above the editor window to add code tags.

Doing that makes the code easier to read and copy to an editor. Look at the difference between John/my posts that contain code and yours

Matyk:
it compiles without an error, but I dont know how I would make it add the button push counter, would this do it?

No. You still have no code to check for changes to the button state and incrementing the push count. It still stays at zero forever. Since zero is not equal to 1, 2, 3, or 4 then none of the 'if' statements will do anything.

I really have no more ideas...

#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
const int mypulsebase =  (1000);
const int pulseIncrement = (250);
const int myPulsetime =  mypulsebase;


int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

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

}

void loop() {
  buttonState = digitalRead(buttonPin);

  if (buttonPushCounter % 1 == 0) {
    m1.attach(9);
    m1.writeMicroseconds(myPulsetime + pulseIncrement);
    delay(1);
  } else {
    m1.writeMicroseconds(1000);
    delay(1);



    if (buttonPushCounter % 2 == 0) {
      m1.attach(9);
      m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement);
      delay(1);
    } else {
      m1.writeMicroseconds(1000);
      delay(1);



      if (buttonPushCounter % 3 == 0) {
        m1.attach(9);
        m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement + pulseIncrement);
        delay(1);
      } else {
        m1.writeMicroseconds(1000);
        delay(1);


        if (buttonPushCounter % 4 == 0) {
          m1.attach(9);
          m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement + pulseIncrement + pulseIncrement);
          delay(1);
        } else {
          m1.writeMicroseconds(1000);
          delay(1);





        }
      }
    }
  }
}

I really have no more ideas...

You need to know whether the button has become pressed since the last time that you looked at its state.

To do this you need a variable to hold the state the last time it was checked. You have one. It is named lastButtonState. You also need a variable to hold the state now. You have one. It is named buttonState. They are both global variables and both start off being zero.

You need to know what state the button pin is when the button is not pressed. The easy way to do this is to set the pin's state to INPUT_PULLUP using pinMode rather than just INPUT. This causes the pin to be normally kept HIGH. Arrange your wiring to take the pin to GND when the button is pressed. So, when the button is not pressed the pins state will be HIGH and when it is pressed it will be LOW.

Now, each time through loop() read the state of the pin putting the result in buttonState. Compare it with lastButtonState and if they are the same then nothing has changed. However, if they are different then either the button was pressed or released. Test the value of buttonState. If it is LOW then the button has become pressed since last checked. so let's act on it by adding 1 to buttonPushCounter. Whether or not the button has become pressed copy the value of buttonState to lastButtonState ready to check for changes next time through loop()

The result of this is that you have added 1 to buttonPushCounter when the button became pressed and you can use its value to decide what to do. There is a neater way to test its value and decide what to do but get your long winded version working first.

Understood and I am again so sorry for me to be able to do so, it is just really complicated for me. From what I understood I took some parts of the SateChangeDetect or however it is called and found the bit that does all the magic and wizardry (for me I know that you guys probably think that I am the biggest retard that is) and copied it into the code.

#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
const int mypulsebase =  (1000);
const int pulseIncrement = (250);
const int myPulsetime =  mypulsebase;


int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;

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

}

void loop() {
  buttonState = digitalRead(buttonPin);

   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(50);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

 
  if (buttonPushCounter % 0 == 1) {
    m1.attach(9);
    m1.writeMicroseconds(myPulsetime + pulseIncrement);
    delay(1);
  } else {
    m1.writeMicroseconds(1000);
    delay(1);



    if (buttonPushCounter % 0 == 2) {
      m1.attach(9);
      m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement);
      delay(1);
    } else {
      m1.writeMicroseconds(1000);
      delay(1);



      if (buttonPushCounter % 0 == 3) {
        m1.attach(9);
        m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement + pulseIncrement);
        delay(1);
      } else {
        m1.writeMicroseconds(1000);
        delay(1);


        if (buttonPushCounter % 0 == 4) {
          m1.attach(9);
          m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement + pulseIncrement + pulseIncrement);
          delay(1);
        } else {
          m1.writeMicroseconds(1000);
          delay(1);





        }
      }
    }
  }
}

I am really, mega grateful for the help!

Now that you have got the button press counting working does the program do what you want ?

I'm confused.

The % operator returns the remainder when one number is divided by another.

The reference says (my emphasis):

Syntax
remainder = dividend % divisor;
Parameters
remainder : variable. Allowed data types: int, float, double
dividend : variable or constant. Allowed data types: int
divisor : non zero variable or constant. Allowed data types: int

All OP's lines like this:

if (buttonPushCounter % 0 == 4)

.... are logically meaningless and mathematically inadmissible, since x%0 includes division by zero.

OP presumably has some successful results of the button pressing, and I find that in fact the result is always the dividend. In OP's case, that's the buttonPushCounter , which is probably what they want anyway.

Try this:

void setup()
{
  Serial.begin(9600);
  while (!Serial) {}

  int myVar1 = 11;
  int myVar2 = 0;
  int myResult1 = myVar1 % myVar2; //dividing by 0 here
  Serial.println(myVar1);
  Serial.println(myVar2);
  Serial.println(myResult1);       //returns myVar1

  myVar1 = 999;
  myResult1 = myVar1 % myVar2; //dividing by 0 here
  Serial.println(myVar1);
  Serial.println(myVar2);
  Serial.println(myResult1);       //returns myVar1

} //setup

void loop() {} //loop

I get this:

11
0
11
999
0
999

That's very odd, to me. I don't know what the C/C++ standard is supposed to do with division by 0, but it's allowing OP to get the result they expect, since I think they just want the number of button presses.

In that case:

if (buttonPushCounter % 0 == 4)

... which is meaningless but seems to provide the expected result, can just be replaced with:

if (buttonPushCounter == 4)

Can some C/C++ guru describe why x%0 returns x when division by zero is undefined? Is it by design?, or a bug in the compiler?

Can some C/C++ guru describe why x%0 returns x when division by zero is undefined? Is it by design?, or a bug in the compiler?

I am certainly not a guru, but it is the responsibility of the programmer to ensure that division by zero does not occur or if it does to ignore the result.

Lines like

if (buttonPushCounter % 0 == 1)

are just plain wrong, but the OP is just thrashing about as you can see throughout the thread. If he/she ever gets back here I am going to suggest a change that not only corrects the mistake but also considerably reduces the code

I await with bated breathe

UKHeliBob:
it is the responsibility of the programmer to ensure that division by zero does not occur or if it does to ignore the result.

Good point, C/C++ being a "trust the programmer" language.

One of the first, if not the actual first, programs I ever wrote was in Fortran in 1974, to solve a quadratic. There of course one needs to check it is quadratic (non-zero "a") else The Formula can't be used since "a" is in the denominator. So yeah we all learned to trap that by checking "a" and printing "not a quadratic" if it was zero. But if the code didn't include such a check, hours later when you eventually got your output in your pigeon hole, there would have been a run-time "division by real zero" error.

Yes I here, what's the change?

Matyk:
Yes I here, what's the change?

Before we start maing changes, do you understand why your current code does not work to produce the correct values to be writen to m1 based on the value of buttonPushCounter ?t

Have you tried printing the value that you are writing to m1 ?

I did not print but probably would nit have been much use anyway as the motors did not even budge