0

昨天我在和一个 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  

} 

在此先感谢您的帮助。抱歉,我是新手 :)

4

2 回答 2

1
// 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 );
于 2013-11-05T22:07:45.183 回答
0

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

我想向您介绍两个不错的规则: 1. 避免使用幻数 - 这会使代码的可读性降低且难以更改,因为有时您必须记住许多使用值的地方 2. 类和结构应该是通过指针(如果它们不应该更改,则为常量指针)或引用(这要好得多,例如因为您不必处理 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 编程”。适合初学者的好书。

于 2013-11-05T22:20:14.440 回答