# Monster Shield+Hc 05

**URL:** <https://forum.fazedores.com/t/monster-shield-hc-05/6842>\
**Category:** Arduino\
**Created:** [Outubro 28, 2019, 5:02pm UTC](https://forum.fazedores.com/t/monster-shield-hc-05/6842 "2019-10-28T17:02:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hadduan](https://avatars.discourse-cdn.com/v4/letter/h/bcef8e/32.png) [@hadduan](https://forum.fazedores.com/u/hadduan)\
**Post date:** [Outubro 28, 2019, 5:02pm UTC](https://forum.fazedores.com/t/monster-shield-hc-05/6842/1 "2019-10-28T17:02:30Z")

</div>

Boa tarde,

Estou tendo dificuldade em um trabalho da escola, onde pretendo controlar um driver monster shield com comunicação via bluetooth.  
Cheguei a seguinte programação:

#define BRAKE 0  
#define CW 1  
#define CCW 2  
#define CS\_THRESHOLD 15

//MOTOR 1  
#define MOTOR\_A1\_PIN 7  
#define MOTOR\_B1\_PIN 8

//MOTOR 2  
#define MOTOR\_A2\_PIN 4  
#define MOTOR\_B2\_PIN 9

#define PWM\_MOTOR\_1 5  
#define PWM\_MOTOR\_2 6

#define CURRENT\_SEN\_1 A2  
#define CURRENT\_SEN\_2 A3

#define EN\_PIN\_1 A0  
#define EN\_PIN\_2 A1

#define MOTOR\_1 0  
#define MOTOR\_2 1  
#include \<SoftwareSerial.h\>  
#define RX 0  
#define TX 1  
SoftwareSerial bluetooth(RX, TX);

short usSpeed = 150; //default motor speed  
unsigned short usMotor\_Status = BRAKE;

void setup()  
{  
pinMode(MOTOR\_A1\_PIN, OUTPUT);  
pinMode(MOTOR\_B1\_PIN, OUTPUT);

pinMode(MOTOR\_A2\_PIN, OUTPUT);  
pinMode(MOTOR\_B2\_PIN, OUTPUT);

pinMode(PWM\_MOTOR\_1, OUTPUT);  
pinMode(PWM\_MOTOR\_2, OUTPUT);

pinMode(CURRENT\_SEN\_1, OUTPUT);  
pinMode(CURRENT\_SEN\_2, OUTPUT);

pinMode(EN\_PIN\_1, OUTPUT);  
pinMode(EN\_PIN\_2, OUTPUT);  
bluetooth.begin(9600);

Serial.begin(115200);  
Serial.println(“Begin motor control”);  
Serial.println(); //Print function list for user selection  
Serial.println(“Enter number for control option:”);  
Serial.println(“1. STOP”);  
Serial.println(“2. FORWARD”);  
Serial.println(“3. REVERSE”);  
Serial.println(“4. READ CURRENT”);  
Serial.println("+. INCREASE SPEED");  
Serial.println("-. DECREASE SPEED");  
Serial.println();

}

void loop (){  
verificaBluetooth();  
char user\_input = bluetooth.read();  
}

void verificaBluetooth(){  
if (bluetooth.available()){  
char user\_input = bluetooth.read();  
digitalWrite(EN\_PIN\_1, HIGH);  
digitalWrite(EN\_PIN\_2, HIGH);

```
if (user_input =='1')
{
   Stop();
}
else if(user_input =='2')
{
  Forward();
}
else if(user_input =='3')
{
  Reverse();
}
else if(user_input =='+')
{
  IncreaseSpeed();
}
else if(user_input =='-')
{
  DecreaseSpeed();
}
else
{
  Serial.println("Invalid option entered.");
}

```

}  
}

void Stop()  
{  
Serial.println(“Stop”);  
usMotor\_Status = BRAKE;  
motorGo(MOTOR\_1, usMotor\_Status, 0);  
motorGo(MOTOR\_2, usMotor\_Status, 0);  
}

void Forward()  
{  
Serial.println(“Forward”);  
usMotor\_Status = CW;  
motorGo(MOTOR\_1, usMotor\_Status, usSpeed);  
motorGo(MOTOR\_2, usMotor\_Status, usSpeed);  
}

void Reverse()  
{  
Serial.println(“Reverse”);  
usMotor\_Status = CCW;  
motorGo(MOTOR\_1, usMotor\_Status, usSpeed);  
motorGo(MOTOR\_2, usMotor\_Status, usSpeed);  
}

void IncreaseSpeed()  
{  
usSpeed = usSpeed + 10;  
if(usSpeed \> 255)  
{  
usSpeed = 255;  
}

Serial.print("Speed +: ");  
Serial.println(usSpeed);

motorGo(MOTOR\_1, usMotor\_Status, usSpeed);  
motorGo(MOTOR\_2, usMotor\_Status, usSpeed);  
}

void DecreaseSpeed()  
{  
usSpeed = usSpeed - 10;  
if(usSpeed \< 0)  
{  
usSpeed = 0;  
}

Serial.print("Speed -: ");  
Serial.println(usSpeed);

motorGo(MOTOR\_1, usMotor\_Status, usSpeed);  
motorGo(MOTOR\_2, usMotor\_Status, usSpeed);  
}

void motorGo(uint8\_t motor, uint8\_t direct, uint8\_t pwm)  
{  
if(motor == MOTOR\_1)  
{  
if(direct == CW)  
{  
digitalWrite(MOTOR\_A1\_PIN, LOW);  
digitalWrite(MOTOR\_B1\_PIN, HIGH);  
}  
else if(direct == CCW)  
{  
digitalWrite(MOTOR\_A1\_PIN, HIGH);  
digitalWrite(MOTOR\_B1\_PIN, LOW);  
}  
else  
{  
digitalWrite(MOTOR\_A1\_PIN, LOW);  
digitalWrite(MOTOR\_B1\_PIN, LOW);  
}

```
analogWrite(PWM_MOTOR_1, pwm); 

```

}  
else if(motor == MOTOR\_2)  
{  
if(direct == CW)  
{  
digitalWrite(MOTOR\_A2\_PIN, LOW);  
digitalWrite(MOTOR\_B2\_PIN, HIGH);  
}  
else if(direct == CCW)  
{  
digitalWrite(MOTOR\_A2\_PIN, HIGH);  
digitalWrite(MOTOR\_B2\_PIN, LOW);  
}  
else  
{  
digitalWrite(MOTOR\_A2\_PIN, LOW);  
digitalWrite(MOTOR\_B2\_PIN, LOW);  
}

```
analogWrite(PWM_MOTOR_2, pwm);

```

}  
}

É certo que parte dessa programação peguei fuçando na internet, mas na hora de colocar com bluetooth, não consigo rodar o motor, alguém poderia ajudar?

---

<div class="post-metadata">

**Author:** ![hadduan](https://avatars.discourse-cdn.com/v4/letter/h/bcef8e/32.png) [@hadduan](https://forum.fazedores.com/u/hadduan)\
**Post date:** [Outubro 28, 2019, 5:06pm UTC](https://forum.fazedores.com/t/monster-shield-hc-05/6842/2 "2019-10-28T17:06:05Z")

</div>

Estou usando o pino rx e tx para a comunicação.

---

<div class="post-metadata">

**Author:** ![Arnold\_Rudge](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.fazedores.com/arnold_rudge/32/1675_2.png) [@Arnold\_Rudge](https://forum.fazedores.com/u/Arnold_Rudge)\
**Post date:** [Outubro 28, 2019, 10:28pm UTC](https://forum.fazedores.com/t/monster-shield-hc-05/6842/3 "2019-10-28T22:28:54Z")

</div>

liga o tx e rx do bluetooth ao contrario rx-\>tx e tx-\>rx  
caso e muda - \>if (bluetooth.available()){ usa o proprio serial read

tipo isto

```
char incomingByte; // variable to receive data from the serial port
void loop() {

  if ( Serial.available() > 0 ) // if data is available to read
  {
    
    incomingByte = Serial.read(); // read it and store it in 'incomingByte'
    
    Serial.println(incomingByte);
  }

  if (incomingByte == 'D') //direita
  {
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    delay(300);
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);

  }

}
```
