Thanx for your responses,
The 2 values 20 and 25 are motors's speed. My little tank is a cheap one so I must trim to go straight :-[
I've tested first without the delay, but the motors never run...
I use a code by JoakimCh from this forum to control my motor driver TB6612FNG :
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1263858213
And the full code:
#include <VirtualWire.h>
//declaration des pins du controleur de moteurs
#define out_A_PWM 3
#define out_A_IN2 2
#define out_A_IN1 4
#define out_STBY 5
#define out_B_PWM 6
#define out_B_IN2 7
#define out_B_IN1 8
#define motor_A 0
#define motor_B 1
#define motor_AB 2
//IR Detection
int irPin = 11; // capteur IR connecté sur pin 11
int val = 0;
int mode = 0; //0 = radio 1 = auto
void setup()
{
pinMode(irPin, INPUT);
//configuration des pins du controleur de moteurs
pinMode(out_STBY,OUTPUT);
pinMode(out_A_PWM,OUTPUT);
pinMode(out_A_IN1,OUTPUT);
pinMode(out_A_IN2,OUTPUT);
pinMode(out_B_PWM,OUTPUT);
pinMode(out_B_IN1,OUTPUT);
pinMode(out_B_IN2,OUTPUT);
vw_setup(2000); // Bits per sec
vw_set_rx_pin(9);
vw_rx_start(); // Start the receiver PLL running
}
int lireCapteurDistance(){
//detection IR digital (10cm)
val = digitalRead(irPin);
return val;
}
void loop()
{
int ordre;
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
// Message with a good checksum received, dump HEX
for (i = 0; i < buflen; i++)
{
ordre = buf[i];
}
if(mode == 1){
if (ordre == 'F'){
mode = 0;
}else{
mode_auto();
}
}else{
switch (ordre) {
case 'A':
avancer();
motor_standby(true);
break;
case 'B':
reculer();
motor_standby(true);
break;
case 'C':
tournerDroite();
motor_standby(true);
break;
case 'D':
tournerGauche();
motor_standby(true);
break;
case 'E':
mode = 1;
break;
case 'F':
mode = 0;
break;
case 'S':
stopper();
break;
}
}
}
}
void mode_auto(){
//Serial.println(lireCapteurDistance());
//TODO
}
void avancer()
{
motor_standby(false);
motor_speed2(motor_A,20);
motor_speed2(motor_B,25);
}
void reculer()
{
motor_standby(false);
motor_speed2(motor_A,-20);
motor_speed2(motor_B,-25);
}
void stopper()
{
motor_brake(motor_A);
motor_brake(motor_B);
}
void tournerGauche()
{
motor_standby(false);
motor_speed2(motor_A,20);
motor_speed2(motor_B,-25);
}
void tournerDroite()
{
motor_standby(false);
motor_speed2(motor_A,-20);
motor_speed2(motor_B,25);
}
void motor_speed2(boolean motor, char speed) { //speed from -100 to 100
byte PWMvalue=0;
PWMvalue = map(abs(speed),0,100,50,255); //anything below 50 is very weak
if (speed > 0)
motor_speed(motor,0,PWMvalue);
else if (speed < 0)
motor_speed(motor,1,PWMvalue);
else {
motor_coast(motor);
}
}
void motor_speed(boolean motor, boolean direction, byte speed) { //speed from 0 to 255
if (motor == motor_A) {
if (direction == 0) {
digitalWrite(out_A_IN1,HIGH);
digitalWrite(out_A_IN2,LOW);
} else {
digitalWrite(out_A_IN1,LOW);
digitalWrite(out_A_IN2,HIGH);
}
analogWrite(out_A_PWM,speed);
} else {
if (direction == 0) {
digitalWrite(out_B_IN1,HIGH);
digitalWrite(out_B_IN2,LOW);
} else {
digitalWrite(out_B_IN1,LOW);
digitalWrite(out_B_IN2,HIGH);
}
analogWrite(out_B_PWM,speed);
}
}
void motor_standby(boolean state) { //low power mode
if (state == true)
digitalWrite(out_STBY,LOW);
else
digitalWrite(out_STBY,HIGH);
}
void motor_brake(boolean motor) {
if (motor == motor_A) {
digitalWrite(out_A_IN1,HIGH);
digitalWrite(out_A_IN2,HIGH);
} else {
digitalWrite(out_B_IN1,HIGH);
digitalWrite(out_B_IN2,HIGH);
}
}
void motor_coast(boolean motor) {
if (motor == motor_A) {
digitalWrite(out_A_IN1,LOW);
digitalWrite(out_A_IN2,LOW);
digitalWrite(out_A_PWM,HIGH);
} else {
digitalWrite(out_B_IN1,LOW);
digitalWrite(out_B_IN2,LOW);
digitalWrite(out_B_PWM,HIGH);
}
}
MadProf