Go Down

Topic: using pullstime  (Read 1 time) previous topic - next topic

UKHeliBob

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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

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?


Code: [Select]
#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);





        }
      }
    }
  }
}

johnwasser

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. 
Code: [Select]
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:
Code: [Select]
void loop() {
    m1.attach(9);
    m1.writeMicroseconds(1250);
    delay(1);
}



I don't think that will do what you wanted.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Matyk

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);





        }
      }
    }
  }
}

UKHeliBob

#49
Mar 23, 2019, 05:47 pm Last Edit: Mar 23, 2019, 05:47 pm by UKHeliBob
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
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

johnwasser

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.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

Matyk

I really have no more ideas...


Code: [Select]
#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);





        }
      }
    }
  }
}

UKHeliBob

Quote
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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

#53
Mar 23, 2019, 10:58 pm Last Edit: Mar 23, 2019, 11:12 pm by Matyk
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.

Code: [Select]
#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);





        }
      }
    }
  }
}

Matyk

I am really, mega grateful for the help!

UKHeliBob

Now that you have got the button press counting working does the program do what you want ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

neiklot

#56
Mar 24, 2019, 06:56 am Last Edit: Mar 24, 2019, 07:12 am by neiklot
I'm confused.

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

The reference says (my emphasis):

Quote
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:

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
if (buttonPushCounter % 0 == 4)

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

Code: [Select]
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?







UKHeliBob

Quote
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
Code: [Select]
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
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

neiklot

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.




Matyk

Yes I here, what's the change?

Go Up