Arduino - Botão - Servo Motor

Vamos aprender:

Esse processo se repete.

O tutorial inclui duas partes principais:

Sobre Servo Motor e Botão

Se você não conhece o servo motor e o botão (pinagem, como funcionam, como programar...), aprenda sobre eles nos seguintes tutoriais:

Diagrama de Ligação

Diagrama de Ligação Arduino Botão Servo Motor

This image is created using Fritzing. Click to enlarge image

Código Arduino - Botão Controla Servo Motor Sem Debounce

/* * Este código de Arduino foi desenvolvido por pt.newbiely.com * Este código de Arduino é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.newbiely.com/tutorials/arduino/arduino-button-servo-motor */ #include <Servo.h> // constants won't change const int BUTTON_PIN = 7; // Arduino pin connected to button's pin const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin Servo servo; // create servo object to control a servo // variables will change: int angle = 0; // the current angle of servo motor int lastButtonState; // the previous state of button int currentButtonState; // the current state of button void setup() { Serial.begin(9600); // initialize serial pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); currentButtonState = digitalRead(BUTTON_PIN); } void loop() { lastButtonState = currentButtonState; // save the last state currentButtonState = digitalRead(BUTTON_PIN); // read new state if(lastButtonState == HIGH && currentButtonState == LOW) { Serial.println("The button is pressed"); // change angle of servo motor if(angle == 0) angle = 90; else if(angle == 90) angle = 0; // control servo motor arccoding to the angle servo.write(angle); } }

Passos Rápidos

  • Conecte o Arduino ao PC via cabo USB
  • Abra a Arduino IDE, selecione a placa e a porta corretas
  • Copie o código acima e abra com a Arduino IDE
  • Clique no botão Upload na Arduino IDE para carregar o código no Arduino
  • Pressione o botão várias vezes.
  • Veja a mudança do servo motor

※ Nota:

Na prática, o código acima às vezes não funciona corretamente. Para que ele sempre funcione corretamente, precisamos fazer o debounce do botão. Fazer o debounce do botão não é fácil para iniciantes. Felizmente, graças à biblioteca ezButton, podemos fazer isso facilmente.

Código Arduino - Botão Controla Servo Motor Com Debounce

Por que precisamos de debounce? ⇒ veja o tutorial Arduino - Debounce de Botão

/* * Este código de Arduino foi desenvolvido por pt.newbiely.com * Este código de Arduino é disponibilizado ao público sem nenhuma restrição. * Para tutoriais completos e diagramas de ligação, visite: * https://pt.newbiely.com/tutorials/arduino/arduino-button-servo-motor */ #include <Servo.h> #include <ezButton.h> // constants won't change const int BUTTON_PIN = 7; // Arduino pin connected to button's pin const int SERVO_PIN = 9; // Arduino pin connected to servo motor's pin ezButton button(BUTTON_PIN); // create ezButton object that attach to pin 7; Servo servo; // create servo object to control a servo // variables will change: int angle = 0; // the current angle of servo motor void setup() { Serial.begin(9600); // initialize serial button.setDebounceTime(50); // set debounce time to 50 milliseconds servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(angle); } void loop() { button.loop(); // MUST call the loop() function first if(button.isPressed()) { Serial.println("The button is pressed"); // change angle of servo motor if(angle == 0) angle = 90; else if(angle == 90) angle = 0; // control servo motor arccoding to the angle servo.write(angle); } }

Passos Rápidos

  • Instale a biblioteca ezButton. Veja Como Fazer
  • Copie o código acima e abra com a Arduino IDE
  • Clique no botão Upload na Arduino IDE para carregar o código no Arduino
  • Pressione o botão várias vezes
  • Veja a mudança do servo motor

Vídeo

※ NOSSAS MENSAGENS

  • Fique à vontade para compartilhar o link deste tutorial. Porém, por favor, não use nosso conteúdo em outros sites. Investimos muito esforço e tempo para criar este conteúdo, por favor respeite nosso trabalho!