I have been working on a 4WD robot for a while now. The parts I'm using are Arduino v4 shield, l298N driver motor, Hc 05 ultrasonic sensor, DHT 11, mq2, Bluetooth module, servo motor, ir transmitter and receiver module and an uno all powered by 2 18650 Li ion batteries and a power bank. when i tried using the Ir sensors only, ultrasonic only or both for obstacle detection and avoidance the code freezes and nothing works anymore until i move the obstacle away then the robot makes the decision it wanted to make. it also used to reset occasionally while frozen but i fixed that issue by powering the Arduino separately. i noticed this problem mostly occurs when all the IRs give a low signal. i think the problem is with my code but i don't really see what i typed wrong.
#include <dht.h>
#include <Servo.h>
#define servoPin 11
// Create a servo object
Servo Servo1;
//Ultrasonic Sensor
#define trigPin 9
#define echoPin 10
// defines variables
long duration;
int distance;
//sensor pins
#define gasSens A4
dht DHT;
#define DHT11_PIN 2
//motor driver pins
#define in1 8
#define in2 7
#define in3 6
#define in4 5
//infared pins
#define lftIr 12
#define ctrIr 4
#define rghIr 3
#define buzzer 13
int dist=1000;
int lastVal=0;
int degVal;
int leftDist;
int rightDist;
int gasVal;
int logicVal=49;
int buzzerTrig=0;
unsigned long curTime;
void setup() {
// put your setup code here, to run once:
Servo1.attach(servoPin);
Servo1.write(100);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
pinMode(lftIr,INPUT);
pinMode(ctrIr,INPUT);
pinMode(rghIr,INPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
curTime=millis();
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available() > 0){ // Checks whether data is comming from the serial port
logicVal = Serial.read(); // Reads the data from the serial port
}
if(millis()-curTime>=10000){
int chk = DHT.read11(DHT11_PIN);
gasVal=analogRead(gasSens);
Serial.print(DHT.temperature);
Serial.print(" C");
Serial.print("|");
Serial.print(DHT.humidity);
Serial.print("%");
Serial.print("|");
Serial.print(gasVal);
Serial.print("|");
if(gasVal>500){Serial.println("Dangerous gas levels");buzzerTrig=1;}
else{Serial.println("normal");buzzerTrig=0;}
curTime=millis();
}
if(buzzerTrig==1){
buzzerCall();
logicVal==48;
}
if(logicVal=='1'){
dist=distanceCall();
int leftIr=digitalRead(lftIr);
int centerIr=digitalRead(ctrIr);
int rightIr=digitalRead(rghIr);
if (leftIr==LOW && rightIr==HIGH){
stopAll();
while(leftIr==LOW){
rightMov();
leftIr=digitalRead(lftIr);
}
stopAll();
}
else if (leftIr==HIGH && rightIr==LOW){
stopAll();
while(rightIr==LOW){
leftMov();
rightIr=digitalRead(rghIr);
}
stopAll();
}
else if (leftIr==LOW && centerIr==LOW && rightIr==LOW){
stopAll();
while(leftIr==LOW || centerIr==LOW || rightIr==LOW){
backMov;
leftIr=digitalRead(lftIr);
centerIr=digitalRead(ctrIr);
rightIr=digitalRead(rghIr);
}
stopAll();
}
else if (leftIr==HIGH && centerIr==LOW && rightIr==HIGH){
stopAll();
while(centerIr==LOW){
backMov;
centerIr=digitalRead(ctrIr);
}
stopAll();
delay(10);
leftMov();
delay(500);
stopAll();
}
else{
fowardMov();
}
}
}
int distanceCall(){
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
return distance;
}
void fowardMov(){
digitalWrite(in2,HIGH);
digitalWrite(in4,HIGH);
digitalWrite(in1, LOW);
digitalWrite(in3, LOW);
}
void backMov(){
digitalWrite(in1,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in2, LOW);
digitalWrite(in4, LOW);
}
void rightMov(){
digitalWrite(in1, HIGH);
digitalWrite(in4, HIGH);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
}
void leftMov(){
digitalWrite(in2, HIGH);
digitalWrite(in3, HIGH);
digitalWrite(in1, LOW);
digitalWrite(in4, LOW);
}
void stopAll(){
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void buzzerCall(){
tone(buzzer, 5000);
delay(500);
noTone(buzzer);
tone(buzzer, 1000);
delay(500);
noTone(buzzer);
}
The goal is for the robot to avoid obstacles and move autonomously using only the IR sensors. I used to power everything with the batteries but sometimes it shakes and moves slowly so i decided to power them seperately. Do you think the problem is with my code or it's a hardware problem? Thank you
logicVal==48;
... what is the purpose oflogicVal
? ... a more descriptive name would be useful ... assigning a numerical value and testing a character value tends to be confusingi move the obstacle away then the robot makes the decision it wanted to make
and, what decision would that be? .. you did not say anything about how you expect the robot to behave, and how it should react ... there is no way to know if parts of code actually are meant to do what they do