Go Down

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

UKHeliBob

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

Matyk

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

UKHeliBob

#62
Mar 24, 2019, 09:31 am Last Edit: Mar 24, 2019, 09:32 am by UKHeliBob
OK.  Let's plunge on as you don't seem interested as to why it does not work

When you do
Code: [Select]

variableA = variableB % x

then variableA is assigned the remainder of variableB divided by the value of x

Change your code to add 2 extra print lines like this
Code: [Select]

      Serial.println(buttonPushCounter);
      Serial.print("buttonPushCounter % 5 = ");
      Serial.println(buttonPushCounter % 5);

Upload the code and press the button several times and post the output here
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

No, I am interested(this project has been going on for a long while)but it is just hard for me.



Matyk

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(buttonPushCounter);
      Serial.print("buttonPushCounter % 5 = ");
      Serial.println(buttonPushCounter % 5);
    } 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 == 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

Quote
post the output here
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

Code: [Select]
1
1
buttonPushCounter % 5 = 1
off
2
buttonPushCounter % 5 = 2
off
3
buttonPushCounter % 5 = 3
off
4
buttonPushCounter % 5 = 4
off
5
buttonPushCounter % 5 = 0
off
6
buttonPushCounter % 5 = 1
off
7
buttonPushCounter % 5 = 2
off
8
buttonPushCounter % 5 = 3
off
9
buttonPushCounter % 5 = 4
off
10
buttonPushCounter % 5 = 0

UKHeliBob

As you can see buttonPushCounter % 5 produces a number between 0 and 5

So you could use this value to set what you write to m1 like this
Code: [Select]

byte index = buttonPushCounter % 5;
if (index == 0)
  {
     m1.writeMicroseconds(something);
  }
else if (index == 1)
  {
     m1.writeMicroseconds(somethingElse);
  }
else if (index == 2)
  {
     m1.writeMicroseconds(anotherValue);
  }


Try that and get it working then I will suggest an even better way to do it
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

Compiles, no error


Code: [Select]
#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;

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(buttonPushCounter);
      Serial.print("buttonPushCounter % 5 = ");
      Serial.println(buttonPushCounter % 5);
    } 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);
  }

  lastButtonState = buttonState;

  {
    byte index = buttonPushCounter % 5;
    if (index == 0)
    {
      m1.writeMicroseconds(1000);
    }
    else if (index == 1)
    {
      m1.writeMicroseconds(1250);
    }
    else if (index == 2)
    {
      m1.writeMicroseconds(1500);
    }
    else if (index == 3)
    {
      m1.writeMicroseconds(1750);
    }
      else if (index == 4)
      {
        m1.writeMicroseconds(2000);
      }
  }
}

neiklot

#69
Mar 24, 2019, 10:12 am Last Edit: Mar 24, 2019, 10:13 am by neiklot
Code: [Select]
pinMode(buttonPin, INPUT_PULLUP);

Code: [Select]

buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) { // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<


That code's actually counting releases. With pullups enabled, pressed is low, yet you're looking for a high after a change, which is a release not a press.


Matyk

everything is working what now?

neiklot

Well every press has a release, so if you count releases that's numerically the same as counting presses, but it's not strictly a buttonPushCounter, it's a buttonReleaseCounter.




IanCrowe

This whole set of nested ifs:-

Code: [Select]

{
    byte index = buttonPushCounter % 5;
    if (index == 0)
    {
      m1.writeMicroseconds(1000);
    }
    else if (index == 1)
    {
      m1.writeMicroseconds(1250);
    }
    else if (index == 2)
    {
      m1.writeMicroseconds(1500);
    }
    else if (index == 3)
    {
      m1.writeMicroseconds(1750);
    }
      else if (index == 4)
      {
        m1.writeMicroseconds(2000);
      }
  }


Can be replaced with two lines of code:-

Code: [Select]

byte index = buttonPushCounter % 5;
m1.writeMicroseconds(1000 + (index * 250);


To explain it fully buttonPushCounter % 5 returns either 0, 1, 2, 3 or 4 to index therefore you have the following microsecond values written to m1:-

If index = 0, index * 250 is 0 so 1000 is written to m1.
If index = 1, index * 250 is 250 so 1250 is written to m1.
If index = 2, index * 250 is 500 so 1500 is written to m1.
If index = 3, index * 250 is 750 so 1750 is written to m1.
If index = 4, index * 250 is 100 so 2000 is written to m1.

Much simpler than a whole pile of nested ifs.

Ian



neiklot

#73
Mar 24, 2019, 10:39 am Last Edit: Mar 24, 2019, 10:40 am by neiklot
Much simpler than a whole pile of nested ifs.
Perhaps that's what this was going to be:

Try that and get it working then I will suggest an even better way to do it
@IanCrowe, I think UKHeliBob has a plan here: he's walked this OP through a process for a couple of days now, and with all due respect to the OP, he (the OP) is struggling, and perhaps it would be better to leave UKHB to it....

(I've been here since #5, and just chipped in with parts where I saw errors, like the x%0 thing and the counting of releases not presses. Your suggestion is of course a very valid one, I just think it's best to let UKHB go with this... (imo, of course.))




UKHeliBob

everything is working what now?
Now we move on to reducing your code by using the number in the index variable to do some "magic"

As you have seen you can use it to calculate the value to write to m1 and that is one option that I had considered suggesting, and it works but the relationship between the number of button presses and the value written is fixed which may not always be what is wanted and it required the Arduino to do the caculation each time through loop()

I was going to suggest using an array of values to be written to m1 based on the value of index.

For the sake of Matyk, an array is a way of holding a list of several values of the same type and accessing the one you want using its place in the list.  The index variable gives us a convenient way to access the required value.

So, how does it work ?
Declare a global array of ints like this
Code: [Select]

int writeValues[5] = {123, 234, 345, 456, 567};  //fill in the values that you really want

NOTE: This array has 5 spaces numbered 0 to 4 NOT 1 to 5

Then, once you have the value of index you can do
m1.writeMicroseconds(writeValues[index]);

This uses index to look up the value to write.  Because you have control of the values in the array the value written for each button press is fully under your control and does not need to be linear.

Give both methods a try
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up