Paso 42: Código de prueba-Control de PC mediante USB
Rápidamente estamos utilizando hasta las clavijas en el primer arduino. Tendremos que utilizar varias tarjetas y entonces comunicarse entre ellos. Para conseguir esto comenzó he añadido entradas serie al código de prueba. Una estructura de caso control de interruptor de llama a la función basada en entrada en serie.El código está a continuación:
Ahora los siguientes comandos de controlan de los movimientos. Se envían utilizando al monitor serie. Usando todas minúsculas (es realmente los códigos ascii para las letras que se envían, asuntos tan casos)
f = adelante
b = al revés
r = brazo derecho hacia arriba
t = brazo derecho abajo
w = brazo izquierdo hasta
e = brazo izquierdo abajo
z = curva de la cintura hacia abajo
x = curva de cintura para arriba
/////////////////////////////Arduino Code //////////////////////////////
////// Robot test with little routines made into functions////// ////// Added serial control /////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 = 200; int t2 = 1000; int WheelSpeed =75; int incomingByte; ///////// 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("...."); delay(2000); if(Serial.available()>0){ incomingByte = Serial.read(); switch(incomingByte){ case'f': Foward(); break; case'b': Backward(); break; case'r': RightArmUp(); break; case't': RightArmDown(); break; case'w': LeftArmUp(); break; case'e': LeftArmDown(); break; case'z': WaistBendDown(); break; case'x': WaistBendUp(); break; } } } //////////////////////////////////////////////////////// BASIC FUNCTIONS ////////////////////////////////////////////////////////////////// void RightArmUp(){ // Test Right Arm Serial.println("Right Arm"); //Right Arm Up Serial.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 Down Serial.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 Arm Serial.println("Left Arm Up"); //Left Arm Up digitalWrite(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 Down Serial.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 Waist Serial.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 Up Serial.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); } void Spin2(){ Serial.println("SPIN2"); digitalWrite(LeftWheelDir, HIGH); 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); } 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); } 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); }