Bonjour
Pour jouer , j'ai acheté sur ali un petit buggy composé d'un arduinor3 uno, et de divers capteurs et moteurs.
Pour le programmer , je me suis imposé d'utiliser exclusivement l'interface blockly@arduino (voir ici)
mon code et le vehicule fonctionne très bien tant que je n'y incorpore pas tout ce qui est en rapport avec la librairie servo, sinon un seul des moteur se met en marche !
voila le code,
#include <SoftwareSerial.h>
#include <Servo.h>
int distance;
int valbluetooth;
// pin assignation for L298N : MotA
int l298n_MotA[3] = {3, 4, 5};
// pin assignation for L298N : MotB
int l298n_MotB[3] = {10, 9, 8};
SoftwareSerial COMbluetooth(13,12);
Servo SERVO;
int mesure_distance_cm(byte trig_pin, byte dist_pin)
{
digitalWrite(trig_pin,HIGH);
delayMicroseconds(1000);
digitalWrite(trig_pin,LOW);
int value = (pulseIn(dist_pin,HIGH)/2)/29.1+2;
if (value>255) { value=255; }
delay(20);
return value;
}
void l298n_forward(int speed,int Pins[3])
{
analogWrite(Pins[0],speed);//Motor speed
digitalWrite(Pins[1],HIGH);//turn DC Motor move clockwise
digitalWrite(Pins[2],LOW);//turn DC Motor move clockwise
}
// Décrire cette fonction
void avance() {
l298n_forward(175, l298n_MotA);
l298n_forward(175, l298n_MotB);
}
void l298n_backward(int speed,int Pins[3])
{
analogWrite(Pins[0],speed);//Motor speed
digitalWrite(Pins[1],LOW);//turn DC Motor move anti-clockwise
digitalWrite(Pins[2],HIGH);//turn DC Motor move anti-clockwise
}
// Décrire cette fonction
void recule() {
l298n_backward(175, l298n_MotA);
l298n_backward(175, l298n_MotB);
}
// Décrire cette fonction
void gauche() {
l298n_forward(80, l298n_MotA);
l298n_forward(250, l298n_MotB);
}
// Décrire cette fonction
void droite() {
l298n_forward(250, l298n_MotA);
l298n_forward(80, l298n_MotB);
}
// Décrire cette fonction
void pivoteG() {
l298n_backward(120, l298n_MotA);
l298n_forward(120, l298n_MotB);
}
// Décrire cette fonction
void pivoteD() {
l298n_forward(120, l298n_MotA);
l298n_backward(120, l298n_MotB);
}
void l298n_stop(int Pins[3])
{
analogWrite(Pins[0],0);//Motor speed
digitalWrite(Pins[1],LOW);//turn DC Motor off
digitalWrite(Pins[2],LOW);//turn DC Motor off
}
// Décrire cette fonction
void arret() {
l298n_stop(l298n_MotA);
l298n_stop(l298n_MotB);
}
void setup() {
// pin assignation for L298N : MotA
pinMode(4,OUTPUT);//IN1_MotA Pin
pinMode(5,OUTPUT);//IN2_MotA Pin
pinMode(3,OUTPUT);//PWM_MotA Pin
// pin assignation for L298N : MotB
pinMode(9,OUTPUT);//IN1_MotB Pin
pinMode(8,OUTPUT);//IN2_MotB Pin
pinMode(10,OUTPUT);//PWM_MotB Pin
COMbluetooth.begin(9600);
Serial.begin(9600);
SERVO.attach(11); // SI J ENLEVE CA CA FONCTIONNE !!
pinMode(7,OUTPUT);//Sonar triger pin
pinMode(2,INPUT);//Sonar distance pulse pin
distance = 15;
valbluetooth = 5;
}
void loop() {
if (COMbluetooth.available()) {
valbluetooth = COMbluetooth.read();
}
if (valbluetooth == 8) {
avance();
SERVO.write(90); // SI J ENLEVE CA CA FONCTIONNE !!
} else if (valbluetooth == 4) {
gauche();
SERVO.write(30); // SI J ENLEVE CA CA FONCTIONNE !!
} else if (valbluetooth == 6) {
droite();
} else if (valbluetooth == 5) {
arret();
} else if (valbluetooth == 2) {
recule();
} else if (valbluetooth == 1) {
pivoteG();
} else if (valbluetooth == 3) {
pivoteD();
}
if (mesure_distance_cm(7,2) <= distance) {
valbluetooth = 5;
}
}
Une idée ? Olivier