Étape 5: Codage de base
Cela a été mon premier projet afin que le code soit rugueux. Voici un code de base pour tous les quatre du moteur de tourner pendant 5 secondes, il n’y a pas de codeur ou xbee dans ce code.
//ROBOT CAR CODE 001
int speedch1 = 9; int speedch2 = 10; int speedch3 = 11; int speedch4 = 12;
int dirch1 = 44; int dirch2 = 45; int dirch3 = 42; int dirch4 = 43;
int curch1 = A1; int curch2 = A2; int curch3 = A3; int curch4 = A4;
void setup() { // initialize the digital pin as an output. pinMode(dirch1,OUTPUT); pinMode(dirch2,OUTPUT); pinMode(dirch3,OUTPUT); pinMode(dirch4,OUTPUT);
pinMode(speedch1,OUTPUT); pinMode(speedch2,OUTPUT); pinMode(speedch3,OUTPUT); pinMode(speedch4,OUTPUT);
}
la routine de la boucle s’exécute maintes et maintes fois pour toujours : // the loop routine runs over and over again forever: void loop() { delay(2000); digitalWrite(dirch1,LOW); // direction motor spins digitalWrite(dirch2,HIGH); // 1 and 2 are on the same side but fight against each other. digitalWrite(dirch3,LOW); digitalWrite(dirch4,HIGH); analogWrite(speedch1,200); // speed PWM 0 - 255 analogWrite(speedch2,200); analogWrite(speedch3,200); analogWrite(speedch4,200); delay(5000);//how long the motors run analogWrite(speedch1,0); // stop analogWrite(speedch2,0); analogWrite(speedch3,0); analogWrite(speedch4,0); } Delay(2000) ; digitalWrite(dirch1,LOW) ; direction moteur tourne digitalWrite(dirch2,HIGH) ; 1 et 2 sont du même côté mais lutte contre l’autre. digitalWrite(dirch3,LOW) ; digitalWrite(dirch4,HIGH) ; analogWrite(speedch1,200) ; Vitesse PWM 0 - 255 analogWrite(speedch2,200) ; analogWrite(speedch3,200) ; analogWrite(speedch4,200) ; retard (5000); //how long que les moteurs tournent analogWrite(speedch1,0) ; arrêt analogWrite(speedch2,0) ; analogWrite(speedch3,0) ; analogWrite(speedch4,0) ; // Interrupt information // pins may be different on uno<br>// 0 on pin 2 // 1 on pin 3