首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arduino伺服函数

Arduino伺服函数
EN

Stack Overflow用户
提问于 2013-11-06 05:41:22
回答 2查看 755关注 0票数 0

我昨天和一辆阿杜伊诺鬼混。它是昨天寄来的,而我对Arduino毫无经验。这是我的代码,如何将其放入一个函数中?我想缩短代码,因为它在这里看起来很重复。我知道函数是如何工作的,我只是不知道正确的语法。那么,我该如何将其放入一个函数中呢?

代码:

代码语言:javascript
复制
/* 

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  

} 

提前感谢你的帮助。抱歉,我对此还不熟悉:)

EN

回答 2

Stack Overflow用户

发布于 2013-11-06 06:07:45

代码语言:javascript
复制
// 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
EN

Stack Overflow用户

发布于 2013-11-06 06:20:14

我希望我的评论能帮助你理解这些变化。

我想向你介绍两个很好的规则: 1.避免魔术数字--这会使代码更难读,更难更改,因为有时你必须记住许多使用值的地方。2.类和结构应该通过指针(常量指针,如果它们不应该被更改)或引用(这更好,例如,因为你不必处理NULL情况)来传递。

代码语言:javascript
复制
/* 

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,"C programming for Arduino“。对初学者来说很棒的一本书。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19799576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档