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