Hc - 05 Bluetooth Modules disconnecting after starting my tm1637 display

Hi there people I'm new to arduino and coding so bare with. Got a question regarding frequency alteration on the NANO (non genuine) . I have been making an electronic timer recently but I've come across this road block in my progress and thought that the best course of action will be to post my problem here.

On one arduino I have the transmitter with push buttons that I used. It sends the data to the receiver and it displays the value on the tm1637 display. It will show the amount of time in minuets that you want to use. (num) Another button on the transmitter will start the time and another will reset it.

I am using two arduino mega2560 boards with two more hc - 05 bluetooth modules with the tm1637 connected to one of the megas. I have followed [Transmitter and Receiver for multiple data ] (SEND RECEIVE MULTIPLE DATA - #21 by GolamMostafa) this guide that is at the very bottom of the forum that is a Connection diagram between UNO and NANO using Soft and Hard UART Link.

The problem that I am running into is that whenever I start the timer from the transmitter, the display starts counting down properly with the time that I wanted. But then the hc 05 modules disconnect all serial communication and then the timer no longer works.

#include <TM1637Display.h>

bool flag = false;          //Frame synchronizer has not come
byte frameArray[21];
byte potMapRxArray[5];
byte x, y, y1;
int i = 0;
int j = 0;

const int CLK = 2; //Set the CLK pin connection to the display
const int DIO = 3; //Set the DIO pin connection to the display

unsigned long startTime; //variables for the clock 
unsigned long currentTime;
unsigned long elapsedTime;

const long interval;  // Toggle interval in milliseconds
unsigned long previousMillis = 0;

TM1637Display display(CLK, DIO); //set up the 4-Digit Display.

unsigned int num = 10; // starting time 

void setup()
{
  Serial.begin(9600);             // Serial port to computer
 // for (int i=0, j=8; i<5, j<13; i++, j++)
 // {
 //   myservo[i].attach(j);
 // }
  pinMode(10,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(4,OUTPUT);
  display.setBrightness(2); //set the diplay to brightness
}
void loop()
{
  if (i == 21) //complete frame is received
  {
    buildpotMapRxArray();  //nuumber in: z, p, and myData[] of union{}
    updown();
    clk();
    i = 0;
    flag = false;
    i = 0;
    j = 0;
  }
}

void buildpotMapRxArray()
{
  //cli();
  Serial.println();//write(frameArray[18]);
  // HERE: goto HERE;

  for (i = 9, j = 0; i < 19, j < 5; i++, j++)
  {
    if (potMapRxArray[2] == 0xB4) {
      digitalWrite(6,HIGH);
    }
    if (potMapRxArray[0] == 0xB4) {
      digitalWrite(4,HIGH);
    }
    else {
      digitalWrite(4,LOW);
    }
    y = frameArray[i];
    if (y < 0x41)
    {
      y = y & 0x0F;
      y = y << 4;
    }
    else
    {
      y = y - 0x37;
      y = y << 4;
    }
    // Serial.println(y, HEX);
    //-------------------
    i = i + 1; //i++;
    y1 = frameArray[i];
    if (y1 < 0x41)
    {
      y1 = y1 & 0x0F;

    }
    else
    {
      y1 = y1 - 0x37;

    }
    //------------------------
    y = y | y1;
    potMapRxArray[j] = y;
  }
  Serial.println(potMapRxArray[0], HEX);
  Serial.println(potMapRxArray[1], HEX);
  Serial.println(potMapRxArray[2], HEX);
  Serial.println(potMapRxArray[3], HEX);
  Serial.println(potMapRxArray[4], HEX);
  Serial.println("======================");
}


void serialEvent()
{
  if (flag == true)
  {
    while (Serial.available())
    { //Serial Port has data
      x = Serial.read();
      Serial.write(x);     // Send the data to Serial monitor
      frameArray[i] = x;    //save in array
      i++;
    }
  }

  else
  {
    byte s = Serial.read();
    if ( s != 0x3A)
    {
      flag = false;
    }
    else
    {
      if ( s == 0x3A)
      {
        Serial.write(s);
        flag = true;
        i++;
      }
    }

  }
}

void(* resetFunc) (void) = 0; //declare reset function @ address 0

void updown() {
 // this void increases the amount of time that you want to set, checking potMapRxArray[4] (subtract) and potMapRxArray[3] (add) 
 display.showNumberDec(num, true, 4, 0);
    if (potMapRxArray[3] == 0xB4) {
      digitalWrite(10,HIGH);
      num++;         // increment 'num'
        if(num > 9999)
          num = 0;
    }
    else {
      digitalWrite(10,LOW);
    }
    if (potMapRxArray[4] == 0xB4) {
      digitalWrite(8,HIGH);
      num--;     // decrement 'num'
        if(num < 0)
          num = 9999;
    }
    else {
      digitalWrite(8,LOW);
    }
}


void clk() {
 //this void checks if the potMapRxArray[2] is equal to 0xB4 and starts the timer on the tm1637 
 if (potMapRxArray[2] == 0xB4) {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    digitalWrite(6,HIGH);
    previousMillis = currentMillis;
    num = num * 60;
    // Button is pressed, start countdown
    startTime = millis(); // Record the starting time

    while (true) {
      currentTime = millis(); // Get the current time
      elapsedTime = (currentTime - startTime) / 1000; // Calculate elapsed time in seconds

      if (elapsedTime <= num) {
        unsigned long remainingTime = num - elapsedTime;
        unsigned int minutes = remainingTime / 60;
        unsigned int seconds = remainingTime % 60;
        display.showNumberDecEx(minutes * 100 + seconds, 0b01000000, true);
        if (remainingTime == 0) {
          // Start blinking when countdown reaches 00:00
          while (true) {
            display.showNumberDecEx(0, 0b01000000, true); // Display "00:00"
            delay(500);
            display.clear(); // Clear the display
            delay(500);
            if (digitalRead(10) == HIGH) {
              resetFunc();  //call reset
            }
          }
        }
      }
    }
  }
 }
}

In your post #1, you have the following devices. It is not clear to me which device is connected with whom and their purposes. Please, post a hand drwan connection diagram among the devices along with a textual description.

NANO
MEGA-1
MEGA-2
Blutooth-1
Bluetooth-2
TM1367 Display Module
Push Button-1
push Button-2
Push Button-3

Here is my hand drawn circuit diagram to my master and slave.
The first button adds time,
The second button subtracts time,
The third button starts the time,
The last button restarts the time.

// on the master 
the four pushbuttons all connect to A0-A3 respectively, and they are connected with vcc and gnd 
there is a 1000k resistor for each pushbutton 
the master hc - 05 rx/tx connects to 6,7 respectively, and it is connected with vcc and gnd 

// on the slave 
the hc - 05 rx/tx is 1,0 respectively, and it is connected with vcc and gnd 
the tm1637 display's CLK and DIO are connected to 2,3 respectively, it id connected with vcc and gnd 

What I want to do is add time with the first pushbutton, subtract time with the second pushbutton, the amount of time will be displayed with the amount of minuets on the tm1637 display. Then the 3rd button will start the countdown timer. And the fourth will restart the time. ON the slave - The code to start the timer is in the clk viod. To add and subtract time is in the updown viod. They both check for the number 0XB4 and start.

I will relay this again but when I start the timer with the third button the serial communication stops completely between the hc - 05 bluetooth modules.

The code to the master is here:

#include<SoftwareSerial.h>
SoftwareSerial mySerialBTUNO(6, 7);  //SRX = DPin-6, STX = DPin-7

byte potMapTxArray[5];
unsigned long prmillis = 0;
byte frameArray[11];   //1(: START) + 1 (Information bte) + 2 (address) + 1 (EOF) + 5(inf) + 1(CHKSUM)  

void setup()
{
  Serial.begin(9600);   //Serial Monitor-1 is ON
  mySerialBTUNO.begin(9600);
  analogReference(DEFAULT);   //5V is the Full scale of ADC
}

void loop()
{
  do
  {
    ;
  }
  while (millis() - prmillis < 20);
  prmillis = millis();
  //---------------------
  for (int i = 0, j = 14; i < 5, j < 19; i++, j++)
  {
    potMapTxArray[i] = (byte)map(analogRead(j), 0, 1023, 0, 180);
    Serial.println(potMapTxArray[i], HEX);   //printing mapped values on Serial Monitor---
  }
  

  //--Sending mapped values of PM0 - PM1 (potArra[5]) to NANO---
  //using
  buildFrame();
  sendFrame();
  Serial.println("=============================");

}

void buildFrame()
{
  // byte *ptr = (byte*) &x; //decl. of pointer var. for binary32 formatted value of a flp number

  frameArray[0] = 0x3A;   //: statrt of a frame

  frameArray[1] = 05; //number of information byte = number of bytes for floating point number

  frameArray[2] = 0x10;   //lower byte of the buffer address: 0x0010
  frameArray[3] = 0x00;   //upper byte of the buffer address: 0x0010

  frameArray[4] = 0x00; //code to mark end-of-file (0x01 = EOF) when sending data from file
  //-------------------

  for (int i = 5, j = 0; i < 10, j < 5; i++, j++) //saving 4-byte binary32 vale for a flp number
  {
    frameArray[i] =  potMapTxArray[j];
  }

  frameArray[10] = chkSum();         //creating normal sum based CHKSUM for error control

}

byte chkSum()  //CHKSUM algorithm: all bytes offrame except :; discard carry; take 2's Compl.
{
  byte sum = 0;
  for (int k = 1; k < 10 ; k++)
  {
    sum += frameArray[k];
  }
  sum = ~sum;
  sum++;
  return sum;
}
//-------------------------------------------------------
void sendFrame()                //conver digit to ASCII and write to HC-12 for transmission
{
   mySerialBTUNO.write(frameArray[0]);     //always send the synchronizer marker (:) as binary code
   Serial.print((char)frameArray[0]);    //show the character on Hard Serial Monitor
  
  for(int i = 1; i<11; i++)
  {
    byte x = frameArray[i];
//    Serial.print(frameArray[i]);
    byte x1 = x;
    x1 = x1>>4;
    if (x1 <=9)
    {
      x1 = x1+0x30;             
      mySerialBTUNO.write(x1);      //transmit: 0x30 - 0x39 for 0 - 9 for the 1st digit of a byte
       Serial.write(x1);
       //HERE: goto HERE;
    }
    else
    {
      mySerialBTUNO.write(x1+0x37);  //transmit: 0x41 - 0x46 for A - F for the 1st digit of a byte
       Serial.write(x1+0x37);
    }
  //--------------------------------
    x = frameArray[i];
    x = x & 0x0F;
    if (x <=9)
    {
      x = x+0x30;
      mySerialBTUNO.write(x);      //transmit: 0x30 - 0x39 for 0 - 9 for the 2nd digit of a byte
       Serial.write(x);
    }
    else
    {
      mySerialBTUNO.write(x+0x37);  //transmit: 0x41 - 0x46 for 0=A - F for the 2nd digit of a byte
       Serial.write(x+0x37);
    }
    
  } 
  Serial.println();    //enter new line 
}
2 Likes

Kindly answer to my questions:

1. At startup, what message/value will appear on the Display Unit connected with Slave? If the displayed value is time, then what is the format: MIN:SEC? Is the initial value is: 00:00 (assume you have four-digit display unit).

2. If AddButton is pressed, is it going to add 1-sec with the present displayed value of the Display Unit?

3. If SubButton is pressed, is it going to deduct 1-sec with the present displayed value of the Display Unit?

4. If StartButton is pressed, is it going tell the Slave to start a timer and shows the value on the Display Unit?

5. If RestartButton is pressed, is it going to instruct the Slave to begin time counting from 00:00 and to show it on the Display Unit?

6. Are your Bluetooth Modules working correctly in Master-Slave configuration?

7. I would recommend to use hardware UART Ports (UART1 or UART2 or UART3) Ports for the Bluetooth Modules. Software UART Ports' Bd is limited to only 9600.

8. Your buttons are just On/Off switches; why have you not connected them with digital IO lines with internal-pull resistors?

9. The RX-pin of the HC-05 Bluetooth is not 5V tolerant; so, use a voltage diviser (Fig-1) to bring down the 5.0V HIGH level to 3.3V HIGH level.
hc05-UNO

Figure1:

Thank you GolamMostafa for being patient with me. I will answer your questions that you have taken the time to ask. I have uploaded a video to youtube that will answer most of your questions, but I will still answer them here for your convenience. (https://www.youtube.com/watch?v=dAVOLUJDrEA)

  1. The Displayed value at the start is "10" for 10 minuets. In the slave code the variable "num" is the first displayed value that is added and subtracted.

  2. When the AddButton is pressed (A1) it will add 1 minute to the displayed value

  3. When the SubButton is pressed (A2) it will subtract 1 minute from the displayed value

  4. Yes when StartButton is pressed (A0) is will take the displayed value from before and start counting down. For example if we subtract the displayed value down to 8, then press the StartButton it will count down from 8 minuets.

  5. No when the RestartButton is pressed (A3) is will reset the add/subtract value back to 10 and restart the program.

  6. Yes they are communicating properly but maybe using the UART ports will be a good start.

  7. I am not using those but only on the slave. On the master I am using pins 6 and 7 for my rx and tx communication because that is how the example did it that I referenced in the original post. If you think this is a reason for the disconnection please let me know. I will try and use the rx and tx pins in the future.

  8. The resistors are acting as pulldown resistors for my pushbuttons, so that it remains LOW when it is released and HIGH when pressed down. Reference my video for a demonstration.

  9. Yes I have seen that voltage divider before but I heard in a youtube video that it will work without the divider and so far I have communication between the two modules.

At the end of the youtube video you can see that the serial communication between the two modules stops when I press the StartButton and start the timer. This is the roadblock that I cannot solve.

Let me elaborate more of the serial communication, it will type the following into the serial port:

:051000000C10000000CF
C
10
0
0
0
======================

The intel hex frame is at the top.
Then it displays each frame arrays underneath of it. There are 5 of them but I am only using 4.
The equals at the bottom is to divide each frame.

For example if the AddButton is pressed the frame will look like this, the second to last array is changed to B4. And if the SubButton is pressed then the very last array will show as B4.

In the slave code I will have an if statement checking to see if potMapRxArray[3] == 0xB4 which is the add button. In the SubButton it is potMapRxArray[4] == 0xB4. (Located in the updown viod) Lastly the StartButton is located in the clk viod, potMapRxArray[2] == 0xB4, and will start the timer.

:051000000C10000000CF
C
10
0
B4
0
======================

I have just realized that you were the person that posted the example that I am using hehe lol.

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