0

ok so yesterday i was in trouble with the buttons, with help from you i got over it and everything worked fine with the 1st speed of the servo, then i added the other 2 speeds and it looks like it all messed up... the buttons don`t respond as they should and the servo moves very weirdly. Here is the code:

const int button1Pin = 2;
const int button2Pin = 3;
const int ledPin1 = 13;
const int ledPin2 = 12;
const int ledPin3 = 11;
#include <Servo.h>
Servo servo1;


int x = 0;
int button1State = 0;
int lastButton1State = 0;
int button2State = 0;
int lastButton2State = 0;



void setup() {
  servo1.attach(9);
  pinMode(button1Pin, INPUT);
  pinMode(button2Pin, INPUT);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
    int position;
    button1State = digitalRead(button1Pin);
    button2State = digitalRead(button2Pin);

      if (button1State != lastButton1State)
     {
        if (button1State == HIGH) 
          {
            x++;
            Serial.println("on1");
            Serial.print("number of button Pushes:  ");
            Serial.println(x);
          }
        else 
          {
            Serial.println("off1");  
          }
     }

    if (button2State != lastButton2State)
      {    
        if (button2State == HIGH) 
          {
            x--;
            Serial.println("on2");
            Serial.print("number of button Pushes:  ");
            Serial.println(x);
          }
        else 
          {
            Serial.println("off2"); 
          }
       }
          delay(1000);

lastButton1State = button1State;
lastButton2State = button2State;

  if(x == 1)
    {
      digitalWrite(ledPin1, HIGH);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, LOW);
        for(position = 0; position < 360; position += 2)
          {
            servo1.write(position);
            delay(20);
          }
    }
   else if (x == 2)
    {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, HIGH);
      digitalWrite(ledPin3, LOW);
        for(position = 0; position <360; position += 5)
          {
            servo1.write(position);
            delay(20);
          }
    }
   else 
    {
      digitalWrite(ledPin1, LOW);
      digitalWrite(ledPin2, LOW);
      digitalWrite(ledPin3, HIGH);
        for(position = 0; position < 360; position += 10)
          {
            servo1.write(position);
            delay(20);
          }
    }
}
1

There may be better ways to mitigate button bounce. Check this link for a discussion on button bounce and example code to work around this problem. In your code, it appears you can not press your buttons faster than once a second.

Determine if the servos you are using are capable of the range you are demanding. Some servos only travel from 0 degrees to 180 degrees. Your code is asking the servo to travel from 0 degrees to 360 degrees. Also, it is unlikely that any normal servo can travel this far. Most servos use a potentiometer as a feed back sensor. And most potentiometers can only be turned to something less than 1 full revolution.

It is likely you have not given the servos enough time to move to the zero degree position at the start of each of your 3 loops. Understand that normal servos can not make complete revolutions. They need to stay within their designed limit of rotation. If you want to start over at 0 degrees you need to allow the servos time to rotate back in reverse to the 0 degree position.

Consider your last loop. You are asking the servo to move from 0 degrees to 360 degrees in 10 degree steps. You are allowing 20ms per step. That means the servo will be demanded to move almost a complete revolution in (((360 / 10) * 0.020 = 0.72 seconds) less than a second. Some but not all servos can move that fast.

  • so i should add more delay? another way to button bounce i don`t really know... can you tell me more? – Radu M Jul 24 '16 at 12:34
  • If the code restricts you pressing the button no faster than once a second and you are fine with that, you can leave that part for later. (Unless there is another problem with that bit of the code.) However you need to tell the servos to go to zero degrees & give the servos some time to do this before each of the 3 loops. Finally, you need to determine how fast the servo can actually respond. I might try increasing the 20ms delay for all loops equally until I noticed a speed difference for the 2 fastest loops. – st2000 Jul 24 '16 at 12:45
  • that is actually a good idea, i just got home and i`m gonna try this out and tell you how it worked out – Radu M Jul 24 '16 at 21:25
  • ok so trying this out still not a solution, i tried removing the servo part of the code and it looks just fine, the buttons work perfectly and the leds light up corespondingly. I think that the problem stands in making the servo rotate endesly. At the start x = 0 so nothing happens, i press the button once so it adds 1 to x, x = 1. After the x = 1 the code enters a loop from which it cant get out, it must move the servo continously so it doesnt allow any other functions to work out, like the buttons. Just an idea i don`t know if this is true. – Radu M Jul 24 '16 at 21:39
  • The stack exchange format is not well suited for code debugging. Unless there is a specific question about the code, it is difficult to adhere to the 1 specific question and 1 concise answer format. That said, there may be problems with you code. For instance, I would be annoyed that nothing happens after pressing a button for at least a second. Second, you never control your "x" counter. It could get very large or it could also go negative. And who knows what may happen if you told the servo library method to set the servo to a negative angle. Did you at least try to add the new delays? – st2000 Jul 24 '16 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.