Arduino Forum

Using Arduino => Programming Questions => Topic started by: Matyk on Mar 20, 2019, 09:34 pm

Title: using pullstime
Post by: Matyk on Mar 20, 2019, 09:34 pm
So I'm working on an Arduino project, specifically a project that would control an ESC. What I want to do is basically to use pulls time or millis to generate a PWM signal, that is firstly neutral for the ESC (1500) and when a button would be pushed it would add 200, so 1700, 1900. I have already made a code that makes a signal depending on a button, however, it is quite inconvenient to have 4 buttons under one hand, if this code would work I would basically just click the one button until the desired speed.

I have come this far, however, I am a complete amateur so I am having a lot of issues... 

My code now:


Title: Re: using pullstime
Post by: johnwasser on Mar 20, 2019, 10:57 pm
Copy and paste your code.  A picture of code will not compile.  Use the </> button (in the control bar above the posting window) to mark your code as code.
Title: Re: using pullstime
Post by: Matyk on Mar 21, 2019, 09:08 am
# include <Servo.h>
Servo m1;
int pushButton1 = A2;
int pushButton2 = A3;
int pullstime = millis;
int pushpulls = 10;

void setup() {
  // put your setup code here, to run once:
šq+<  ě  m1.attach(9);
  delay(1);
  m1.write(40);

}

void loop() {
  if (digitalRead(pushButton1) == HIGH > LOW) { //Opens the door when the 'open' button is pressed
    m1.attach(9);                     //Gives the servo power
    m1.writeMicroseconds(1500);                    //Tells servo to move to 0 degree mark
    delay(1);                       // waits 1000ms for the servo to reach the position
  } else {
    m1.writeMicroseconds(1000);
    delay(1);

    {

      if (digitalRead(pushButton2) == HIGH > LOW) { //Opens the door when the 'open' button is pressed
        m1.attach(9);                     //Gives the servo power
        m1.writeMicroseconds(2000);                  //Tells servo to move to 0 degree mark
        delay(1);                       // waits 1000ms for the servo to reach the position
      } else {
        m1.writeMicroseconds(1000);
        delay(1);

        {

          if (digitalRead(pushpulls) == HIGH > LOW)  {
            m1.attach(9);
            m1.write( pullstime + 0.2)
            or millis == 1500 + 0.2;



              if (digitalRead(pushpulls) == HIGH > LOW)  {
            m1.attach(9);
            m1.write( pullstime - 0.2);
            or millis == 1500 - 0.2;



          }
        }
      }
    }
  }
}
Title: Re: using pullstime
Post by: Robin2 on Mar 21, 2019, 09:18 am
You should always use unsigned long variables with millis().

You can't do fractions of a millisec (such as pullstime + 0.2). If you need a time interval shorter than 1 millisec use micros()

Whatever you think this is intended to do, it is wrong
Code: [Select]
if (digitalRead(pushpulls) == HIGH > LOW)
it should simply be
Code: [Select]
if (digitalRead(pushpulls) == HIGH)

You should attach your servos once in setup() - not repeatedly in loop()

And I'm sure there are other problems.

...R

PS ... When posting code please use the code button </>


Code: [Select]
so your code looks like this
and is easy to copy to a text editor See How to use the Forum (http://forum.arduino.cc/index.php?topic=149014.0)
Title: Re: using pullstime
Post by: Matyk on Mar 21, 2019, 09:35 am
I can't find the two arrows
Title: Re: using pullstime
Post by: neiklot on Mar 21, 2019, 09:59 am
I can't find the two arrows
If you mean the </> icon, you need to be in Reply not Quick Reply, although you can enable the icons in Quick Reply by editin the settings in your Profile.

Title: Re: using pullstime
Post by: neiklot on Mar 21, 2019, 10:12 am
But if all you want to do is have one button increment something each time it's pressed, you could do worse than look at the State Change Detect (https://www.arduino.cc/en/Tutorial/StateChangeDetection) example (also in the IDE at File > Examples...).

(Although the example shows the button wired to 5V with a pull down resistor to ground. Much easier to use the internal pullup, and a button press is then a low.)



Title: Re: using pullstime
Post by: Matyk on Mar 21, 2019, 10:43 am
Code: [Select]
# include <Servo.h>
Servo m1;
int pushButton1 = A2;
int pushButton2 = A3;
int pullstime = millis;
int pushpulls = 10;


void setup() {
  // put your setup code here, to run once:
  m1.attach(9);
  delay(1);
  m1.write(40);

}

void loop() {
  if (digitalRead(pushButton1) == HIGH) { //Opens the door when the 'open' button is pressed
    m1.attach(9);                     //Gives the servo power
    m1.writeMicroseconds(1500);                    //Tells servo to move to 0 degree mark
    delay(1);                       // waits 1000ms for the servo to reach the position
  } else {
    m1.writeMicroseconds(1000);
    delay(1);

    {

      if (digitalRead(pushButton2) == HIGH) { //Opens the door when the 'open' button is pressed
        m1.attach(9);                     //Gives the servo power
        m1.writeMicroseconds(2000);                  //Tells servo to move to 0 degree mark
        delay(1);                       // waits 1000ms for the servo to reach the position
      } else {
        m1.writeMicroseconds(1000);
        delay(1);

        {

          if (digitalRead(pushpulls) == HIGH)  {
            m1.attach(9);
            m1.write( pullstime + 0.2);



            {
              if (digitalRead(pushpulls) == HIGH)  {
                m1.attach(9);
                m1.write( pullstime - 0.2);


                }
              }
            }
          }
        }
      }
    }
  }[code]
[/code]
Title: Re: using pullstime
Post by: Matyk on Mar 21, 2019, 10:52 am
just please, I am the biggest and nubbiest beginner there can be, so could please simplify the expert help. Sorry
Title: Re: using pullstime
Post by: Matyk on Mar 21, 2019, 10:59 am
I have looked at the stage change detect and it looks good, but I don't know how I would tell it to change the pulse time (1500) with each click so that the ESC understands it
Title: Re: using pullstime
Post by: neiklot on Mar 21, 2019, 11:05 am
but I don't know how I would tell it to change the pulse time (1500) with each click
In the example, the counter is incremented (just as an example of what you can do) each time the button is clicked. So in there, you do the same with your value, I forget what it's called, but something like:

Code: [Select]

myPulseTime = myPulseTime + pulseIncrement


.... kind of thing.
Title: Re: using pullstime
Post by: Matyk on Mar 21, 2019, 12:30 pm
Code: [Select]
//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 (or use the built-in LED on most


 

// this constant won't change:
#include <Servo.h>
Servo m1;
const int  buttonPin = 2;  // the pin that the pushbutton is attached to
const int ledPin = 13;       // 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, OUTPUT);
  // 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(50);
  }
  // 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 % 1 == 0) { //Opens the door when the 'open' button is pressed
    m1.attach(9);                     //Gives the se
    m1.writeMicroseconds(1500);                  //Tells servo to move to 0 degree mark
    delay(1);                       // waits 1000ms for the servo to reach the position
  } else {
    m1.writeMicroseconds(1000);
    delay(1);

    if (buttonPushCounter % 2 == 0) { //Opens the door when the 'open' button is pressed
      m1.attach(9);                     //Gives the se
      m1.writeMicroseconds(1700);                  //Tells servo to move to 0 degree mark
      delay(1);                       // waits 1000ms for the servo to reach the position
    } else {
      m1.writeMicroseconds(1000);
      delay(1);

      if (buttonPushCounter % 3 == 0) { //Opens the door when the 'open' button is pressed
        m1.attach(9);                     //Gives the se
        m1.writeMicroseconds(2000);                  //Tells servo to move to 0 degree mark
        delay(1);                       // waits 1000ms for the servo to reach the position
      } else {
        m1.writeMicroseconds(1000);
        delay(1);


      }
    }
  }
}
Title: Re: using pullstime
Post by: Matyk on Mar 22, 2019, 10:51 am
Code: [Select]

 #include <Servo.h>
Servo m1;
int long unsigned
const int buttonPin = A2;
const int ledPin = 9;
const int myPulseTime = millis(1500);
const int pulseIncrement = millis(250);
const int myPulseTime = (myPulseTime + pulseIncrement);


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.write myPulseTime = (myPulseTime + pulseIncrement);
    delay(1);
  } else {
    m1.writeMicroseconds(1000);
    delay(1);



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



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





      }
    }
  }
}



[/quote]
Title: Re: using pullstime
Post by: Matyk on Mar 22, 2019, 10:57 am
go this far but it keeps giving me this message: "two or more data types in declaration of 'buttonPin'"
Title: Re: using pullstime
Post by: UKHeliBob on Mar 22, 2019, 11:19 am
Code: [Select]
int long unsigned
const int buttonPin = A2;

What is the first line above supposed to do ?
Title: Re: using pullstime
Post by: UKHeliBob on Mar 22, 2019, 11:23 am
Code: [Select]
  if (buttonPushCounter % 1 == 0 {
No closing bracket on the if

In any case, the remainder when dividing an integer by 1 will always be zero
Title: Re: using pullstime
Post by: Matyk on Mar 22, 2019, 11:34 am
If I don't say that the int long unsigned gives me this: too many arguments to function 'long unsigned int millis()'
Title: Re: using pullstime
Post by: UKHeliBob on Mar 22, 2019, 11:42 am
Stop thrashing about and think logically.

For instance
Code: [Select]
const int myPulseTime = millis(1500);
millis() returns a value.  What is the 1500 about ?
Title: Re: using pullstime
Post by: Matyk on Mar 22, 2019, 11:44 am
it is 15 milliseconds, if the ESC gets 1500 it does not output any speed, 1500- 2000 is the actual speed
Title: Re: using pullstime
Post by: Matyk on Mar 22, 2019, 11:54 am
and do you know how I could solve the: two or more data types in declaration of 'buttonPin'
Title: Re: using pullstime
Post by: Matyk on Mar 22, 2019, 12:00 pm
// Should it be like this




Code: [Select]
#include <Servo.h>
Servo m1;
int unsigned long
const int buttonPin = A2;
const int ledPin = 9;
const int myPulseTime = writeMicroseconds(1500);
const int pulseIncrement = writeMicroseconds(250);
const int myPulseTime = (myPulseTime + pulseIncrement);


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.write myPulseTime = (myPulseTime + pulseIncrement);
    delay(1);
  } else {
    m1.writeMicroseconds(1000);
    delay(1);



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



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





      }
    }
  }
}

Title: Re: using pullstime
Post by: UKHeliBob on Mar 22, 2019, 12:08 pm
Quote
// Should it be like this
No

buttonPin is presumably the number of a pin.  It will not change in the program and it will not have a value of greater than 255 so a byte will be large enough to hold the value

So it should be
Code: [Select]
const byte buttonPin = A2;
and delete the previous line
Title: Re: using pullstime
Post by: Matyk on Mar 22, 2019, 12:12 pm
did that thanks man, but I still get the "too many arguments to function 'long unsigned int millis()'" if i delete the thing.
Title: Re: using pullstime
Post by: johnwasser on Mar 22, 2019, 12:42 pm
Code: [Select]
const int myPulseTime = writeMicroseconds(1500);
const int pulseIncrement = writeMicroseconds(250);
const int myPulseTime = (myPulseTime + pulseIncrement);


You are trying to declare "myPulseTime" as two different constants.  One of those will have to change names.

writeMicroseconds(1500) is not a constant.  Use 1500 instead.
writeMicroseconds(250) is not a constant.  Use 250 instead.
Title: Re: using pullstime
Post by: Matyk on Mar 22, 2019, 01:19 pm
Thanks so much, everything is working, but the damn thing is now saying: expected ';' before 'myPulsetime'
at the last segment ( the part before the special symbols).
Title: Re: using pullstime
Post by: Matyk on Mar 22, 2019, 01:24 pm
Code: [Select]
#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
const int mypulseTime =  (1500);
const int pulseIncrement = (250);
const int myPulsetime = (mypulseTime + pulseIncrement);


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.write myPulsetime = (myPulseTime + pulseIncrement);
    delay(1);
  } else {
    m1.writeMicroseconds(1000);
    delay(1);



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



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





      }
    }
  }
}


Title: Re: using pullstime
Post by: UKHeliBob on Mar 22, 2019, 02:08 pm
Thanks so much, everything is working, but the damn thing is now saying: expected ';' before 'myPulsetime'
at the last segment ( the part before the special symbols).
It can't be working if you are still getting errors while compiling

Post the code as it is now and the full error message
Title: Re: using pullstime
Post by: Matyk on Mar 22, 2019, 08:58 pm
#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
const int mypulseTime =  (1250);
const int pulseIncrement = (250);
const int myPulsetime = (mypulseTime + pulseIncrement);


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.write myPulsetime = (mypulseTime + pulseIncrement);
    delay(1);
  } else {
    m1.writeMicroseconds(1000);
    delay(1);



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



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





      }
    }
  }
}


Arduino: 1.8.7 (Mac OS X), Board: "Arduino/Genuino Uno"

/Users/matyaskloc/Desktop/ESC-ARDUINO-CONTROL/ANother_one/ANother_one.ino: In function 'void loop()':
ANother_one:29:14: error: expected ';' before 'myPulsetime'
     m1.write myPulsetime = (mypulseTime + pulseIncrement);
              ^
ANother_one:29:58: error: invalid use of non-static member function
     m1.write myPulsetime = (mypulseTime + pulseIncrement);
                                                          ^
ANother_one:39:16: error: expected ';' before 'myPulsetime'
       m1.write myPulsetime = (mypulseTime + pulseIncrement);
                ^
ANother_one:39:60: error: invalid use of non-static member function
       m1.write myPulsetime = (mypulseTime + pulseIncrement);
                                                            ^
ANother_one:49:18: error: expected ';' before 'myPulsetime'
         m1.write myPulsetime = (mypulseTime + pulseIncrement)
                  ^
ANother_one:50:17: error: invalid use of non-static member function
         delay(1);
                 ^
exit status 1
expected ';' before 'myPulsetime'



Title: Re: using pullstime
Post by: UKHeliBob on Mar 22, 2019, 09:44 pm
Code: [Select]
    m1.write myPulsetime = (mypulseTime + pulseIncrement);
What are you trying to write to the servo ?
Title: Re: using pullstime
Post by: Matyk on Mar 22, 2019, 09:54 pm
Servos and ESCs get the same kind of signal, 50Hz signal. The pulse signal varies from 1ms to 2ms, with 1ms represents the motor off completely and 2ms is the maximum speed and anything between is also varying speed.
so technically what I am trying to do is, for the first press of the button the signal would be 1500 (1250+250) so middle speed, second press it would be 1750 (1500+250) 3/4 of the max speed and for the third click, it would be 2000 (1750+250).
Title: Re: using pullstime
Post by: johnwasser on Mar 22, 2019, 10:42 pm
This should do what you want (or close to it) if you add the missing code to increment 'buttonPushCounter' when the button is pushed.
Code: [Select]
#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;

const int PulseTimeBase = 1250;
const int PulseTimeIncrement = 250;
int myPulseTime = PulseTimeBase;


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

  m1.attach(9);
}

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

  switch (buttonPushCounter)
  {
    case 1:  // First push: 1/2 speed
      myPulseTime = PulseTimeBase + PulseTimeIncrement;
      break;

    case 2: // Second push: 3/4 speed
      myPulseTime = PulseTimeBase + PulseTimeIncrement + PulseTimeIncrement;
      break;

    case 3: // Third push: Full speed
      myPulseTime = PulseTimeBase + PulseTimeIncrement + PulseTimeIncrement + PulseTimeIncrement;
      break;

    default:  // 1/4 speed and reset the push counter
      buttonPushCounter = 0;
      myPulseTime = PulseTimeBase;
      break;
  }

  m1.writeMicroseconds(myPulseTime );
}
Title: Re: using pullstime
Post by: UKHeliBob on Mar 22, 2019, 11:18 pm
Servos and ESCs get the same kind of signal, 50Hz signal. The pulse signal varies from 1ms to 2ms, with 1ms represents the motor off completely and 2ms is the maximum speed and anything between is also varying speed.
so technically what I am trying to do is, for the first press of the button the signal would be 1500 (1250+250) so middle speed, second press it would be 1750 (1500+250) 3/4 of the max speed and for the third click, it would be 2000 (1750+250).
I am familiar with and understand all that and in your code you have instances of writing correctly to m1 like this
Code: [Select]
   m1.writeMicroseconds(1000);
So why in other places in the code do you use the write() function like this ?
Code: [Select]
       m1.write myPulsetime = (mypulseTime +  pulseIncrement);
which is so obviously different
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 09:03 am
the  m1.writeMicroseconds(1000); is for when nothing is pressed, so the motor speed would go to zero (1000), and the myPulsetime = (mypulseTime +  pulseIncrement); is for the variation of speed (the thing is that can't have a potentiometer because this project is going underwater, so I just wanted 1 button that would vary through 3 speeds) I put the m1.write because I didn't know what function or whatever I should use.
Title: Re: using pullstime
Post by: UKHeliBob on Mar 23, 2019, 09:21 am
Quote
the  m1.writeMicroseconds(1000); is for when nothing is pressed, so the motor speed would go to zero (1000), and the myPulsetime = (mypulseTime +  pulseIncrement); is for the variation of speed
I get that, but you are not writing myPulsetime to m1

You seem to know how to write 1000 to m1 so have a wild guess as to how you would write myPulsetime to it.
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 09:50 am
I appreciate your faith in me man, but I am quite sceptical I know what I am doing, would it be

m1.writemyPulsetime = (mypulseTime +  pulseIncrement); ?

also, do you know what Johnwasser meant in the first comment of this page? the if you add the missing code to increment 'buttonPushCounter' when the button is pushed.

Title: Re: using pullstime
Post by: UKHeliBob on Mar 23, 2019, 10:00 am
Quote
would it be
No

Take a deep breath and get your brain in gear.

If you want to write 1000 to m1 you use
Code: [Select]
m1.writeMicroseconds(1000);

This uses the Servo library function to write a value to the ESC.  The value in this case is 1000.

You perform a calculation like this
Code: [Select]

myPulsetime = (mypulseTime +  pulseIncrement);
after which myPulsetime has a value.

As an aside, how would you print that value ?

Rather than printing it you want to write the value of myPulsetime to m1.  You know how to write 1000, so substitute myPulsetime for 1000 in the command and you will have written the value of myPulsetime  to m1.

I do not want to simply give you the answer because it is important to think for yourself
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 10:02 am
I'd probably use the serial print, or is it a hit and miss?
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 10:49 am
Right, so I took my code which was edited by Johnwasser, which compiles however I can't figure out if I should connect the button pin A2 to ground or to 5 volts


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

const int PulseTimeBase = 1000;
const int PulseTimeIncrement = 250;
int myPulseTime = PulseTimeBase;


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

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

  m1.attach(9);
}

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

  switch (buttonPushCounter)
  {
    case 1:  // 1/4 speed and reset the push counter
      buttonPushCounter = 0;
      myPulseTime = PulseTimeBase + PulseTimeIncrement;
      break;
   
     case 2:  // First push: 1/2 speed
      myPulseTime = PulseTimeBase + PulseTimeIncrement + PulseTimeIncrement;
      break;

    case 3: // Second push: 3/4 speed
      myPulseTime = PulseTimeBase + PulseTimeIncrement + PulseTimeIncrement + PulseTimeIncrement;
      break;

    case 4: // Third push: Full speed
      myPulseTime = PulseTimeBase + PulseTimeIncrement + PulseTimeIncrement + PulseTimeIncrement + PulseTimeIncrement;
      break;


    default:  // reset  and reset the push counter
      buttonPushCounter = 0;
      myPulseTime = PulseTimeBase;
      break;




  }
  m1.writeMicroseconds(myPulseTime );
}
Title: Re: using pullstime
Post by: UKHeliBob on Mar 23, 2019, 10:51 am
I'd probably use the serial print, or is it a hit and miss?
Yes, you would use Serial.print().  What would the command look like ?
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 10:57 am
I would either do

Serial.print(myPulsetime = (mypulseTime +  pulseIncrement));

or Serial.print(1000)

However, I kind of got over that version so what do you think about this code? what should I connect the A2 to ground/ 5 volts? It wasn't written by me I just edited it a bit.thanks for the help, man I really appreciate it


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

const int PulseTimeBase = 1000;
const int PulseTimeIncrement = 250;
int myPulseTime = PulseTimeBase;


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

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

  m1.attach(9);
}

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

  switch (buttonPushCounter)
  {
    case 1:  // 1/4 speed and reset the push counter
      buttonPushCounter = 0;
      myPulseTime = PulseTimeBase + PulseTimeIncrement;
      break;
   
     case 2:  // First push: 1/2 speed
      myPulseTime = PulseTimeBase + PulseTimeIncrement + PulseTimeIncrement;
      break;

    case 3: // Second push: 3/4 speed
      myPulseTime = PulseTimeBase + PulseTimeIncrement + PulseTimeIncrement + PulseTimeIncrement;
      break;

    case 4: // Third push: Full speed
      myPulseTime = PulseTimeBase + PulseTimeIncrement + PulseTimeIncrement + PulseTimeIncrement + PulseTimeIncrement;
      break;


    default:  // reset  and reset the push counter
      buttonPushCounter = 0;
      myPulseTime = PulseTimeBase;
      break;




  }
  m1.writeMicroseconds(myPulseTime );
}
Title: Re: using pullstime
Post by: UKHeliBob on Mar 23, 2019, 11:17 am
Code: [Select]
Serial.print(myPulsetime = (mypulseTime +  pulseIncrement));
Why make the code more obscure than it needs to be ?

Why not more obviously
Code: [Select]

myPulsetime = myPulsetime +  pulseIncrement;
Serial.print(myPulsetime);


Similarly
Code: [Select]

myPulsetime = myPulsetime +  pulseIncrement;
m1.writeMicroseconds(myPulsetime);
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 11:35 am
And how about this?




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 % 3 == 0) {
      m1.attach(9);
      m1.writeMicroseconds(myPulsetime + pulseIncrement + pulseIncrement);
      delay(1);
    } else {
      m1.writeMicroseconds(1000);
      delay(1);



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


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





        }
      }
    }
  }
}




Title: Re: using pullstime
Post by: UKHeliBob on Mar 23, 2019, 12:13 pm
Do you think that it would be a good idea to change the value of buttonPushCounter somewhere in the program ?
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 12:19 pm
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);





        }
      }
    }
  }
}


Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 12:27 pm
or maybe this


int buttonPushCounter = buttonstate;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;
Title: Re: using pullstime
Post by: UKHeliBob on Mar 23, 2019, 03:23 pm
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.
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 04:07 pm
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);





        }
      }
    }
  }
}
Title: Re: using pullstime
Post by: johnwasser on Mar 23, 2019, 04:42 pm
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.
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 04:46 pm
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);





        }
      }
    }
  }
}
Title: Re: using pullstime
Post by: UKHeliBob on Mar 23, 2019, 05:47 pm
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
Title: Re: using pullstime
Post by: johnwasser on Mar 23, 2019, 05:50 pm
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.
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 08:30 pm
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);





        }
      }
    }
  }
}
Title: Re: using pullstime
Post by: UKHeliBob on Mar 23, 2019, 09:34 pm
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.
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 10:58 pm
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);





        }
      }
    }
  }
}
Title: Re: using pullstime
Post by: Matyk on Mar 23, 2019, 11:05 pm
I am really, mega grateful for the help!
Title: Re: using pullstime
Post by: UKHeliBob on Mar 23, 2019, 11:30 pm
Now that you have got the button press counting working does the program do what you want ?
Title: Re: using pullstime
Post by: neiklot on Mar 24, 2019, 06:56 am
I'm confused.

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

The reference  (https://www.arduino.cc/reference/en/language/structure/arithmetic-operators/remainder/)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?






Title: Re: using pullstime
Post by: UKHeliBob on Mar 24, 2019, 08:10 am
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
Title: Re: using pullstime
Post by: neiklot on Mar 24, 2019, 08:21 am
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.



Title: Re: using pullstime
Post by: Matyk on Mar 24, 2019, 09:16 am
Yes I here, what's the change?
Title: Re: using pullstime
Post by: UKHeliBob on Mar 24, 2019, 09:20 am
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 ?
Title: Re: using pullstime
Post by: Matyk on Mar 24, 2019, 09:22 am
I did not print but probably would nit have been much use anyway as the motors did not even budge
Title: Re: using pullstime
Post by: UKHeliBob on Mar 24, 2019, 09:31 am
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
Title: Re: using pullstime
Post by: Matyk on Mar 24, 2019, 09:34 am
No, I am interested(this project has been going on for a long while)but it is just hard for me.


Title: Re: using pullstime
Post by: Matyk on Mar 24, 2019, 09:40 am
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);





        }
      }
    }
  }
}
Title: Re: using pullstime
Post by: UKHeliBob on Mar 24, 2019, 09:41 am
Quote
post the output here
Title: Re: using pullstime
Post by: Matyk on Mar 24, 2019, 09:45 am
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
Title: Re: using pullstime
Post by: UKHeliBob on Mar 24, 2019, 09:55 am
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
Title: Re: using pullstime
Post by: Matyk on Mar 24, 2019, 10:07 am
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);
      }
  }
}
Title: Re: using pullstime
Post by: neiklot on Mar 24, 2019, 10:12 am
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.

Title: Re: using pullstime
Post by: Matyk on Mar 24, 2019, 10:22 am
everything is working what now?
Title: Re: using pullstime
Post by: neiklot on Mar 24, 2019, 10:26 am
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.



Title: Re: using pullstime
Post by: IanCrowe on Mar 24, 2019, 10:28 am
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


Title: Re: using pullstime
Post by: neiklot on Mar 24, 2019, 10:39 am
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.))



Title: Re: using pullstime
Post by: UKHeliBob on Mar 24, 2019, 11:16 am
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
Title: Re: using pullstime
Post by: Matyk on Mar 24, 2019, 10:12 pm
I tried both methods both compile, serial monitor looks fine but the still don't do anything. Is it still in the program?


Code: [Select]
#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
int writeValues[5] = {1000, 1250,1500,1750,2000};
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 == LOW) {
      // 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;

  {
  if (buttonState == LOW);
  m1.writeMicroseconds(writeValues[5]);
  }
}





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);
  }
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;
 
  {
  if (buttonState == HIGH);
  byte index = buttonPushCounter % 5;
  m1.writeMicroseconds(1000 + (index * 250));

    }
  }







Title: Re: using pullstime
Post by: UKHeliBob on Mar 24, 2019, 11:24 pm
Code: [Select]
    if (buttonState == LOW);
    m1.writeMicroseconds(writeValues[5]);

Three things wrong here

1 - the if is terminated by a semicolon.  The semicolon is the only code whose execution depends on whether the if test returns true.  Remove it

2 - the array entry 5 does not exist.  The array values are numbered 0 to 4 as I warned you in an earlier post

3 - you are meant to use the value of buttonPushCounter % 5 as the array index to select the correct value for the value to write to m1.
Title: Re: using pullstime
Post by: Matyk on Mar 25, 2019, 10:49 am
oh wait so would I do it like this, for every value...

{
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[0]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[1]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[2]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[3]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[4]);
  }
}
Title: Re: using pullstime
Post by: Matyk on Mar 25, 2019, 10:56 am
sorry wrong thing...


Code: [Select]
{
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[0]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[1]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[2]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[3]);
  }

  {
  if (buttonState == LOW)
  m1.writeMicroseconds(writeValues[4]);
  }
}
Title: Re: using pullstime
Post by: UKHeliBob on Mar 25, 2019, 11:03 am
NO, NO, NO NO !

You have an array of 5 values numbered from 0 to 4
Code: [Select]

int writeValues[5] = {1000, 1250,1500,1750,2000};


You have a number between 0 and 4 derived like this
Code: [Select]
buttonPushCounter % 5)

You do not need to test its value 5 times.  Instead you use its value to select the associated value from the array like this
Code: [Select]
m1.writeMicroseconds(writeValues[buttonPushCounter % 5)]);
Title: Re: using pullstime
Post by: Matyk on Mar 25, 2019, 11:11 am
Code: [Select]
#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
int writeValues[5] = {1000, 1250,1500,1750,2000};
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 == LOW) {
      // 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;

  {
  m1.writeMicroseconds(writeValues[buttonPushCounter % 5]);
  }
}
Title: Re: using pullstime
Post by: UKHeliBob on Mar 25, 2019, 11:17 am
Does it work ?
Title: Re: using pullstime
Post by: Matyk on Mar 25, 2019, 11:20 am
I can't test it right now...
Title: Re: using pullstime
Post by: Matyk on Mar 25, 2019, 05:08 pm
Is it the f***in code again?

Code: [Select]
#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
int writeValues[5] = {1000, 1250,1500,1750,2000};
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 == LOW) {
      // 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;

  {
  m1.writeMicroseconds(writeValues[buttonPushCounter % 5]);
  }
}
Title: Re: using pullstime
Post by: UKHeliBob on Mar 25, 2019, 06:00 pm
Is what in the code ?

You forgot to do the m1.attach() in setup()
Title: Re: using pullstime
Post by: Matyk on Mar 25, 2019, 08:32 pm
oh the message didn't send itself, well basically I now can't upload the code to the Arduino.

Code: [Select]
Sketch uses 3268 bytes (10%) of program storage space. Maximum is 32256 bytes.
Global variables use 273 bytes (13%) of dynamic memory, leaving 1775 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00

Title: Re: using pullstime
Post by: UKHeliBob on Mar 25, 2019, 10:39 pm
Quote
I now can't upload the code to the Arduino.
That is not a problem with the code.  Close down the IDE, unplug the Arduino then restart the IDE and plug the Arduino back in and check that the COM port that it is using is correct.
Title: Re: using pullstime
Post by: Matyk on Mar 26, 2019, 07:26 pm
Quite a problem here, I did what you said and it didn't work, then I completely removed the Arduino software and installed the Arduino software again; it is still not working or I can't upload it. I think that something happened to the Arduino itself even though I didn't do anything abusive. What do you think?
PS: I spent like 4 hours on this, trying programmers and other things.


Code: [Select]
Sketch uses 3268 bytes (10%) of program storage space. Maximum is 32256 bytes.
Global variables use 273 bytes (13%) of dynamic memory, leaving 1775 bytes for local variables. Maximum is 2048 bytes.
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x00
Title: Re: using pullstime
Post by: UKHeliBob on Mar 27, 2019, 12:00 am
It is possible that you have a dead board but that would not be caused by the program

Personally I would try another Arduino, another USB cable, another USB port, another PC or all four.  I assume that you have rebooted the PC.

Do any COM ports show up in Device Manager (assuming that you are using Windows) ?  Do the COM ports change in Device Manager when you plug or unplug the Arduino ?
Title: Re: using pullstime
Post by: Matyk on Mar 29, 2019, 07:02 pm
Sorry for the long hile, I had to get a hold of a new computer...
Title: Re: using pullstime
Post by: Matyk on Mar 29, 2019, 07:21 pm
However I think that the only thing I do not have a spare of is the problem, UKHeliBob I know it is kind of a stupid request from me, but do you think that you could finish the code so that it works? I know that I sound like the most entitled idiot, but it will be a while before I´ll get a new Arduino and then again you saw how much I struggled, which I kind of sensed that I was really getting on your nerve. If you don´t finish the code, I get it since it is a really entitled request from me and I hope that you don´t imagine me as a douche or something. It is that it would just take less time for me to finally get to the whole project testing, but anyway thanks!
Title: Re: using pullstime
Post by: UKHeliBob on Mar 30, 2019, 08:00 am
Your code looks OK.

I have tidied it up the layout a little and added another debug print to show the value being written to m1 but the logic looks OK
Code: [Select]

#include <Servo.h>
Servo m1;
const byte buttonPin = A2;
const int ledPin = 9;
int writeValues[5] = {1000, 1250, 1500, 1750, 2000};
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 == LOW)
    {
      // 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);
      Serial.print("writing ");
      Serial.println(writeValues[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;
  m1.writeMicroseconds(writeValues[buttonPushCounter % 5]);
}
Title: Re: using pullstime
Post by: Matyk on Mar 30, 2019, 09:10 am
thanks so much dude, really. Have a great day! :-)
Title: Re: using pullstime
Post by: Matyk on Mar 30, 2019, 11:39 am
for some reason, your code can´t compile it always says


Code: [Select]
error: scalar object 'writeValues' requires one element in initializer

 int writeValues = {1000, 1250, 1500, 1750, 2000};

                                                ^

exit status 1
scalar object 'writeValues' requires one element in initializer



Do  you know what to do, I don´t understand it
Title: Re: using pullstime
Post by: Matyk on Mar 30, 2019, 11:45 am
For some reason when I do this :

int writeValues[5]  = {1000, 1250, 1500, 1750, 2000};

it compiles fine, is it because it didn´t have an array value or something?
Title: Re: using pullstime
Post by: Matyk on Mar 30, 2019, 11:51 am
It still doesn´t upload, but still a huge thanks! :-)
Title: Re: using pullstime
Post by: UKHeliBob on Mar 30, 2019, 11:57 am
For some reason when I do this :

int writeValues[5]  = {1000, 1250, 1500, 1750, 2000};

it compiles fine, is it because it didn´t have an array value or something?
The code that I posted had the [5] for the array in it.  See post #91.  I don't know what you did to it to remove it but I hope that it did not involve typing the code into the IDE.

Note that the code has nothing to do with whether it uploads to the Arduino or not.  One thing that you could do it is to uninstall the IDE and install it again.