Controlling a car + sensors by an Infrared Sensor

Hello, i start an Arduino UNO beginner project by controlling cars with along with a few sensors.
( ultrasound & humidty )
The connection has no problem.
The car body is running well with the remote
The code of ultrasound and humidity works well in void loop( ), but not well if i run them under IR remote

What i try to do : turn on/off ultrasound or humidity sensors and looping them when button 1/ button 2 is pressed. ( i used bool data type)
What i done : the car body able to moves, but when i pressed on the button 1/ button 2 responsible for the sensors, they only execute once and starts hangging there. It will restart it self after a few seconds :frowning:
Question : What is the problem on my code ?

My Code As Below
#define DECODE_NEC 1
#include <IRremote.h>
#include “DHT.h”
#define DECODE_NEC 1
#include <IRremote.h>

const int motor1pin1 = 2;
const int motor1pin2 = 3;
const int motor2pin1 = 4;
const int motor2pin2 = 5;
const int buzzer = 12;
const int triggerSource = 8;
const int echoRead = 9;
DHT myDht(10, DHT11);
int IR_RECEIVE_PIN = 11;
float cm;
float f1;
float h,t;
bool switchMode1 = false;
bool switchMode2 = false;

void setup()
{ Serial.begin(9600);
Serial.println(“Probe Starts”);
pinMode(motor1pin1,OUTPUT);
pinMode(motor1pin2,OUTPUT);
pinMode(motor2pin1,OUTPUT);
pinMode(motor2pin2,OUTPUT);
pinMode(buzzer,OUTPUT);
pinMode(triggerSource,OUTPUT);
pinMode(echoRead,INPUT);
myDht.begin();
tone(buzzer,987.8);delay(450);noTone(buzzer);
tone(buzzer,987.8);delay(450);noTone(buzzer);
tone(buzzer,987.8);delay(200);noTone(buzzer);
tone(buzzer,830.6);delay(200);noTone(buzzer);
tone(buzzer,1318.5);delay(500);noTone(buzzer);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
}

void loop()
{ if (IrReceiver.decode())
{ IrReceiver.resume(); // IR reciever reset
if (IrReceiver.decodedIRData.command == 0x52) //back
{ digitalWrite(motor1pin1,HIGH);
digitalWrite(motor1pin2,LOW);
digitalWrite(motor2pin1,LOW);
digitalWrite(motor2pin2,HIGH);
}
else if (IrReceiver.decodedIRData.command == 0x5A) // rotate right
{ digitalWrite(motor1pin1,HIGH);
digitalWrite(motor1pin2,LOW);
digitalWrite(motor2pin1,HIGH);
digitalWrite(motor2pin2,LOW);
}
else if (IrReceiver.decodedIRData.command == 0x18) // rotate right
{ digitalWrite(motor1pin1,LOW);
digitalWrite(motor1pin2,HIGH);
digitalWrite(motor2pin1,HIGH);
digitalWrite(motor2pin2,LOW);
}
else if (IrReceiver.decodedIRData.command == 0x8) // rotate left
{ digitalWrite(motor1pin1,LOW);
digitalWrite(motor1pin2,HIGH);
digitalWrite(motor2pin1,LOW);
digitalWrite(motor2pin2,HIGH);
}
else if (IrReceiver.decodedIRData.command == 0x1C)
{ digitalWrite(motor1pin1,LOW);
digitalWrite(motor1pin2,LOW);
digitalWrite(motor2pin1,LOW);
digitalWrite(motor2pin2,LOW);
}

// switch on/off for ultrasensor
else if (IrReceiver.decodedIRData.command == 0x45) // button 1
{ if (switchMode1 == false)
{switchMode1 = true;}
else if (switchMode1 == true)
{switchMode1 = false;}
ultrasound();
}
// switch on/off for humidity
else if (IrReceiver.decodedIRData.command == 0x46) // button 2
{ if (switchMode2 == false)
{switchMode2 = true;}
if (switchMode2 == true)
{switchMode2 = false;}
humidity();
}
}
}

// ultrasound sensor
void ultrasound()
{if (switchMode1 == true)
{ digitalWrite(triggerSource,LOW);
delayMicroseconds(2);
digitalWrite(triggerSource,HIGH);
delayMicroseconds(10);
digitalWrite(triggerSource,LOW);
cm = pulseIn(echoRead,HIGH)/58.0;
f1 = 4000-(10*cm);
tone(buzzer,f1);
delay(150);
noTone(buzzer);
Serial.print(cm);
Serial.print(“cm\n”);
}
else if (switchMode1 == false)
return;
}

//humidity sensor
void humidity()
{if (switchMode2 == true)
{ h = myDht.readHumidity();
t = myDht.readTemperature();
Serial.print(“Humidity = “);
Serial.print(h);
Serial.print(”%, Temperature = “);
Serial.print(t);
Serial.print(”'C\n”);
}
else if (switchMode2 == false)
return;
}

What components are you using in your car? Motor, motor driver, ultrasound sensor, IR remote/receiver etc.? How is it all powered?

Steve

else if (switchMode1 == false)If a Boolean variable isn’t true, there really isn’t any need to test to see if it is false.

And, since you’re going to return anyway, there’s no need for an else clause.

Please remember to use code tags when posting code

Hello guys I have found that whenever i add the passive buzzer, my arduino circuit could not work anymore, but they works super well when the passive buzzer is removed. Even my motor driver can operate, so it should not be an issue of overloading.
I think the passive buzzer have something to do with it.

The component used is :
Arduino UNO x1
L298N H-Bridge Motor Driver x1
6V DC TT Motor x2
UltraSensor x1
Humidity Sensor DHT11 x1
Mini bread board x1
Jumper Wires x22
IR sensor x1

I have change the code as :

/* Link ultrasound(),humidity(),stopDriving() */

#define DECODE_NEC 1
#include <IRremote.h>
#include "DHT.h"
int IR_RECEIVE_PIN = 11;
const int triggerSource = 8;
const int echoRead = 9;
DHT myDht(10, DHT11);
float cm;
float h,t;
float f1;
bool ultrasoundMode = false;
const int motor1pin1 = 2;
const int motor1pin2 = 3;
const int motor2pin1 = 4;
const int motor2pin2 = 5;
const int buzzer = 12;


void setup() 
{ Serial.begin(9600);
 Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
 IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK, USE_DEFAULT_FEEDBACK_LED_PIN);
 Serial.print(F("Ready to receive IR signals at pin "));
 Serial.println(IR_RECEIVE_PIN);
 myDht.begin();
 pinMode(8,OUTPUT);
 pinMode(9,INPUT);
 pinMode(motor1pin1,OUTPUT);
 pinMode(motor1pin2,OUTPUT);
 pinMode(motor2pin1,OUTPUT);
 pinMode(motor2pin2,OUTPUT);
 pinMode(buzzer,OUTPUT);
 /*
 tone(buzzer,987.8);delay(450);noTone(buzzer);
 tone(buzzer,987.8);delay(450);noTone(buzzer);
 tone(buzzer,987.8);delay(200);noTone(buzzer);
 tone(buzzer,830.6);delay(200);noTone(buzzer);
 tone(buzzer,1318.5);delay(500);noTone(buzzer);*/
}


void loop() 
{ if (IrReceiver.decode()) 
 { IrReceiver.printIRResultShort(&Serial);
   if (IrReceiver.decodedIRData.protocol == UNKNOWN) 
   { // We have an unknown protocol here, print more info
     IrReceiver.printIRResultRawFormatted(&Serial, true);
   }
   IrReceiver.resume(); // Enable receiving of the next value
   if (IrReceiver.decodedIRData.command == 0x45) 
   { Serial.println("Ultrasound Activate");
     ultrasoundMode = true;
     ultrasound();
     delay(1000);
   }
   else if (IrReceiver.decodedIRData.command == 0x46) 
   { Serial.println("Humidity Scan");
     humidity();
     delay(1500);
   }
   
   else if (IrReceiver.decodedIRData.command == 0x18)  //foward
   { digitalWrite(motor1pin1,LOW);
     digitalWrite(motor1pin2,HIGH);
     digitalWrite(motor2pin1,HIGH);
     digitalWrite(motor2pin2,LOW);
     delay(500);
     stopDriving();
   }
   else if (IrReceiver.decodedIRData.command == 0x52) //reverse
   { digitalWrite(motor1pin1,HIGH);
     digitalWrite(motor1pin2,LOW);
     digitalWrite(motor2pin1,LOW);
     digitalWrite(motor2pin2,HIGH);
     delay(500);
     stopDriving();
   }
   else if (IrReceiver.decodedIRData.command == 0x8) //left rotation 
   { digitalWrite(motor1pin1,LOW);
     digitalWrite(motor1pin2,HIGH);
     digitalWrite(motor2pin1,LOW);
     digitalWrite(motor2pin2,HIGH);
     delay(500);
     stopDriving();
   }
   else if (IrReceiver.decodedIRData.command == 0x5A) 
   { digitalWrite(motor1pin1,HIGH);
     digitalWrite(motor1pin2,LOW);
     digitalWrite(motor2pin1,HIGH);
     digitalWrite(motor2pin2,LOW);
     delay(500);
     stopDriving();
   }
 }
}


void ultrasound()
{ while (ultrasoundMode == true)
 { digitalWrite(triggerSource,LOW);
   delayMicroseconds(2);
   digitalWrite(triggerSource,HIGH);
   delayMicroseconds(10);
   digitalWrite(triggerSource,LOW);
   cm = pulseIn(echoRead,HIGH)/58.0;
   /*
   f1 = 4000-(10*cm);
   if(f1>0)
   {tone(buzzer,f1);delay(300);noTone(buzzer);}*/
   Serial.print(cm);
   Serial.print("cm\n\n");
   if (cm<10){stopDriving();} // Autostop
   delay(1500);
   if (IrReceiver.decode()) 
   { IrReceiver.printIRResultShort(&Serial);
   if (IrReceiver.decodedIRData.protocol == UNKNOWN) 
   { // We have an unknown protocol here, print more info
     IrReceiver.printIRResultRawFormatted(&Serial, true);
   }
   IrReceiver.resume(); // Enable receiving of the next value
   if (IrReceiver.decodedIRData.command == 0x45)
   { ultrasoundMode = false;
     Serial.println("Ultrasound Sensor Deactivate");
   }
   else if (IrReceiver.decodedIRData.command == 0x46) 
   { Serial.println("Humidity Scan");
     humidity();
     delay(1500);
   } 
   else if (IrReceiver.decodedIRData.command == 0x18)  // Self foward in Ultrasound Mode
   { digitalWrite(motor1pin1,LOW);
     digitalWrite(motor1pin2,HIGH);
     digitalWrite(motor2pin1,HIGH);
     digitalWrite(motor2pin2,LOW);
   }
   else if (IrReceiver.decodedIRData.command == 0x52) // Self reverse in Ultrasound Mode
   { digitalWrite(motor1pin1,HIGH);
     digitalWrite(motor1pin2,LOW);
     digitalWrite(motor2pin1,LOW);
     digitalWrite(motor2pin2,HIGH);
   }
   else if (IrReceiver.decodedIRData.command == 0x8) // Self left rotation in Ultrasound Mode

   { digitalWrite(motor1pin1,LOW);
     digitalWrite(motor1pin2,HIGH);
     digitalWrite(motor2pin1,LOW);
     digitalWrite(motor2pin2,HIGH);
     delay(500);
     stopDriving();
   }
   else if (IrReceiver.decodedIRData.command == 0x5A) // Self right rotation in Ultrasound Mode
   { digitalWrite(motor1pin1,HIGH);
     digitalWrite(motor1pin2,LOW);
     digitalWrite(motor2pin1,HIGH);
     digitalWrite(motor2pin2,LOW);
     delay(500);
     stopDriving();
   }
   else if (IrReceiver.decodedIRData.command == 0x1C) //Stop in Ultrasound Mode
   { stopDriving();}
 }
}
}


void humidity()
{ h = myDht.readHumidity();
 t = myDht.readTemperature();
 if (isnan(h) || isnan(t))
 { Serial.println(F("Temporary Failed, Please Retry"));
   return;
 }
 Serial.print("Humidity = ");
 Serial.print(h);
 Serial.print("%, Temperature = ");  
 Serial.print(t);
 Serial.print("'C\n\n");
}

void stopDriving()
{ digitalWrite(motor1pin1,LOW);
 digitalWrite(motor1pin2,LOW);
 digitalWrite(motor2pin1,LOW);
 digitalWrite(motor2pin2,LOW);
}

I comment out the buzzer codes as they are the code makes the project failed suddenly, sorry this is the first time i post so may i know what kind of codetag do i need to add ?

(Please use the code tags button </> on the menu and paste your code in the box that opens. Thanks, Moderator)

The ones that look like this [code] your code goes here[/code], so that the results look like this your code goes here

Thanks for the guidance, I tried to draw the schematic of my project, basically there are 3 circuits in it

  1. Buzzer circuit.
  2. Motor Driver to Motor circuit.
  3. Ultrasonic Sensor, Humidity Sensor and IR Sensor connected in parallel circuit.

My Problem Now : The program still cannot function if i include the buzzer code in it.
( Both of them works super smooth if I run them separately )

tone() takes an unsigned int for the frequency. You keep trying to pass floats to it. Have you tried it using
a valid data type?

Steve

The pulseIn() function in your "ultrasound()" function can take an optional timeout value.

Without it (as you've done), the function will default to a 1 second timeout and block code execution for that long if no echo is detected. For an HC-SR04 a timeout after 15 ms is consistent with the useful range of the sensor. That may or may not be the one and only issue, but it is a potential problem.

Edit: Looking more closely at your code, there are a lot of instances of delay() which also block execution. This is a very bad idea, see the Demonstration code for several things at the same time thread for how to code without "delay()".

MrMark:
The pulseIn() function in your “ultrasound()” function can take an optional timeout value.

Without it (as you’ve done), the function will default to a 1 second timeout and block code execution for that long if no echo is detected. For an HC-SR04 a timeout after 15 ms is consistent with the useful range of the sensor. That may or may not be the one and only issue, but it is a potential problem.

Edit: Looking more closely at your code, there are a lot of instances of delay() which also block execution. This is a very bad idea, see the Demonstration code for several things at the same time thread for how to code without “delay()”.

Wow thanks, this is a good idea

slipstick:
tone() takes an unsigned int for the frequency. You keep trying to pass floats to it. Have you tried it using
a valid data type?

Steve

The frequency of passive buzzer works just fine with float numbers, I also tried to change it into unsigned int but unforunately the circuit still cannot function when ever the passive buzzer is added into it. Even i just add this code on the void setup( ){ } stage.

tone(buzzer,987.8);delay(450);noTone(buzzer);
 tone(buzzer,987.8);delay(450);noTone(buzzer);
 tone(buzzer,987.8);delay(200);noTone(buzzer);
 tone(buzzer,830.6);delay(200);noTone(buzzer);
 tone(buzzer,1318.5);delay(500);noTone(buzzer);

( The buzzer circuit and other circuit works well when they are not combine together )
I believe its not a matter of overloading Arduino board because it also cannot works when i remove the motor driver but do not remove the buzzer circuit.
( I don't know why only the buzzer circuit is like a curse to others )

yew_qi_ming:
( I don’t know why only the buzzer circuit is like a curse to others )

It’s probably the “delay()” part of the buzzer code. “delay()” blocks execution of code until the end of the delay period making it unresponsive to other events.

The tone() function can take an optional “duration” argument which will play a tone for the specified duration and is, to my understanding, non-blocking.

tone(buzzer,987.8);delay(450);noTone(buzzer);

becomes

tone(buzzer,987,450) ;

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.