简体   繁体   English

当您按下按钮时如何将伺服系统转动 90 度并用 180 度和 0 度做同样的事情

[英]How do you turn a servo 90 degrees when you press a button and do the same with 180 degrees and 0 degrees

it maybe looks like a dumb quistion but im still in my learning process and im really trying to look on the internet but sometimes i don't really get it to make it work.它可能看起来像一个愚蠢的问题,但我仍然在我的学习过程中,我真的想在互联网上寻找,但有时我并没有真正让它发挥作用。 my code is as followed:我的代码如下:

 #include <Arduino.h> #include <Servo.h> Servo Myservo; int SERVO_PIN = 8; int ServoButton_S1 = 2; void setup() { Myservo.attach(SERVO_PIN); } void loop() { Myservo.write(0); delay(2000); Myservo.write(90); delay(2000); Myservo.write(180); delay(2000); }

The servo now goes from 0 degrees (wait 2 sec) and than goes to 90 degrees (wait 2 sec) and goes to 180 degrees and so it continious.伺服器现在从 0 度(等待 2 秒)变为 90 度(等待 2 秒)并变为 180 度,因此它是连续的。

The problem is that i want a servo that moves whenever i press the button.问题是我想要一个在我按下按钮时移动的伺服器。 so its like when i press the button the servo goes to 90 degrees but when i press the button again it goes to 180 degrees and when i push it again to 0 degrees and so it has to continue.所以就像当我按下按钮时伺服转到 90 度,但是当我再次按下按钮时,它转到 180 度,当我再次将它推到 0 度时,它必须继续。 so i added the button and i called it:所以我添加了按钮并将其命名为:

int servobutton_s1 = 2;诠释伺服按钮_s1 = 2;

but whenever i added it in the void loop it only works 1 time.但每当我在 void 循环中添加它时,它只能工作 1 次。

i hope someone can help me out.我希望有人能帮助我。 Thanks谢谢

You have to check the button state for every loop iteration.您必须为每个循环迭代检查按钮 state。
Something like:就像是:

#include <Arduino.h>
#include <Servo.h>

Servo Myservo;
int SERVO_PIN = 8;
const int ServoButton_S1 = 2;
int some_counter = 0;

void setup() {
  Myservo.attach(SERVO_PIN);
}

void loop() 
{
  //read the state of the pushbutton value:
  int buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    if (some_counter == 0)
    {
        Myservo.write(0);
        delay(2000);
        ++some_counter;
    }
    else if (some_counter == 1)
    {
        Myservo.write(90);
        delay(2000);
        ++some_counter;
    } 
    else if (some_counter == 2)
    {
        Myservo.write(180);
        delay(2000);
        some_counter = 0;
    }
  }
}

You can also check the Arduino website;您也可以查看 Arduino 网站; there are a lot of tutorial examples.有很多教程示例。

This is just another solution.这只是另一种解决方案。

#include <Servo.h>

#define SERVO_PIN 8
#define ANGLE_BUTTON 2

Servo Myservo;
int counter = 0;
int angles[] = {0, 90, 180};
int angleAmounts = sizeof(angles) / sizeof(angles[0]);

void setup() {
  pinMode(ANGLE_BUTTON, INPUT_PULLUP);
  Myservo.attach(SERVO_PIN);
}

void loop()
{
  int buttonState = digitalRead(ANGLE_BUTTON);

  if (buttonState == LOW) {
    counter = ++counter % angleAmounts;
    Myservo.write(angles[counter]);
    delay(2000);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM