Go Down

Topic: push button to servo and milles() (Read 127 times) previous topic - next topic

diogoesteves

Dec 15, 2017, 04:41 pm Last Edit: Dec 16, 2017, 12:56 pm by diogoesteves
Helo i am realy new to Arduino, and i am trying to set a servo motor to move from 90º to 0º in 2.5 Sec. than 0º to 180 in 5 sec. and than from 180º to 90º, all of this in 10 sec. total and after i push a button.
But i want to use the milles().
can any one help me with the code?

i am at this point:

Code: [Select]

#include <Servo.h>
#define buttonPin 2
#define servoPin 3

Servo servo;   

void setup() {
  Serial.begin(9600);
  servo.attach(servoPin);
 pinMode(buttonPin,INPUT_PULLUP);

}

void loop() {
 
  int posi = 90;   
  int buttonState;
  buttonState = digitalRead(buttonPin);
   if (buttonState == LOW) {
    servo.write (posi);
 
   for (posi = 90; posi >= 0; posi -= 1) {
    servo.write(posi);             
    delay(27);
    Serial.println(posi);   
 

    for (posi = 0; posi <= 90; posi += 1) {
    servo.write(posi);             
    delay(27);
    Serial.println(posi);   
                   
  }
 
  for (posi = 90; posi <= 180; posi += 1) {
    servo.write(posi);             
    delay(27);
    Serial.println(posi);
  }

    for (posi = 180; posi >= 90; posi -= 1) {
    servo.write(posi);             
    delay(27);
    Serial.println(posi);
    }
                     
  }
   
}   



septillion

Please read How to use the forum and edit your post accordingly. Especially the part about code tags and question formulation. Because What exactly is the problem?
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

aarg

The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select "modify" from the pull down menu labelled, "More", at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Unless the sketch is too large, it's better if you post your code, rather than attach it. When it's attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the "Temp" folder and manually remove it. It's much easier to just view the code in your post.

Many questions can be answered by reading the documentation which is provided with the IDE, available under the help tab, or online here.

There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to provide the entire code, not just a snippet. Use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Use blank lines sparingly, no more than one at a time. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

Robin2

Have a look at the demo Several things at a time. It moves a servos slowly and you could adjust the timing to meet your needs. It also has a switch function.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

UKHeliBob

Code: [Select]

 unsigned long previousMillis = 0;

Isn't previousMillis meant to hold the millis() value at the start of the period being timed ?

Otherwise
Code: [Select]
   if (currentMillis - previousMillis >= interval)

Is going to be true for 49 days once 5 seconds has elapsed.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

diogoesteves

I am sorry i am realy new toall off this, but thx alot to every body for the help.


Code: [Select]

 unsigned long previousMillis = 0;

Isn't previousMillis meant to hold the millis() value at the start of the period being timed ?

Otherwise
Code: [Select]
   if (currentMillis - previousMillis >= interval)

Is going to be true for 49 days once 5 seconds has elapsed.
Yeah i want to use that fuction but i can´t do that =(.

UKHeliBob

Quote
i want to use that fuction but i can´t do that
Why not ?
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