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
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;
}