Robot with remote control

hello im makin this robot but im having some problems

first of all the program im using is this

#include <IRremote.h>
#include <IRremoteInt.h>

//carica libreria IR 
int RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);//imposta il pin di ricezione dei dati
decode_results results;
unsigned long code; // memorizza il codice ricevuto

//ai codici ricevuti aggiungere 0x + codice
#define UP      0x11//comando marcia avanti
#define KEY_UP    0xE246AFCA  //codice del tasto freccia su
#define DOWN  0x22 //comando marcia indietro
#define KEY_DOWN  0x3AFB26D4  //codice del tasto freccia giu
#define LEFT 0x33 //comando gira a sinistra
#define KEY_SX 0x4D43596A //codice del tasto freccia sinistra
#define RIGHT 0x44 //comando gira a destra
#define KEY_DX 0xBB0EDD22 //codice del tasto freccia destra
#define STOP 0x55 //Comando stop
#define KEY_STOP 0xB520CEC //codice del tasto STOP

//variabili usate dal sonar
#define ECHO 12 //arduino pin D12 -SRF04 pin 4
#define TRIG 11 //arduino pin D11-SRF04 pin 3
unsigned long echo_time = 0;//tempo di ritorno dell echo sonar in us
#define DISTANCE_CM echo_time / 48  //calcola la distanza in cm
unsigned long go_sonar = 0; //timer per accensione a tempo 
boolean obstacle = 0 ; // memorizza se è stato avvicinato un ostacolo
unsigned long max_distance = 30 ; // distanza massima a cui può avvicinarsi il robot

//variabili usate dai motori 
#define PIN_PWMA 5 //arduino pin D3 PWM motore A
#define PIN_DIRA 2 //arduino pin D2 comando direzione motore A
#define PIN_PWMB 9 //arduino pin D3 PWM motore B
#define PIN_DIRB 8 //arduino pin D2 comando direzione motore B
int velo_A = 0 ; // imposta velocità motore A
int velo_B = 0; //imposta velocità motore B
int dir_A = LOW ; // imposta la direzione del motore A
int dir_B= LOW ; // imposta la direzione motore B

void setup() {
  // put your setup code here, to run once:
  irrecv.enableIRIn();//attiva il ricevitore 
  pinMode(ECHO, INPUT); //arduino attiva ingresso
  pinMode (TRIG, OUTPUT); //arduino attiva uscita
  pinMode (13, OUTPUT); // arduino attiva uscita pin D13 led L
  pinMode (PIN_PWMA, OUTPUT); //arduino attiva uscita motore A
  pinMode (PIN_PWMB, OUTPUT); //arduino attiva uscita motore B
  pinMode (PIN_DIRA, OUTPUT); //arduino attiva uscita direzione A
  pinMode (PIN_DIRB, OUTPUT); //arduino attivs uscita direzione B
  Serial.begin(9600); // attiva la comunicazione seriale-USB
}

void loop() {
 
  //ricevi i segnali dal telecomando 
  if (irrecv.decode(&results)) { // stato ricevuto un comando
    digitalWrite(13, HIGH);// accende il led L di arduino
    if (results.value == KEY_UP) code = UP; //se premuto freccia SU
    if(results.value == KEY_DOWN) code = DOWN; //se premuto freccia GIU
    if(results.value == KEY_SX) code = LEFT;//se premuto freccia SINISTRA
    if(results.value == KEY_DX) code = RIGHT;//se premuto freccia DESTRA  
    if(results.value == KEY_STOP) code = STOP;// se premuto tasto STOP
    Serial.print(results.value, HEX);//invia alla seriale -USB il codice ricevuto
    Serial.print("   "); // invia 3 spazi
    Serial.println(code, HEX); // invia alla seriale-USB il tasto premuto
    irrecv.resume();// riceve il prossimo codice
    delay (5); //aspetta 5 ms
    digitalWrite (13, LOW); // spegne il led L di arduino
  }

  // misura la distanza con il sonar 
  delay(1); //conta fino ad 1ms
  if(go_sonar++ >= 250) {//attiva il sonar ogni 250 cicli del loop
    digitalWrite(TRIG, HIGH);//attiva l emissione sonar
    delayMicroseconds(10); //aspetta 10 us
    digitalWrite(TRIG, LOW);//termina l emissione sonar
    echo_time =pulseIn(ECHO, HIGH);//misura la durata dell echo
    if(DISTANCE_CM <= max_distance) obstacle = 1; //si è troppo vicini all ostacolo
    else obstacle = 0; //non si è in presenza di ostacoli
    if(obstacle) Serial.print("obstacle "); //scrivi in presenza di un ostacolo
    Serial.print("  distance  ");
    Serial.println(DISTANCE_CM); //stampa la distanza in centimetri
    go_sonar = 0; // azzera il timer
  }
code == STOP;
  //comanda i motori
  if(!obstacle) {//se non ci sono ostacoli muove il robot
    //se DIR_A= LOW allora led A verde// se DIR_B =HIGH allora led B rosso
    if(code == UP) { 
      velo_A = 280; 
      velo_B = 230; 
      dir_A =LOW; 
      dir_B =HIGH;
    }

    else if(code == DOWN) {
      velo_A = 250; 
      velo_B = 240; 
      dir_A =HIGH; 
      dir_B =LOW;
    }
    else if(code == LEFT) {
      velo_A = 250; 
      velo_B = 250; 
      dir_A =HIGH; 
      dir_B =HIGH;
    }
    else if(code == RIGHT) {
      velo_A = 250; 
      velo_B = 125; 
      dir_A =LOW; 
      dir_B =LOW;
    }
    else if(code == STOP) {
      velo_A = 0; 
      velo_B = 0; 
      dir_A =LOW; 
      dir_B =LOW;
    }
  }
  //altrimenti ferma il robot finche è presente l ostacolo
  else { 
    velo_A = 0; 
    velo_B = 0; 
    dir_A = LOW; 
    dir_B = LOW;
  }
  analogWrite(PIN_PWMA, velo_A);//imposta la velocità del motore A
  analogWrite(PIN_PWMB, velo_B);//imposta la velocità del motore B
  digitalWrite(PIN_DIRA, dir_A);//imposta la direzione del motore A
  digitalWrite(PIN_DIRB, dir_B); //imposta la direzione del motore B
}

my first problem is that when i connect batteries to the shields it completely ignores the program and starts to run in circle. The problem of running in circle should be caused by the speed of the 2 motors but why is it running in circle and not following the program?
2ndly when it works(rarely,and battieries arent connect; ONLY ARDUINO AND COMPUTER) it doesnt read the comands of the remote controller very properly; could it be caused by low battieries of the remote control?
thx for ur incomin help

Did you write the program?

Yes I wrote it
I'm not really good with arduino language but I'm getting used. Think that this is my first work

if u need some explanation about the robot ask me :slight_smile:

the robot weights is 460 g and uses 4 batteries of 1.5v
the wheels are 36 mm
link to motors is : http://www.futurashop.it/double-gearbox-7300-dgearbox
it is in italian.

up

up

It looks like you have a lot of shields on there. Are you sure they aren't interfering with each other?

chummer1010:
It looks like you have a lot of shields on there. Are you sure they aren't interfering with each other?

I think the first suspect would be cleanliness of the power coming from that switching regulator on the top.

Preferably the motors would be taking power directly from the batteries, unregulated, and then the linear regulator on the Arduino should be able to provide plenty of clean power for everything else.

the 3 shield are necessary because the motorshield(the middle1) is used for connect motors; the robotshield(at the top) has the sonar and the ir receiver.

Chagrin:
I think the first suspect would be cleanliness of the power coming from that switching regulator on the top.

Preferably the motors would be taking power directly from the batteries, unregulated, and then the linear regulator on the Arduino should be able to provide plenty of clean power for everything else.

Should i connect the batteries to the motorshield and keep the connection with the computer?

Hi,
We will need;
links to the specs/data on all the shields you are using.
A diagram of how you have the motors and powersupply (batteries) connected.

When you wrote this code, did you write it a section at a time?
First get the motor drive working ,ON ITS OWN.
Second get the IR Remote working, ON ITS OWN.
Third get the sonar working, ON ITS OWN.

Then combine two codes and get them working, BEFORE adding the next.

Do you have a DMM to measure voltages and currents.
If so, what is the battery voltage when the robot starts to misbehave.

Also have you got bypass capacitors on the motors.

Thanks.. Tom... :slight_smile: