Stap 38: Test Code opnieuw - functie
De test code wordt steeds lang en verwarrend.
Zoals het nu is, moet elke actie eigen blok code met alle details in het. Dus, een herhalen moet geheel ' nother kopie van de code.
Een gemakkelijke manier om dit te behandelen is dat elke actie in een 'functie' vervolgens de functie aanroepen vanuit de hoofdlus.
De nieuwe code is hieronder. Ik heb genomen alles dat was in de hoofdlus en verplaatst naar het einde. Dan maakte ik elke actie in zijn eigen functie, met een eigen naam.
Als dit (de wijzigingen zijn vetgedrukt):
void RightArmDown() {
Rechterarm naar beneden
Serial.println ("rechterarm naar beneden");
digitalWrite (RightArmDir, laag);
analogWrite (RightArmSpeed, Slow);
vertraging (t1);
analogWrite (RightArmSpeed, Fast);
vertraging (2 * t2);
analogWrite (RightArmSpeed, Slow);
delay(T1);
analogWrite (RightArmSpeed, 0);
delay(2*T1);
}
In de hoofdlus, kunt u nu - de lange sectie van code vervangen door de naam van de functie:
nietig RightArmDown();
Dus - dat maakt de dingen een beetje makkelijker.
Hieronder is de onlangs georganiseerde code (overgenomen als html...)
Een video met het goed werkt:
[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); }