Arduino Uno and HC-05 car control

Hi everyone :slight_smile: my english is not perfect so sorry. I was interested of this video - YouTube and decided to start similar projet. I purchase Arduino Uno and oneHC-05 module.
Using MIT App Inventor I created an app.

The idea is:
when i press unlock on my mobile HC-05 to send "1" to arduino uno and to open relay 1. When release unlock HC-05 to send "2" and to close relay 1.
when i press lock on my mobile HC-05 to send "3" to arduino uno and to open relay 2. When release lock HC-05 to send "4" and to close relay 2.
when i press strar/stop on my mobile HC-05 to send "5" to arduino uno and to open relay 3. When release start/stop HC-05 to send "6" and to close relay 3.
when i press unlock on my mobile HC-05 to send "1" to arduino uno and to open relay 4. When press lock on my mobile HC-05 to send "3" and to close relay 4.

Here is code that I wrote:

#define Relay1 7
#define Relay2 9
#define Relay3 11
#define Relay4 13

void setup()
{
Serial.begin(9600);

digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay4, HIGH);

pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);

}

void loop()
{

int incomingByte = 0;

if (Serial.available() > 0) {

incomingByte = Serial.parseInt();
}
if (incomingByte == 1) {
digitalWrite(Relay1, HIGH);
digitalWrite(Relay4, HIGH);
}
if (incomingByte == 2) {
digitalWrite(Relay1, LOW);
}
if (incomingByte == 3) {
digitalWrite(Relay2, HIGH);
digitalWrite(Relay4, LOW);
}
if (incomingByte == 4) {
digitalWrite(Relay2, LOW);
}
if (incomingByte == 5) {
digitalWrite(Relay3, HIGH);
}
if (incomingByte == 6) {
digitalWrite(Relay3, LOW);
}

}

Is there any mistakes in code? I also want to add other relay 5 it need to be open when HC-05 send "5" and to stay open for 30seconds after that to close but don't have an idea how to write this :frowning:

I will keep in mind every sugestions

Just a few suggestions about code and style.

#1 The custom is to set the PinMode before writing to a pin.

#2. Sometimes an array is better than a series of similarly-named variables.

#3. The waterfall of IF statements might better be done with a SWITCH statement.

I forgot to say that I am not a programer :slight_smile: So please if i understand correct
#1. Needs to be :

void setup()
{
Serial.begin(9600);

pinMode(Relay1, OUTPUT);
pinMode(Relay2, OUTPUT);
pinMode(Relay3, OUTPUT);
pinMode(Relay4, OUTPUT);

digitalWrite(Relay1, HIGH);
digitalWrite(Relay2, HIGH);
digitalWrite(Relay3, HIGH);
digitalWrite(Relay4, HIGH);

}

But for #2 and #3 do not have an idea how needs to be so please write me some example.

But for #2 and #3 do not have an idea how needs to be so please write me some example.

  int RelayPins[] = {7, 9, 11, 13};
  for(byte b=0; b<sizeof(RelayPins) / sizeof(RelayPins[0]); b++)
  {
     pinMode(RelayPins[b], OUTPUT);
     digitalWrite(RelayPins[b], HIGH);
  }
   incomingByte = Serial.parseInt();

This is stupid. parseInt() returns a long, NOT a byte. Using a type (Byte) in the name of a variable that is of a different type (int) is stupid.

You can look up the switch statement, and learn how to use it, the same as you could have done a few seconds worth of research on arrays.

Thanks a lot . I am waiting for Arduino uno and HC05 to arrive and will test your sugestion . And can you help me how to switch relay 5 for 30seconds?

And can you help me how to switch relay 5 for 30seconds?

That depends. Do you need to do anything else during those 30 seconds?

If not, use delay(30000). If you do, then think about how you would turn the relay on at some random time, and turn it off 30 minutes later. What would you need to know? And, look at the blink without delay example.

Hello Bascho,

I forgot to say that I am not a programer :slight_smile:

I disagree. You are now a programmer and you just need to invest time and energy to become better at it. One reference that might help you is:

and this reference may help with the other things that you will need to learn. Each of the Arduino example programs may provide additional insight.

It may seem frustrating at times but please take this as an opportunity to learn and grow.

Thanks I will take some time to read and try ti learn it :slight_smile: .I hope to have a working remote soon and to show it herr

And finaly I am ready. With a little reading and some help from a friend I am ready.

First I change app for mobile phone to use only 3 cases to command 5 realys.

Here is the app:

First of all I change the name of HC-05 module, set it to work as a slave device and change pin code.
After that I uploaded this code to Arduino Uno.

void setup() {
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}

void loop() {

if(Serial.available()>0)
{
char data= Serial.read();
switch(data)
{
case '1': digitalWrite(8, HIGH);
delay(750);
digitalWrite(8, LOW);
digitalWrite(10, HIGH);
break;
case '2': digitalWrite(10, LOW);
delay(750);
digitalWrite(9, HIGH);
delay(750);
digitalWrite(9, LOW);

break;
case '3': digitalWrite(12, HIGH);
delay(750);
digitalWrite(11, HIGH);
delay(750);
digitalWrite(11, LOW);
delay(30000);
digitalWrite(12, LOW);

break;

default : break;
}
Serial.println(data);
}
delay(50);
}

Finaly connect HC-05 to Arduino Uno using this scheme :

Now everything is working fine. I am waiting better weather to make final wirring and connect it to my car but I tested it with diodes and I am shure that it will work.

p.s. The last picture is wrong but the wirring is the same resistors. Just I am using Arduino Uno
Rx of HC-05 -------------- Tx of Arduino Uno
Tx of HC-05 -------------- Rx of Arduino Uno
5V of HC-05 -------------- 5V of Arduino Uno
GND of HC-05 -------------- GND of Arduino Uno

Hi friends I have a new idea to upgrade the module. To extend communication range ss it possible to use ESP8266 module instead HC-05? Because I do not have ESP8266 I have a questions about it. Is it possible to assign IP address to ESP8266 and to send data to it directly from my mobile phone or it needs to be connected to a router?