Paso 6: Código de Arduino
Aquí está el código para hacer tu robot parpadean de ojos. Subir esto a la Arduino en su robot.
//// include the library "servo.h". #include ///// name the LEDs.
const int LED1 = 11; const int LED2 = 12; ////name the controller buttons.
const int button1 = 2; const int button2 = 3; const int button3 = 4; const int button4 = 7; const int button5 = 8;
//// define servos.
Servo servoLeft; // Define left servo. Servo servoRight; // Define right servo. Servo servoTop; // Define upper head servo. Servo servoNeck; // Define lower head servo.
//// set the enishole value of the controler butons to 0.
int buttonState1 = 0; int buttonState2 = 0; int buttonState3 = 0; int buttonState4 = 0; int buttonState5 = 0; void setup() { servoLeft.attach(10); // Set left servo to digital pin 10 servoRight.attach(9); // Set right servo to digital pin 9 servoTop.attach(5); // Set servoTop to digital pin 5 servoNeck.attach(6); // Set servoNeck to digital pin 6 //// define the LEDs as outputs. pinMode(LED1, OUTPUT); pinMode(LED2, OUTPUT);
//// define the controler butons as inputs. pinMode(button1, INPUT); pinMode(button2, INPUT); pinMode(button3, INPUT); pinMode(button4, INPUT); pinMode(button5, INPUT); }
////////this code plays over and over in a loop.
void loop() { buttonState1 = digitalRead(button1); buttonState2 = digitalRead(button2); buttonState3 = digitalRead(button3); buttonState4 = digitalRead(button4); buttonState5 = digitalRead(button5); if (buttonState3 == HIGH) { forward(); delay(50); } else { stopRobot(); digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); } if (buttonState4 == HIGH) { reverse(); delay(50); } else { digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); stopRobot(); } if (buttonState2 == HIGH) { turnRight(); delay(50); } else { digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); stopRobot(); } if (buttonState1 == HIGH) { turnLeft(); delay(50); } else { digitalWrite(LED1, LOW); digitalWrite(LED2, HIGH); stopRobot(); } if (buttonState5 == HIGH) { Up(); delay(50); } else { digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); stopRobot(); } if (buttonState2 and buttonState1 == HIGH) { Laugh(); delay(50); } else { digitalWrite(LED1, LOW); digitalWrite(LED2, LOW); stopRobot(); } if (buttonState3 and buttonState4 == HIGH) { Jiter(); delay(50); } else { digitalWrite(LED1, HIGH); digitalWrite(LED2, LOW); stopRobot(); } }
//////// funcshins void forward() { servoLeft.write(0); servoRight.write(180); }
void reverse() { servoLeft.write(180); servoRight.write(0); }
void turnRight() { servoLeft.write(180); servoRight.write(180); }
void turnLeft() { servoLeft.write(0); servoRight.write(0); }
void stopRobot() { servoLeft.write(90); servoRight.write(90); servoTop.write(90); servoNeck.write(90); }
void Up() { servoTop.write(50); servoNeck.write(90); }
void Laugh() { servoTop.write(90); servoNeck.write(20); }
void Jiter() { servoTop.write(90); servoNeck.write(130); }