Go Down

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

Matyk

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:


johnwasser

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

Matyk

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



          }
        }
      }
    }
  }
}

Robin2

#3
Mar 21, 2019, 09:18 am Last Edit: Mar 21, 2019, 10:17 am by Robin2
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
Two or three hours spent thinking and reading documentation solves most programming problems.

Matyk

I can't find the two arrows

neiklot

#5
Mar 21, 2019, 09:59 am Last Edit: Mar 21, 2019, 10:00 am by neiklot
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.


neiklot

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




Matyk

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]

Matyk

just please, I am the biggest and nubbiest beginner there can be, so could please simplify the expert help. Sorry

Matyk

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

neiklot

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.

Matyk

#11
Mar 21, 2019, 12:30 pm Last Edit: Mar 21, 2019, 12:30 pm by Matyk
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);


      }
    }
  }
}

Matyk

#12
Mar 22, 2019, 10:51 am Last Edit: Mar 22, 2019, 10:59 am by Matyk
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]

Matyk

go this far but it keeps giving me this message: "two or more data types in declaration of 'buttonPin'"

UKHeliBob

Code: [Select]
int long unsigned
const int buttonPin = A2;

What is the first line above supposed to do ?
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