Code not working when I want it to

Hello I have been trying to do this circuit for a while and I have been having some issues, mainly my code when I start everything is working well but when i try to press this pushbutton and asking hc06 to give me info at the same time it does not work, is it because of how i a wrote the code? I'm not sure how to make everything work independately with another, i tried researching a bit but to no avail can someone help?
To try and explain a bit how i wanted the circuit to go it would be something like, when the circuit starts i want the ledG and ledB high and the I2C displaying a small message, (which is working fine), if I send a message from my phone to the bluetooth terminal with the '1' character it would display the current status of my circuit (in this case the ledG and ledB, represent a combination), then if i press the pushbuttonB it would do a countdown and then change the information in the I2C and turn off and on different leds, but when i try to get information from the hc06 even when i send the information it does not work and i don't receive any feedback from the hc06.

#include <SoftwareSerial.h>
SoftwareSerial BT(0, 1); 
#include <LiquidCrystal_I2C.h>
#include <Wire.h> // funçoes de comunicaçao do i2c
#include "Arduino.h"
#define buzzer 7
const int ledR = 13; // ledVermelho conectado ao pino 13 e ao GND
const int ledG = 12; // ledVerde conectado ao pino 13 e ao GND
const int ledY = 11; // ledAmarelo conectado ao pino 13 e ao GND
const int ledB = 9;   // LedVerdePeao conectado ao pino 13 e ao GND
const int ledB2 = 10; // LedVermelhoPeao conectado ao pino 13 e ao GND
int ldr = 0;
int buttonA = 2;
int buttonB = 4;
const int MotorA = 4;
const int MotorB = 5;
const int en = 2, rw = 1, rs = 0, d4 = 4, d5 = 5, d6 = 6, d7 = 7, bl = 3; // pinouts do i2c
const int i2c_addr = 0x27; // IP(?) HEXADECIMAL DO I2C
LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE);
//bluetooth presets

void setup() {
  lcd.begin(16, 2);
  pinMode(ledR, OUTPUT);
  pinMode(ledG, OUTPUT);
  pinMode(ledY, OUTPUT);
  pinMode(ledB, OUTPUT);
  pinMode(ledB2, OUTPUT);
  pinMode(A0, INPUT);
  pinMode(buttonA, INPUT);
  pinMode(buttonB, INPUT);
  pinMode(MotorA, OUTPUT);
  pinMode(MotorB, OUTPUT);
  pinMode(buzzer, OUTPUT);
  // define pin modes for tx, rx:
  pinMode(0, INPUT);
  pinMode(1, OUTPUT);
  // set the data rate for the SoftwareSerial port
  BT.begin(9600);
  // Send test message to other device
  BT.println("Pressione '?' para ver comandos disponiveis ");
}
char a; // stores incoming character from other device

void loop() {
  ldr = analogRead(A0);

    //loop do bluetooth
  if (BT.available())
  // if text arrived in from BT serial...
  {
    a=(BT.read());
    if (a=='1')
    {
      if (ldr < 200) {
        BT.println(ldr);
        BT.println("De momento o semaforo está em modo intermitente.");
        delay(10);
        BT.println("Conduza com cuidado!");
      }
      if (ldr > 200) {
        if (digitalRead(ledG)== HIGH) {
        BT.println("Passagem dos carros autorizada");
        delay(10);
        BT.println("lomba abaixada");
        }
      }
      if (ldr > 200) {
        if (digitalRead(ledB2)== HIGH){
          BT.println("Passagem dos peos autorizada");
          delay(10);
          BT.println("lomba alevantada");
        }
      }
    }
    if (a=='0') {
      BT.println("LDR VALUE IS");
      BT.println(ldr);
    }
    if (a=='?')
    {
      BT.println("Envie '0' para saber valor do LDR");
      delay(10);
      BT.println("Envie '1' para saber o estado do semaforo");
    }   
    // add if commands here if needed
  }
  // parte fora do bluetooth
  if (digitalRead(buttonA) == LOW) {
    delay(500);
    digitalWrite(MotorA, LOW);
    digitalWrite(MotorB, LOW);
    noTone(buzzer);
  }
  if (ldr > 200) {
  if (digitalRead(buttonB) == HIGH) {
    lcd.clear();
    delay(10);
    lcd.setCursor(2,0);
    lcd.println(" Passagem de    ");
    lcd.setCursor(0,1);
    lcd.println("    peoes em  5 ");
    lcd.setCursor(14,1);
    lcd.println("4 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("3 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("2 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("1 ");
    delay(500);
    lcd.setBacklight(1);
    digitalWrite(ledR, LOW);
    digitalWrite(ledG, LOW);
    digitalWrite(ledY, HIGH);
    digitalWrite(ledB, HIGH);
    digitalWrite(ledB2, LOW);
    digitalWrite(MotorA, LOW);
    digitalWrite(MotorB, LOW);
    tone(buzzer, 600,500);
    lcd.setCursor(14,1);
    lcd.println("0 ");
    delay(750);
    digitalWrite(ledR, LOW);
    digitalWrite(ledG, LOW);
    digitalWrite(ledY, LOW);
    digitalWrite(ledB, LOW);
    digitalWrite(ledB2, LOW);
    digitalWrite(MotorA, LOW);
    digitalWrite(MotorB, LOW);
    digitalWrite(MotorA, HIGH); // ABRIR LOMBA 
    digitalWrite(MotorB, LOW); // PARA FECHAR
    delay(750);
    digitalWrite(ledR, LOW);
    digitalWrite(ledG, LOW);
    digitalWrite(ledY, HIGH);
    digitalWrite(ledB, HIGH);
    digitalWrite(ledB2, LOW);
    digitalWrite(MotorA, LOW);
    digitalWrite(MotorB, LOW);
    tone(buzzer, 600,500);
    delay(750);
    digitalWrite(ledR, LOW);
    digitalWrite(ledG, LOW);
    digitalWrite(ledY, LOW);
    digitalWrite(ledB, LOW);
    digitalWrite(ledB2, LOW);
    digitalWrite(MotorA, LOW);
    digitalWrite(MotorB, LOW);
    delay(750);
    digitalWrite(ledR, LOW);
    digitalWrite(ledG, LOW);
    digitalWrite(ledY, HIGH);
    digitalWrite(ledB, HIGH);
    digitalWrite(ledB2, LOW);
    digitalWrite(MotorA, LOW);
    digitalWrite(MotorB, LOW);
    tone(buzzer, 600,500);
    delay(750);
    digitalWrite(ledR, HIGH);
    digitalWrite(ledG, LOW);
    digitalWrite(ledY, LOW);
    digitalWrite(ledB, LOW);
    digitalWrite(ledB2, HIGH);
    digitalWrite(MotorA, LOW);
    digitalWrite(MotorB, HIGH);
    digitalWrite(MotorA, LOW); // A LOMBA ESTA ABERTA
    digitalWrite(MotorB, LOW); // PARA FECHAR
    lcd.clear();
    delay(10);
    lcd.setCursor(2,0);
    lcd.println("Passagem dos  ");
    lcd.setCursor(0,1);
    lcd.println("   carros em     ");    
    lcd.setCursor(14,1);
    lcd.println("10 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("9 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("8 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("7 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("6 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("5 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("4 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("3 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("2 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("1 ");
    delay(1000);
    lcd.setCursor(14,1);
    lcd.println("0 ");
    delay(500);
    lcd.clear();
    digitalWrite(MotorA, LOW); // ABRIR LOMBA 
    digitalWrite(MotorB, HIGH); // FECHAR LOMBA
    digitalWrite(ledR, LOW);
    digitalWrite(ledG, HIGH);
    digitalWrite(ledY, LOW);
   	digitalWrite(ledB, HIGH);
    digitalWrite(ledB2, LOW);
    digitalWrite(MotorA, HIGH);
    digitalWrite(MotorB, LOW);
    lcd.setCursor(0,0);
    lcd.println("Passagem  carros");
    lcd.setCursor(0,1);
    lcd.println("   Autorizada   ");
    delay(500);
    }
  }
    if (ldr > 200) {
      if (digitalRead(buttonB) == LOW) {
      lcd.setBacklight(1);
      lcd.clear();
      delay(10);
    	digitalWrite(ledR, LOW);
    	digitalWrite(ledG, HIGH);
    	digitalWrite(ledY, LOW);
   		digitalWrite(ledB, HIGH);
    	digitalWrite(ledB2, LOW);
    	digitalWrite(MotorA, LOW);
    	digitalWrite(MotorB, LOW);
      lcd.setCursor(0,0);
      lcd.println("Passagem  carros");
      lcd.setCursor(0,1);
      lcd.println("   Autorizada   ");
    }
  }
    if (ldr < 200) {
    lcd.setBacklight(1);
    lcd.clear();
    delay(10);
    digitalWrite(ledR, LOW);
    digitalWrite(ledG, HIGH);
    digitalWrite(ledY, LOW);
    digitalWrite(ledB, LOW);
    digitalWrite(ledB2, LOW);
    digitalWrite(MotorA, LOW);
    digitalWrite(MotorB, LOW);
    lcd.setCursor(0,0);
    lcd.println("Semaforo em modo");
    lcd.setCursor(2,1);
    lcd.println("Intermitente  ");
    delay(1000);
  }
}    

Hi!
I didn´t dive into every detail of your code, but pins 0 and 1 are the hardware serial of a Uno (are you using an Uno R3?). If you keep the comms in that pins you don´t need software serial. In the other hand you´ll have proplems to upload your code with the HC-06 on.

My first suggestions are:

  • Change the software serial to other pins and check if the sketch works.
  • Verify if the HC-06 baud rate is also set at 9600.

I´m presuming you had succesfully paired and connected your phone with the HC-06 before sending or receiving commands.

You did not mention your wiring, so I will ask if you have it wired correctly. The HC-06 might use 3v3 logic on the RX pin while Arduino uses 5v logic on the TX pin, so a voltage divider or a level converter might be needed between the Arduino TX and HC-06 RX pins.

Hi sorry for not responding earlier i was still thinking about it and forgot i made a post.

I noticed this when trying to upload the sketch and i simply disconnected the rx 0 pin and let it upload before reconnecting the hc06 is there any trouble with this? If so i guess i could change it since i already added the softwareserial library

I´ve never tried, but it´s not recommended and it´s a high cost to pay, as you´re making your program use more memory than it would be needed and losing the opportunity to keep debugging it on the Serial Monitor. Why not simply choose other 2 pins and change the line below accordingly?

SoftwareSerial BT(0, 1); 

Also, @xfpd is right. You should be aware of the difference in logic level between the HC-06 and the Arduino you are using (which you haven't said yet...)

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