Arduino trådlös Animatronic Hand (6 / 9 steg)
Steg 6: koden!!!
Allra första du vill göra är se till att din sköld eller xbee's är urkopplade från arduino. Eller se till att rätt byglarna är den på rätt ställe (i mitt fall). Jag har skrivit här koden och om du använder det vill kredit.
Detta är koden för sändande Arduino:
int Finger1 = 0;
int Finger2 = 1;
int Finger3 = 2;
int Finger4 = 3;
int Finger5 = 4;
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte servoValue1;
byte servoValue2;
byte servoValue3;
byte servoValue4;
byte servoValue5;
int FingerV1 = analogRead(Finger1);
int FingerV2 = analogRead(Finger2);
int FingerV3 = analogRead(Finger3);
int FingerV4 = analogRead(Finger4);
int FingerV5 = analogRead(Finger5);
if (FingerV1 < 200) FingerV1 = 200;
else if (FingerV1 > 460) FingerV1 = 460;
if (FingerV2 < 200) FingerV2 = 200;
else if (FingerV2 > 460) FingerV2 = 460;
if (FingerV3 < 200) FingerV3 = 200;
else if (FingerV3 > 460) FingerV3 = 460;
if (FingerV4 < 200) FingerV4 = 200;
else if (FingerV4 > 460) FingerV4 = 460;
if (FingerV5 < 200) FingerV5 = 200;
else if (FingerV5 > 460) FingerV5 = 460;
byte servoVal1 = map(FingerV1,460, 200, 255, 0);
byte servoVal2 = map(FingerV2,460, 200, 255, 0);
byte servoVal3 = map(FingerV3,460, 200, 255, 0);
byte servoVal4 = map(FingerV4,460, 200, 255, 0);
byte servoVal5 = map(FingerV5,460, 200, 255, 0);
Serial.print(servoVal1);
Serial.print(servoVal2);
Serial.print(servoVal3);
Serial.print(servoVal4);
Serial.print(servoVal5);
delay(100);
}
Here is the receiving:
#include
Servo myservo1; // create servo object to control a servo
Servo myservo2;
Servo myservo3;
Servo myservo4;
Servo myservo5;
void setup()
{
Serial.begin(9600);
myservo1.attach(2); // attaches the servo on pin 9 to the servo object
myservo2.attach(3);
myservo3.attach(4);
myservo4.attach(5);
myservo5.attach(6);
}
void loop()
{
if(Serial.available() >=5)
{
byte servoAng1 = Serial.read();
byte servoAng2 = Serial.read();
byte servoAng3 = Serial.read();
byte servoAng4 = Serial.read();
byte servoAng5 = Serial.read();
// Send the servo to the position read... (note: you get to make this happen)
myservo1.write(servoAng1);
myservo2.write(servoAng2);
myservo3.write(servoAng3);
myservo4.write(servoAng4);
myservo5.write(servoAng5);
}
}