Stap 48: MoveHead() toegevoegd aan de rest van de code van de serial test
Ik denk dat we moeten genoeg beweegt om aan de slag op iets anders.seriële commando's toegevoegd:
"h" - verplaatst van het hoofd - loopt door volledige sweep
de '-spin unidirectioneel (functie was er al, maar heb niet een geval)
had'-draaien van de andere kant (functie was er al, maar heb niet een geval)
Deze code moet korter worden gemaakt, maar hier is het:
video
[allMoves
//////////////////////////////// ARDUINO CODE//////////////////////////////////
Robot test met weinig routines verfilmd functies / / /
toegevoegd hoofd bewegen / / /
motor variabelen / / /
Richting lage = naar lichaam of doorsturen
Richting hoog = uit de buurt van lichaam of achterwaarts
int 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 = 200; tijd bij lage snelheid
int t2 = 1000; tijd snelheid
int tBreak = 100; tijd om te stoppen de motor door het omkeren van dir
int WheelSpeed = 75;
int breakSpeed = 10;
int bendSpeed = 255;
int incomingByte;
hoofd variabelen
#include < Servo.h >
Servo myservo; maken van servo-object om te bepalen van een servo
int pos = 80;
int posFront = 80;
///////// SETUP /////////
VOID Setup {}
pinMode (RightArmDir, uitvoer);
pinMode (RightArmSpeed, uitvoer);
pinMode (LeftArmDir, uitvoer);
pinMode (LeftArmSpeed, uitvoer);
pinMode (WaistDir, uitvoer);
pinMode (WaistSpeed, uitvoer);
pinMode (RightWheelDir, uitvoer);
pinMode (RightWheelSpeed, uitvoer);
pinMode (LeftWheelDir, uitvoer);
pinMode (LeftWheelSpeed, uitvoer);
myservo.attach(11); de servo op pin 11 hecht aan de servo-object
Serial.begin(9600);
}
MAIN LOOP / / /
void loop {}
Serial.println("...");
delay(400);
if(Serial.available() > 0) {}
incomingByte = Serial.read();
switch(incomingByte) {}
Case "f":
Foward();
breken;
Case "b":
Backward();
breken;
Case 'r':
RightArmUp();
breken;
't geval ':
RightArmDown();
breken;
Case 'w':
LeftArmUp();
breken;
Case "e":
LeftArmDown();
breken;
Case "z":
WaistBendDown();
breken;
Case 'x':
WaistBendUp();
breken;
Case "h":
MoveHead();
breken;
geval van ':
Spin();
breken;
geval zou ':
Spin2();
breken;
}
}
}
///////////////////////////////////////////////
BASISFUNCTIES / / /
///////////////////////////////////////////////
VOID RightArmUp() {}
De rechterarm test
Serial.println ("rechterarm");
Rechterarm omhoog
Serial.println("up");
digitalWrite (RightArmDir, hoge);
analogWrite (RightArmSpeed, Slow);
vertraging (t1);
analogWrite(RightArmSpeed,Fast);
vertraging (1.8 * t2);
analogWrite(RightArmSpeed,Slow);
delay(T1);
analogWrite (RightArmSpeed, 0);
delay(T1);
}
VOID RightArmDown() {}
Rechterarm naar beneden
Serial.println ("rechterarm naar beneden");
digitalWrite (RightArmDir, laag);
analogWrite (RightArmSpeed, Slow);
vertraging (t1);
analogWrite (RightArmSpeed, Fast);
vertraging (1.8 * t2);
analogWrite (RightArmSpeed, Slow);
delay(T1);
analogWrite (RightArmSpeed, 0);
delay(2*T1);
}
VOID LeftArmUp() {}
Test linkerarm
Serial.println ("linker Arm omhoog");
Linker Arm omhoog
digitalWrite (LeftArmDir, hoge);
analogWrite (LeftArmSpeed, Slow);
vertraging (t1);
analogWrite (LeftArmSpeed, Fast);
vertraging (2 * t2);
analogWrite (LeftArmSpeed, Slow);
vertraging (t1);
analogWrite (LeftArmSpeed, 0);
delay(T1);
}
VOID LeftArmDown() {}
Linkerarm naar beneden
Serial.println ("linker Arm omlaag");
digitalWrite (LeftArmDir, laag);
analogWrite (LeftArmSpeed, Slow);
vertraging (t1);
analogWrite (LeftArmSpeed, Fast);
vertraging (2 * t2);
analogWrite(LeftArmSpeed,Slow);
delay(T1);
analogWrite (LeftArmSpeed, 0);
delay(2*T1);
}
VOID WaistBendDown() {}
Test taille
Serial.println ("taille bocht naar beneden");
digitalWrite (WaistDir, hoge);
analogWrite (WaistSpeed, Slow);
vertraging (t1);
analogWrite (WaistSpeed, bendSpeed);
vertraging (2 * t2);
analogWrite (WaistSpeed, Slow);
delay(T1);
analogWrite (WaistSpeed, 0);
delay(T1);
}
VOID WaistBendUp() {}
Buig omhoog
Serial.println("up");
digitalWrite (WaistDir, laag);
analogWrite (WaistSpeed, Slow);
vertraging (t1);
analogWrite (WaistSpeed, bendSpeed);
vertraging (2 * t2);
analogWrite (WaistSpeed, Slow);
delay(T1);
analogWrite (WaistSpeed, 0);
delay(2*T1);
}
VOID Spin() {}
Serial.println("spin");
digitalWrite (LeftWheelDir, laag);
digitalWrite(RightWheelDir,HIGH);
analogWrite (RightWheelSpeed, WheelSpeed/2);
analogWrite (LeftWheelSpeed, WheelSpeed/2);
vertraging (t1);
analogWrite (RightWheelSpeed, WheelSpeed);
analogWrite (LeftWheelSpeed, WheelSpeed);
vertraging (t2);
analogWrite (RightWheelSpeed, WheelSpeed/2);
analogWrite (LeftWheelSpeed, WheelSpeed/2);
delay(T1);
analogWrite (LeftWheelSpeed, 0);
analogWrite (RightWheelSpeed, 0);
delay(T1);
digitalWrite (LeftWheelDir, hoge);
digitalWrite(RightWheelDir,LOW);
analogWrite (LeftWheelSpeed, breakSpeed);
analogWrite (RightWheelSpeed, breakSpeed);
delay(tBreak);
analogWrite (LeftWheelSpeed, 0);
analogWrite(RightWheelSpeed,0);
}
VOID Spin2() {}
Serial.println("SPIN2");
digitalWrite (LeftWheelDir, hoge);
digitalWrite(RightWheelDir,LOW);
analogWrite (RightWheelSpeed, WheelSpeed/2);
analogWrite (LeftWheelSpeed, WheelSpeed/2);
vertraging (t1);
analogWrite (RightWheelSpeed, WheelSpeed);
analogWrite (LeftWheelSpeed, WheelSpeed);
vertraging (t2);
analogWrite (RightWheelSpeed, WheelSpeed/2);
analogWrite (LeftWheelSpeed, WheelSpeed/2);
delay(T1);
analogWrite (LeftWheelSpeed, 0);
analogWrite (RightWheelSpeed, 0);
delay(T1);
digitalWrite (LeftWheelDir, laag);
digitalWrite(RightWheelDir,HIGH);
analogWrite (LeftWheelSpeed, breakSpeed);
analogWrite(RightWheelSpeed,breakSpeed);
delay(tBreak);
analogWrite (LeftWheelSpeed, 0);
analogWrite(RightWheelSpeed,0);
}
VOID Foward() {}
Serial.println ("Ga doorsturen");
digitalWrite (LeftWheelDir, hoge);
digitalWrite(RightWheelDir,HIGH);
analogWrite (RightWheelSpeed, WheelSpeed/2);
analogWrite (LeftWheelSpeed, WheelSpeed/2);
vertraging (t1);
analogWrite (RightWheelSpeed, WheelSpeed);
analogWrite (LeftWheelSpeed, WheelSpeed);
vertraging (t2);
analogWrite (RightWheelSpeed, WheelSpeed/2);
analogWrite (LeftWheelSpeed, WheelSpeed/2);
delay(T1);
analogWrite (LeftWheelSpeed, 0);
analogWrite (RightWheelSpeed, 0);
delay(T1);
digitalWrite (LeftWheelDir, laag);
digitalWrite(RightWheelDir,LOW);
analogWrite (LeftWheelSpeed, breakSpeed);
analogWrite(RightWheelSpeed,breakSpeed);
delay(tBreak);
analogWrite (LeftWheelSpeed, 0);
analogWrite(RightWheelSpeed,0);
}
VOID Backward() {}
Serial.println ("Ga naar achteren");
digitalWrite (LeftWheelDir, laag);
digitalWrite(RightWheelDir,LOW);
analogWrite (RightWheelSpeed, WheelSpeed/2);
analogWrite (LeftWheelSpeed, WheelSpeed/2);
vertraging (t1);
analogWrite (RightWheelSpeed, WheelSpeed);
analogWrite (LeftWheelSpeed, WheelSpeed);
vertraging (t2);
analogWrite (RightWheelSpeed, WheelSpeed/2);
analogWrite (LeftWheelSpeed, WheelSpeed/2);
delay(T1);
analogWrite (LeftWheelSpeed, 0);
analogWrite (RightWheelSpeed, 0);
delay(T1);
digitalWrite (LeftWheelDir, hoge);
digitalWrite(RightWheelDir,HIGH);
analogWrite (LeftWheelSpeed, breakSpeed);
analogWrite(RightWheelSpeed,breakSpeed);
delay(tBreak);
analogWrite (LeftWheelSpeed, 0);
analogWrite(RightWheelSpeed,0);
}
verplaatsen hoofd / / /
VOID MoveHead() {//create functie Beweeg hoofd / / hoofd heen en weer gaan
voor (pos = 5; pos < 145; pos += 1) //goes van 5 naar 145degrees
{/ / in stappen van 1 graad
myservo.write(POS); servo naar positie in de variabele 'pos' vertellen
Serial.println(POS);
delay(15); wacht 15ms voor de servo te bereiken de positie
if(POS == posFront) {}
delay(600);
}
}
voor (pos = 145; pos > posFront; = pos-= 1) / / goes van 145 graden naar voren
{
myservo.write(POS); servo naar positie in de variabele 'pos' vertellen
delay(15); wacht 15ms voor de servo te bereiken de positie
if(POS == posFront) {}
delay(600);
}
}
}