quarta-feira, 17 de setembro de 2025

 Movimento de servo motores utilizando o arduino

O controle do ângulo de cada um é feito através de um potênciometro que na sua faixa de valores de resistência/tensão no pino de entrada, é convertido a um ângulo de 0 a 180 Graus. Aqui está o circuito e a programação utilizada. A montagem e a simulação foi feita no site: www.tinkercad.com, gratuíto. 



Programação proposta:

#include <Servo.h>

Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo
Servo myservo3;  // create servo object to control a servo
Servo myservo4;  // create servo object to control a servo

int potpin1 = 0;  // analog pin used to connect the potentiometer
int val1;    // variable to read the value from the analog pin

int potpin2 = 1;  // analog pin used to connect the potentiometer
int val2;    // variable to read the value from the analog pin

int potpin3 = 2;  // analog pin used to connect the potentiometer
int val3;    // variable to read the value from the analog pin

int potpin4 = 3;  // analog pin used to connect the potentiometer
int val4;    // variable to read the value from the analog pin

void setup() {
  myservo1.attach(3);  // attaches the servo on pin 9 to the servo object
  myservo2.attach(4);  // attaches the servo on pin 9 to the servo object
  myservo3.attach(5);  // attaches the servo on pin 9 to the servo object
  myservo4.attach(6);  // attaches the servo on pin 9 to the servo object
  
  pinMode(8, INPUT);
  pinMode(potpin1, INPUT);
  pinMode(potpin2, INPUT);
  pinMode(potpin3, INPUT);
  pinMode(potpin4, INPUT);
}

void loop() {
  
  int botao = digitalRead(8);
  
  if(botao==1)
  {
  val1 = analogRead(potpin1);            // reads the value of the potentiometer (value between 0 and 1023)
  val1 = map(val1, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo1.write(val1);                  // sets the servo position according to the scaled value
  
  
  val2 = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023)
  val2 = map(val2, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo2.write(val2);                  // sets the servo position according to the scaled value
  
  val3 = analogRead(potpin3);            // reads the value of the potentiometer (value between 0 and 1023)
  val3 = map(val3, 0, 1023, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
  myservo3.write(val3);                  // sets the servo position according to the scaled value
  
  val4 = analogRead(potpin4);            // reads the value of the potentiometer (value between 0 and 1023)
  val4 = map(val4, 0, 1023, 89, 180);     // scale it to use it with the servo (value between 0 and 90)
  myservo4.write(val4);                  // sets the servo position according to the scaled value
  
 
  delay(15);                           // waits for the servo to get there
  }

}



Nenhum comentário:

Postar um comentário