繁体   English   中英

Arduino伺服功能

[英]Arduino Servo Function

昨天我在和Arduino鬼混。 它昨天到达邮件中,我对Arduino的使用经验为零。 这是我的代码,如何将其放入函数中? 我想缩短代码,因为这里似乎很重复。 我了解函数的工作原理等,只是不知道正确的语法等。 那么,如何将其放入函数中呢?

码:

/* 

What does this program do:
Allows for a user to control a robotic arm
The current version uses multiple potentiometers to control the hand.

*/

#include <Servo.h> // Library that allows us to use inputs to a 179 degree servo


Servo indexF;  // create servo object to control a servo
Servo middleF;
Servo ringF;
Servo pinkyF;
Servo thumb;
Servo wrist;
Servo forearmBicep;




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 v   bmvbbalue from the analog pin

int potpin4 = 3;
int val4;     //Next value assigned to the pinky

int potpin5 = 4;
int val5;



void setup() 
{ 
  indexF.attach(11);  // attaches the servo on pin 9 to the servo object 
  middleF.attach(10); // Same thing ^^
  ringF.attach(9);
  pinkyF.attach(6);
  thumb.attach(5);
} 

 // first define all of ur pots and servos




void loop() 
{ 
  val1 = analogRead(potpin1);            // reads the value of the potentiometer (value         between 0 and 1023) 
  val1 = map(val1, 0, 1023, 0, 179);     // scale it to use it with the servo (value     between 0 and 180) 
  indexF.write(val1);                  // sets the servo position according to the scaled     value 


  val2 = analogRead(potpin2);
  val2 = map(val2, 0, 1023, 0, 179);
  middleF.write(val2);


  val3 = analogRead(potpin3);
  val3 = map(val3, 0, 1023, 0, 179);
  ringF.write(val3);


  val4 = analogRead(potpin4);
  val4 = map(val4, 0, 1023, 0, 179);
  pinkyF.write(val4);


  val5 = analogRead(potpin5);
  val5 = map(val5, 0, 1023, 0, 179);
  thumb.write(val5);

  delay(27);  // waits for the servo to get there  

} 

先谢谢您的帮助。 很抱歉成为这个新手:)

// you will pass two arguments the pot you want to read and the servo you want to write to
void moveServo( potPin, servo ) 
{
    val = analogRead(potPin);
    val = map(val, 0, 1023, 0, 179);
    servo.write(val);
} 

// calling the function

moveServo( potpin1, indexF );

希望我的评论能帮助您了解更改。

我想向您介绍两个不错的规则:1.避免使用幻数-减少代码的可读性和更改难度,因为有时您必须记住许多使用值的地方2.类和结构应该由指针(如果不应该更改,则为const指针)或引用(更好,例如,因为您不必处理NULL情况)传递。

/* 

What does this program do:
Allows for a user to control a robotic arm
The current version uses multiple potentiometers to control the hand.

*/

#include <Servo.h> // Library that allows us to use inputs to a 179 degree servo

//Avoid magic numbers
#define MIN_POTENTIOMETER_VAL 0
#define MAX_POTENTIOMETER_VAL 1023

#define MIN_ANGLE 0
#define MAX_ANGLE 179

#define DELAY 27

Servo indexF;  // create servo object to control a servo
Servo middleF;
Servo ringF;
Servo pinkyF;
Servo thumb;
Servo wrist;
Servo forearmBicep;

// first define all of ur pots and servos
const int potpin1 = 0;  // analog pin used to connect the potentiometer
const int potpin2 = 1;  // analog pin used to connect the potentiometer
const int potpin3 = 2;  // analog pin used to connect the potentiometer
const int potpin4 = 3;
const int potpin5 = 4;

//Servo pins
const int indexFPin = 11;
const int middleFPin = 10;
const int ringFPin = 9;
const int pinkyFPin = 6;
const int thumbFPin = 5;


//Function for moving your servo
//servo is a pointer to the Servo struct. It's cheaper and faster to pass pointers, be be aware that you modify original object by that. You can avoid it by adding "const"
//pin is potpin you want to use, simple variables like int we pass by value, it means they are copied and you can do whatever you want to them, without losing original data
void moveServo(Servo* servo, int pin)
{
  int potentiometerVal = analogRead(pin);
  int mapped = map(potentiometerVal, MIN_POTENTIOMETER_VAL, MAX_POTENTIOMETER_VAL, MIN_ANGLE, MAX_ANGLE);
  if (NULL != servo)
  {
    servo->write(mapped);
  }
}

void setup() 
{ 
  indexF.attach(indexFPin);  // attaches the servo on pin 9 to the servo object 
  middleF.attach(middleFPin); // Same thing ^^
  ringF.attach(ringFPin);
  pinkyF.attach(pinkyFPin);
  thumb.attach(thumbFPin);
} 

void loop() 
{ 
   //Note "&" at the beginning of argument - it means that we pass an address, not the data itself
  moveServo(&indexF, potpin1);
  moveServo(&middleF, potpin2);
  moveServo(&ringF, potpin3);
  moveServo(&pinkyF, potpin4);
  moveServo(&thumb, potpin5);

  delay(DELAY);  // waits for the servo to get there  

} 

进一步阅读:朱利安·贝勒(Julian Bayle),“ Arduino的C编程”。 给初学者的很棒的书。

暂无
暂无

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

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