Go Down

Topic: using pullstime  (Read 2062 times) previous topic - next topic

johnwasser

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 );
}
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

UKHeliBob

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

Matyk

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.

UKHeliBob

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


UKHeliBob

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
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'd probably use the serial print, or is it a hit and miss?

Matyk

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

UKHeliBob

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

UKHeliBob

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

Matyk

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





        }
      }
    }
  }
}





UKHeliBob

Do you think that it would be a good idea to change the value of buttonPushCounter somewhere in the program ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Matyk

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





        }
      }
    }
  }
}



Matyk

or maybe this


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

Go Up