Etape 2: Commencer par le Robot
Je joins le code ci-dessous, mais, si vos ne pas en utilisant le logiciel Arduino j’ai posté ci-dessous. C’est ce que j’ai utilisé pour mon robot rover pour RobotShop.com. J’ai pris un simple croquis WASD et modifié pour permettre à un programme pour lutter contre elle « à distance ». Une fois que vous avez copié le code vous pouvez le télécharger à votre robot à l’aide d’une clé USB par câble ou tout ce qui signifie que vous utilisez pour télécharger sur votre 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 pour une communication série. Si aucune réception faire procéder à l’instruction switch. char de données = Serial.read() ; Réglage de la vitesse. 255 est la vitesse max, vous pouvez modifier les valeurs ci-dessous pour la ralentir si vous le souhaitez. int leftspeed = 255 ; int rightspeed = 255 ; commutateur (données) {case « 0 »: //If l’arduino reçoit un 0 puis il exécutera la commande halt qui est définie ci-dessous. stopper (); break ; case "1": vers l’avant (leftspeed, rightspeed); break ; case "2": inverser (leftspeed, rightspeed); break ; case "3": gauche (rightspeed, leftspeed); break ; case "4": droit (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, basse) ; void halt(void) { digitalWrite(motorSpeed, LOW); digitalWrite(motor2Speed, LOW); }
avant Sub (char a, char b) {analogWrite(motorSpeed, a) ; //releasing le "frein" digitalWrite (motor1, LOW); //Applying pleine puissance à la broche. Ce serait typiquement haut mais, mon fils sont branchés en arrière alors j’ai juste changé la commande. analogWrite (motor2Speed, b) ; digitalWrite (motor2, basse) ; 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); }
Sub inverse (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); } void gauche (char a, char b) {analogWrite (motorSpeed, un); digitalWrite (motor1, HIGH); analogWrite (motor2Speed, b); digitalWrite (motor2, basse) ; 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; void droit (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;