Arduino motor shield is turning off randomly

Hi,

I'm making a robot for school but i have some problems with it. I have the following parts:
2x DC Motor
1x 9v Battery
1x Arduino UNO
1x Arduino Motor shield
1x HC-SR04 Ultrasone
1x IR Remote

I have the following problem:
The motors are always turning around. Except if the ultrasonic is detecting an object. Then it will turn around until the object is away. But sometimes, the arduino stops working. The IR remote, the motors and the serial port aren't working anymore. If i restart the arduino, it will work again. It's always happening when Motor A is start turning after detecting an object. Here's my code(Some serial prints and notes are in dutch because i'm dutch):

#include <NewPing.h> // Libary toevoegen
#define TRIGGER_PIN  4 // Trigger pin
#define ECHO_PIN     2   // Echo pin
#define MAX_DISTANCE 200 // Maximale afstand
boolean ultraAan = true; // Variabele voor in/uitschakelen
int ultraAfstand = 15; // Variabele voor afstand
boolean ultraActive = false;
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Ultrasonic
boolean carOn = false; // Variabele voor in/uitschakelen
int motorspeed_a = 255; 
int motorspeed_b = 255;
#include <IRremote.h> // Libary toevoegen
int IR_PIN = 7; // DataPin IR Remote
IRrecv remote(IR_PIN); // IR Remote
decode_results results; // IR Remote Resultaten


void setup() {
  Serial.begin(9600);
  // Serial.begin(9600); // Seriele poort openen
  remote.enableIRIn(); // IR ontvanger activeren
  pinMode(2, INPUT);
  pinMode(4, OUTPUT);
  pinMode(12, OUTPUT); //Initalizeren Motor A pin
  pinMode(9, OUTPUT); //Initalizeren Rem A pin
  //Motor B instellen
  pinMode(13, OUTPUT); //Initalizeren Motor A pin
  pinMode(8, OUTPUT); //Initalizeren Rem B pin  
}


void loop() {
  process();
}

void process() {
   readIR();
   readUltra();
   if (carOn == true) {
     if (ultraActive == true) {
       remmen("A", true);
       remmen("B", true);
       Serial.println("Rem AAN, UltraActive = true");
       delay(1000);
       remmen("A", false);
       remmen("B", false);
       Serial.println("Rem UIT, UltraActive = true");
       rijden("links", "");
       Serial.println("Rijden LINKS");
     } else {
       remmen("A", true);
       remmen("B", true);
       Serial.println("Rem AAN, UltraActive = false");
       delay(1000);
       remmen("A", false);
       remmen("B", false);
       Serial.println("Rem uit, UltraActive = false");
       rijden("vooruit", "A");
       rijden("vooruit", "B");
       Serial.println("Rijden VOORUIT");
     }

   } else {
     remmen("A", true);
     remmen("B", true);
     Serial.println("Rem aan");
   }
}


void rijden(String richting, String motor) {
  if (richting == "vooruit") {
    if (motor == "A") {
      digitalWrite(12, HIGH); //Motor A instellen op VOORUIT
      Serial.println("Motor A op vooruit");
      analogWrite(3, motorspeed_a);
      Serial.println("Motor A laten draaien");
    } else if (motor == "B") {
      digitalWrite(13,HIGH);
      Serial.println("Motor B op vooruit");
      analogWrite(11, motorspeed_b);
      Serial.println("Motor B laten draaien");
    }
  } else if (richting == "achteruit") {
    if (motor == "A") {
        digitalWrite(12, LOW); //Motor A instellen op ACHTERUIT
        Serial.println("Motor A op achteruit");
        analogWrite(3, motorspeed_a);
        Serial.println("Motor B achteruit draaien");

    } else if (motor == "B") {
        digitalWrite(13,LOW);
        Serial.println("Motor B op achteruit");
        analogWrite(11, motorspeed_b);
        Serial.println("Motor B achteruit draaien");
      
    }
  } else if (richting == "links") {
     digitalWrite(12, LOW);
     Serial.println("Motor A op vooruit");
     digitalWrite(13, HIGH);
     Serial.println("Motor B op achteruit");
     analogWrite(3, motorspeed_a);
     Serial.println("Motor A laten draaien");
     analogWrite(11, motorspeed_b);
     Serial.println("Motor B laten draaien");
  } else if (richting == "rechts") {
     digitalWrite(12, HIGH);
     Serial.println("Motor A op achteruit");
     digitalWrite(13, LOW);
     Serial.println("Motor B op vooruit");
     analogWrite(3, motorspeed_a);
     Serial.println("Motor A laten draaien");
     analogWrite(11, motorspeed_b);
     Serial.println("Motor B laten draaien");
     
  }
  
}

void remmen(String motor, boolean aan) {
  if (aan == true) {
    if (motor == "A") {
      digitalWrite(9, HIGH);
    } else if (motor == "B") {
      digitalWrite(8, HIGH);
    }
  } else if (aan == false) {
    if (motor == "A") {
      digitalWrite(9, LOW);
    } else if (motor == "B") {
      digitalWrite(8, LOW);
    }
  }
}

void readUltra() {
  int UltraDistance = sonar.ping_cm();
  Serial.println("Afstand lezen ultrasonic");
    if (ultraAan) { // Als de ultrasonic ingeschakeld is
      Serial.println("ultraAan is AAN");
      if (UltraDistance < ultraAfstand && UltraDistance > 0) { // Als de werkelijke afstand minder is dan de echte afstand
          Serial.println("UltraAfstand is te klein");
          ultraActive = true;
      } else {
          Serial.println("UltraAfstand is OK");
          ultraActive = false;
      }
      delay(50);
  }
}

void readIR() {
  if (remote.decode(&results)) { // Als er gegevens zijn ontvangen
    translateIR(); // Acties uitvoeren bij de ontvangen codes
    remote.resume(); // Volgende code ontvangen
  }
}


void translateIR() {
  switch(results.value)
  {
  case 3810010651:  
    ultraAfstand = ultraAfstand - 1; 
    break;
  case 5316027:  
    ultraAan =! ultraAan;
    break;
  case 4001918335:  
    ultraAfstand = ultraAfstand + 1; 
    break;
  case 1386468383:  
    break;
  case 3622325019:  
    break;
  case 553536955:  
    carOn =! carOn;
    break;
  case 4034314555:  
    break;
  case 2747854299:  
    break;
  case 3855596927:  
    break;
  case 3238126971:  
    break;
  case 2538093563:  
    break;
  case 4039382595:  
    break;
  case 2534850111:  
    break;
  case 1033561079:  
    break;
  case 1635910171:  
    break;
  case 2351064443:  
    break;
  case 1217346747:  
    break;
  case 71952287:  
    break;
  case 851901943:  
    break;
  case 465573243:  
    break;
  case 1053031451:  
    break; 
  }
}

The serial prints aren't the problem. Can somebody help me please because the robot needs to be hand in at the end of the week.

Thanks!
Martijn

Sorry for my bad english.

Could you post your code again using the code block button in the top left of the post editor?

Easier to read, and formats correctly.

It could be due to the 9volt battery not being able to supply sufficient current. Try a different power supply.

Weedpharma

Yup, the 9V is probably the reason.

But a question on your code:
The switch case in your translateIR, does results.value return a integer? A switch() function works with statements right?
And instead of

case 4034314555:  
    break;
  case 2747854299:  
    break;
  case 3855596927:  
    break;
  case 3238126971:  
    break;
  case 2538093563:  
    break;
  case 4039382595:  
    break;
  case 2534850111:  
    break;
  case 1033561079:  
    break;
  case 1635910171:  
    break;
  case 2351064443:  
    break;
  case 1217346747:  
    break;
  case 71952287:  
    break;
  case 851901943:  
    break;
  case 465573243:  
    break;
  case 1053031451:  
    break;

you can replace it with

default:
break;

(Trouwens, de volgende keer als je een stuk van je code post, klik eerst even op de "</>" knop bovenaan. Er komt dan een klein venstertje waar je je code in post. He is dan allemaal een stuk overzichtelijker en heb je meer kans geholpen te worden, er zijn mensen noggal precies over!!)

Yes, the results.value returns an integer. The remote's works fine! I'm gonna try with another battery tomorrow(The robot is on school now). If the problem is solved, i will let you now!

I have one more question. What kind of power supply do i need. I have a 9v battery to VIN and GROUND but will it work with 4x AA battery's. Or do i need something else?

Everyone thanks for the quick reply. I also did put my code in code tags.

Thanks again!
Martijn

With 4 AA battery's your total voltage is (at best) 6V, and that's on the lower part of the limit voltages an Arduino can have. Better to use 6 or 8 AA battery's.

But it's even better to use a real battery to voltage converter. Something like this:
https://www.antratek.nl/li-po-rider
(Site is in dutch, OP is also dutch so extra convenience)
Or some other LiPo charger/DC-DC step up converter.

I agree that the problem is probably due to insufficient power supply. Try fixing that first. If it doesn't help, it could be DC motor noise. Check out the discussion here http://forum.arduino.cc/index.php?topic=330906.0.