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.