昨天我在和一个 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
}
在此先感谢您的帮助。抱歉,我是新手 :)