简体   繁体   中英

Arduino Servo Function

I was fooling around with an Arduino yesterday. It arrived in the mail yesterday, and I have zero experience with Arduino. Here is my code, how do I go about putting it into a function? I want to shorten the code, as it seems quite repetitive here. I understand how functions work and such, I just don't know the proper syntax and such. So, how would I go about putting this into a function?

Code:

/* 

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  

} 

Thanks in advance for the help. Sorry for being new to this :)

// 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 );

I hope that my comments will help you understand the changes.

I'd like to introduce you to two nice rules: 1. Avoid magic numbers - the make the code less readeable and harder to change, because sometimes you have to remember about many places where you use a value 2. classes and structures should be passed by pointer (const pointer if they are not supposed to be changed) or reference (which is much better, for example because you don't have to deal with NULL cases).

/* 

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  

} 

Further reading: Julian Bayle, "C programming for Arduino". Awesome book for beginners.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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