Paso 38: Ensayo código rehacer - utilizando la función
El código de prueba está haciendo largo y confuso.
Como lo es ahora, cada acción tiene su propio bloque de código con todos los detalles en él. Así que, repitiendo una acción necesita un conjunto ' copia del tro del código.
Una manera fácil de lidiar con esto es hacer que cada acción en una 'función' entonces llamar a la función del loop principal.
El nuevo código está por debajo. He tomado todo lo que fue en el bucle principal y trasladado hasta el final. Luego hice cada acción en su propia función, con su propio nombre.
Como esto (los cambios están en negrita):
void RightArmDown() {
Brazo derecho abajo
Serial.println "(brazo derecho del abajo");
digitalWrite (RightArmDir, bajo);
analogWrite (RightArmSpeed, lento);
retardo (t1);
analogWrite (RightArmSpeed, rápido);
demora (2 * t2);
analogWrite (RightArmSpeed, lento);
Delay(T1);
analogWrite (RightArmSpeed, 0);
Delay(2*T1);
}
Ahora - en el bucle principal, puede reemplazar la sección larga del código con el nombre de la función:
void RightArmDown();
-Hace las cosas un poco más fácil.
A continuación es el código recién organizado (copiado como html...)
Un video con ella funcionando bien:
[1318]
////////////////////// ARDUINO CODE //////////////////////////
////// Robot test with little routines made into functions///////////VARIABLES/////////// Direction low = towards body or Foward//Direction High = away from body or Backwardint RightArmDir = 2; int RightArmSpeed = 3; int LeftArmDir = 4; int LeftArmSpeed = 5; int WaistDir = 7; int WaistSpeed = 6; int RightWheelDir = 8; int RightWheelSpeed = 9; int LeftWheelDir = 12; int LeftWheelSpeed = 10; int Slow = 100; int Fast = 200; int t1 = 100; int t2 = 500; int WheelSpeed =120; ///////// SETUP /////////voidsetup() { pinMode(RightArmDir, OUTPUT); pinMode(RightArmSpeed, OUTPUT); pinMode(LeftArmDir, OUTPUT); pinMode(LeftArmSpeed, OUTPUT); pinMode(WaistDir, OUTPUT); pinMode(WaistSpeed, OUTPUT); pinMode(RightWheelDir, OUTPUT); pinMode(RightWheelSpeed, OUTPUT); pinMode(LeftWheelDir, OUTPUT); pinMode(LeftWheelSpeed, OUTPUT); Serial.begin(9600); } //////// MAIN LOOP ///////voidloop() { Serial.println("Start...."); delay(2000); RightArmUp(); RightArmDown(); LeftArmUp(); LeftArmDown(); WaistBendDown(); WaistBendUp(); Foward(); Backward(); Spin(); Serial.println("Repeat...."); } //////////////////////////////////////////////////////// BASIC FUNCTIONS //////////////////////////////////////////////////////////////////void RightArmUp(){ // Test Right ArmSerial.println("Right Arm"); //Right Arm UpSerial.println("Up"); digitalWrite(RightArmDir, HIGH); analogWrite(RightArmSpeed, Slow); delay (t1); analogWrite(RightArmSpeed,Fast); delay (2*t2); analogWrite(RightArmSpeed,Slow); delay(t1); analogWrite(RightArmSpeed, 0); delay(t1); } void RightArmDown(){ //Right Arm DownSerial.println("Right Arm Down"); digitalWrite(RightArmDir, LOW); analogWrite(RightArmSpeed, Slow); delay (t1); analogWrite(RightArmSpeed, Fast); delay (2*t2); analogWrite(RightArmSpeed, Slow); delay(t1); analogWrite(RightArmSpeed, 0); delay(2*t1); } void LeftArmUp(){ // Test Left ArmSerial.println("Left Arm Up"); //Left Arm UpdigitalWrite(LeftArmDir, HIGH); analogWrite(LeftArmSpeed, Slow); delay (t1); analogWrite(LeftArmSpeed, Fast); delay (2*t2); analogWrite(LeftArmSpeed, Slow); delay (t1); analogWrite(LeftArmSpeed, 0); delay(t1); } void LeftArmDown(){ //Left Arm DownSerial.println("Left Arm Down"); digitalWrite(LeftArmDir, LOW); analogWrite(LeftArmSpeed, Slow); delay (t1); analogWrite(LeftArmSpeed, Fast); delay (2*t2); analogWrite(LeftArmSpeed,Slow); delay(t1); analogWrite(LeftArmSpeed, 0); delay(2*t1); } void WaistBendDown(){ // Test WaistSerial.println("Waist Bend Down"); digitalWrite(WaistDir, HIGH); analogWrite(WaistSpeed, Slow); delay (t1); analogWrite(WaistSpeed, Fast); delay (2*t2); analogWrite(WaistSpeed, Slow); delay(t1); analogWrite(WaistSpeed, 0); delay(t1); } void WaistBendUp(){ //Bend UpSerial.println("Up"); digitalWrite(WaistDir, LOW); analogWrite(WaistSpeed, Slow); delay (t1); analogWrite(WaistSpeed, Fast); delay (2*t2); analogWrite(WaistSpeed, Slow); delay(t1); analogWrite(WaistSpeed, 0); delay(2*t1); } void Spin(){ Serial.println("SPIN"); digitalWrite(LeftWheelDir, LOW); digitalWrite(RightWheelDir,HIGH); analogWrite(RightWheelSpeed, WheelSpeed/2); analogWrite(LeftWheelSpeed, WheelSpeed/2); delay (t1); analogWrite(RightWheelSpeed, WheelSpeed); analogWrite(LeftWheelSpeed, WheelSpeed); delay (t2); analogWrite(RightWheelSpeed, WheelSpeed/2); analogWrite(LeftWheelSpeed, WheelSpeed/2); delay(t1); analogWrite(LeftWheelSpeed, 0); analogWrite(RightWheelSpeed, 0); delay(2*t1); } void Foward(){ Serial.println("Go Foward"); digitalWrite(LeftWheelDir, HIGH); digitalWrite(RightWheelDir,HIGH); analogWrite(RightWheelSpeed, WheelSpeed/2); analogWrite(LeftWheelSpeed, WheelSpeed/2); delay (t1); analogWrite(RightWheelSpeed, WheelSpeed); analogWrite(LeftWheelSpeed, WheelSpeed); delay (t2); analogWrite(RightWheelSpeed, WheelSpeed/2); analogWrite(LeftWheelSpeed, WheelSpeed/2); delay(t1); analogWrite(LeftWheelSpeed, 0); analogWrite(RightWheelSpeed, 0); delay(2*t1); } void Backward(){ Serial.println("Go Backward"); digitalWrite(LeftWheelDir, LOW); digitalWrite(RightWheelDir,LOW); analogWrite(RightWheelSpeed, WheelSpeed/2); analogWrite(LeftWheelSpeed, WheelSpeed/2); delay (t1); analogWrite(RightWheelSpeed, WheelSpeed); analogWrite(LeftWheelSpeed, WheelSpeed); delay (t2); analogWrite(RightWheelSpeed, WheelSpeed/2); analogWrite(LeftWheelSpeed, WheelSpeed/2); delay(t1); analogWrite(LeftWheelSpeed, 0); analogWrite(RightWheelSpeed, 0); delay(2*t1); }