Paso 2: A partir del Robot
He adjuntado el código a continuación, pero si tu no utilizando el software de Arduino he publicado a continuación. Esto es lo que usé para mi robot rover para RobotShop.com. Tomé un simple bosquejo WASD y modificado para permitir que un programa para controlar "remotamente". Una vez que han copiado el código puedes subirlo a tu robot utilizando un USB cable o lo que quiere decir que uso para subir a tu robot.
//Setting motor variablesint motorSpeed = 6; int motor2Speed = 5; int motor1 = 8; int motor2 = 7;
void setup() { int i; for(i=5;i<=8;i++) pinMode(i, OUTPUT); Serial.begin(9600); //Start Serial Communication
}
void loop() {//waiting para cualquier comunicación serial. Si se recibe cualquier conducta la sentencia switch. char datos = Serial.read(); Ajuste de la velocidad. 255 es la velocidad máxima, puede cambiar los valores de abajo para frenar Si quieres. int leftspeed = 255; int rightspeed = 255; interruptor (datos) {'0' del caso: //If arduino recibe un 0, entonces se ejecutará el comando de interrupción que se define a continuación. detener (); break; caso '1': adelante (leftspeed, rightspeed); break; caso '2': inversa (leftspeed, rightspeed); break; caso '3': izquierda (rightspeed, leftspeed); break; caso de '4': derecha (rightspeed, leftspeed); break; void loop() { //waiting for any serial communication. If any is received conduct the switch statement. char data = Serial.read(); //Setting speed. 255 is max speed, you can change the values below to slow it down if you want. int leftspeed = 255; int rightspeed = 255; switch (data) { case '0': //If the arduino receives a 0 then it will run the halt command which is defined below. halt (); break; case '1': forward (leftspeed, rightspeed); break; case '2': reverse (leftspeed, rightspeed); break; case '3': left (rightspeed, leftspeed); break; case '4': right (rightspeed, leftspeed); break; }
}
void halt(void) {digitalWrite (motorSpeed, LOW); digitalWrite (motor2Speed, bajo); void halt(void) { digitalWrite(motorSpeed, LOW); digitalWrite(motor2Speed, LOW); }
void forward (char a, char b) {analogWrite(motorSpeed, a); //releasing el "freno" digitalWrite (motor1, LOW); //Applying plenos poderes al pin. Esto normalmente sería alta, pero mis cables se enganchan para arriba al revés por lo que acabo de cambiar el comando. analogWrite (motor2Speed, b); digitalWrite (motor2, LOW); void forward(char a, char b) { analogWrite(motorSpeed, a); //releasing the "brake" digitalWrite(motor1, LOW); //Applying full power to the pin. This would typically be HIGH but, my wires are hooked up backwards so I just switched the command. analogWrite(motor2Speed, b); digitalWrite(motor2, LOW); }
void reverse (char a, char b) {analogWrite(motorSpeed, a); digitalWrite (motor1, HIGH); analogWrite (motor2Speed, b); digitalWrite (motor2, HIGH); void reverse (char a, char b) { analogWrite(motorSpeed, a); digitalWrite(motor1, HIGH); analogWrite(motor2Speed, b); digitalWrite(motor2, HIGH); } void left (char a,char b) { analogWrite (motorSpeed, a); digitalWrite(motor1, HIGH); analogWrite (motor2Speed, b); digitalWrite(motor2, LOW); } void right (char a,char b) { analogWrite (motorSpeed, a); digitalWrite(motor1, LOW); analogWrite (motor2Speed, b); digitalWrite(motor2, HIGH); } Anular izquierdo (char a, char b) {analogWrite (motorSpeed, un); digitalWrite (motor1, HIGH); analogWrite (motor2Speed, b); digitalWrite (motor2, LOW); using System;using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports; using System.Speech; using System.Speech.Recognition; using System.Speech.Synthesis; anular derecho (char a, char b) {analogWrite (motorSpeed, un); digitalWrite (motor1, LOW); analogWrite (motor2Speed, b); digitalWrite (motor2, HIGH); namespace Voice_Controlled_Rover { public partial class Form1 : Form { private SerialPort myport;