I need some urgent coding/circuit guidance for my project!

Some brief background on my little project.
I am trying to build a "Smart Home Hub" that displays time using RTC DS1307 and HC-05 Bluetooth Module. The RGB Matrix Board is supposed to display the time and status of the LED lights.

Eg. If "Bedroom Light" is turned on, a scrolling message will appear on the RGB Matrix Board displaying "Bedroom Light On".

Right now, my codes for each individual circuit (RGB Board showing time) & (Turning on LED light via Bluetooth) works perfectly well. However, when combined together, the time does not display and LED lights are buggy and I have no clue how to display the scrolling status message.

I'd appreciate any help in getting my project to work. Thank you in advanced.


Here is my code for the combined circuit :

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <Adafruit_GFX.h>   // Core graphics library
#include <RGBmatrixPanel.h> // Hardware-specific library
#include <MD_DS1307.h>
#define CLK 11 
#define OE   9
#define LAT 10
#define A   A0
#define B   A1
#define C   A2
#define D   A3

RGBmatrixPanel matrix(A, B, C, D, CLK, LAT, OE, false, 64); // Global variables
#define MAX_MESG  4
#define MAX_MES  20
unsigned int NewRTCh = 24;
unsigned int NewRTCm = 60;
unsigned int NewRTCs = 60;
char szTime[12]; // mm:ss\0
char szMesg[MAX_MESG+1] = "";

float Living_Room_Light = 0;
float Kitchen_Light = 0;
float Bedroom_Light = 0;

SoftwareSerial Bluetooth(12, 13); // RX|TX
char Data;
void sendData(String transmitData){
Bluetooth.println(transmitData);}

void getTime(char *psz, bool f = true) // Code for reading time
{
  RTC.readTime();
  if (NewRTCh != RTC.h) 
  {
  sprintf(psz, "%02d", RTC.h);
  matrix.setCursor(1, 4); 
  matrix.setTextSize(2);
  matrix.setTextColor(matrix.Color333(7, 0, 0));
  matrix.fillRect(1, 4, 64, 14, matrix.Color333(0, 0, 0));
  matrix.print(szTime);
  
  sprintf(psz, "%02d", RTC.m);
  matrix.setCursor(27, 4); 
  matrix.setTextColor(matrix.Color333(7, 0, 0));
  matrix.fillRect(24, 4, 25, 14, matrix.Color333(0, 0, 0));
  matrix.print(szTime);
  
  uint8_t y = 0;
 
  NewRTCh=RTC.h;
  }
    else if (NewRTCm != RTC.m) 
  {
  sprintf(psz, "%02d", RTC.m);
  matrix.setCursor(27, 4); 
  matrix.setTextSize(2);
  matrix.setTextColor(matrix.Color333(7, 0, 0));
  matrix.fillRect(24, 4, 25, 14, matrix.Color333(0, 0, 0));
  matrix.print(szTime);
  
  matrix.setCursor(20, 4); 
  matrix.setTextColor(matrix.Color333(7, 0, 0));
  matrix.fillRect(24, 8, 2, 6, matrix.Color333(0, 0, 0));
  matrix.print(f ? ':' : ' ');
  
  matrix.setCursor(51, 9); 
  matrix.setTextSize(1);
  matrix.setTextColor(matrix.Color333(7, 0, 0));
  sprintf(psz, "%02d", RTC.s);
  
  matrix.fillRect(51, 9, 11, 7, matrix.Color333(0, 0, 0));
  matrix.print(szTime);
  
  NewRTCm=RTC.m;
  }
  else if (NewRTCs != RTC.s/10) 
  {
  matrix.setCursor(20, 4); 
  matrix.setTextSize(2);
  matrix.setTextColor(matrix.Color333(7, 0, 0));
  matrix.fillRect(24, 8, 2, 6, matrix.Color333(0, 0, 0));
  matrix.print(f ? ':' : ' ');
  
  matrix.setCursor(51, 9); 
  matrix.setTextSize(1);
  matrix.setTextColor(matrix.Color333(7, 0, 0));
  sprintf(psz, "%02d", RTC.s);
  
  matrix.fillRect(51, 9, 11, 7, matrix.Color333(0, 0, 0));
  matrix.print(szTime);
  
  NewRTCs=RTC.s/10;
  }
  else
  {
  matrix.setCursor(20, 4); 
  matrix.setTextSize(2);
  matrix.setTextColor(matrix.Color333(7, 0, 0));
  matrix.fillRect(24, 8, 2, 6, matrix.Color333(0, 0, 0));
  matrix.print(f ? ':' : ' ');
  
  matrix.setCursor(51, 9); 
  matrix.setTextSize(1);
  matrix.setTextColor(matrix.Color333(7, 0, 0));
  sprintf(psz, "%02d", RTC.s);
  matrix.fillRect(57, 9, 5, 7, matrix.Color333(0, 0, 0));
  matrix.print(szTime);
    }
  }

void delaySeconds (int seconds)
{
  delay(seconds*1000);
}

void setup() 
{
  Bluetooth.begin(9600);
  Bedroom_Light = 5;
  Kitchen_Light = 6;
  Living_Room_Light = 7;
  pinMode(Bedroom_Light,OUTPUT);
  pinMode(Kitchen_Light,OUTPUT);
  pinMode(Living_Room_Light,OUTPUT);
  
  matrix.begin();
  matrix.setTextWrap(false);
  RTC.control(DS1307_CLOCK_HALT, DS1307_OFF);
  RTC.control(DS1307_12H, DS1307_OFF);
}

void _loop() 
{
}

void loop() 
{
  _loop();
  {
  static uint32_t lastime = 0; // Millis() memory
  static bool flasher = false;  // Seconds passing flasher
  if (millis() - lastime >= 1000)
    {
    lastime = millis();
    getTime(szTime, flasher);
    flasher = !flasher;
    }
  }
      Data = Bluetooth.read(); 
      if(Data == '1')
      {
          digitalWrite(Bedroom_Light,HIGH);
          sendData("Bedroom Light On");
      }
      if(Data== '2')
      {
          digitalWrite(Bedroom_Light,LOW);
          sendData("Bedroom Light Off");
      }
      if(Data== '3')
      {
          digitalWrite(Kitchen_Light,HIGH);
          sendData("Kitchen Light On");
      } 
      if(Data == '4')
      {
          digitalWrite(Kitchen_Light,LOW);
          sendData("Kitchen Light Off");
      }    
      if(Data == '5')
      {
          digitalWrite(Living_Room_Light,HIGH);
          sendData("Living Room Light On");
      }     
      if(Data == '6')
      {
          digitalWrite(Living_Room_Light,LOW);
          sendData("Living Room Light Off");
      }
      if(Data == '7')
      {
          digitalWrite(Bedroom_Light,HIGH);
          digitalWrite(Kitchen_Light,HIGH);
          digitalWrite(Living_Room_Light,HIGH);
          sendData("All Lights On");
      }
      if(Data== '8')
      {
          digitalWrite(Bedroom_Light,LOW);
          digitalWrite(Kitchen_Light,LOW);
          digitalWrite(Living_Room_Light,LOW);
          sendData("All Lights Off");
      }
      _loop();
}

uint16_t Wheel(byte WheelPos) 
{
  if(WheelPos < 2) 
  {
   return matrix.Color333(7, 0, 0);
  } 
  else if(WheelPos < 5) 
  {
   WheelPos -= 2;
   return matrix.Color333(7 , 0, 0);
  } 
  else 
  {
   WheelPos -= 5;
   return matrix.Color333(7, 0, 0);
  }
}

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

The easier you make it to read and copy your code the more likely it is that you will get help

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

please post your code using </>

not clear why the code has a while (1) instead of using loop(). since loop() never gets a chance to run, getTime() is never invoked

why not

void
delaySeconds (int seconds)
{
    delay (seconds * 1000);
}

these unconventional approaches just make things more confusing

it would be better to see the "combined" code where others could help identify the problem

Hi @guestoflegend

Is this your project for personal use or school work?
Do you simply want to make it work or do you want to learn the technology?

RV mineirin

This is for school work, but I want to learn it too as it has been bugging me.

Hey firstly thanks for the reply, I'm new here and also the code i pasted above is the "combined" one.

I offer support for a not monetary payment: writing real honest feedback of 40 to 50 lines of text, including questions about things that are unclear.

I'm not the author of this tutorial but in my personal opinion I think this tutorial is good.
Though I can't think with somebody elses head.

Take a look into this tutorial:

Arduino Programming Course

Give it a try and report your real honest opinion about this tutorial.
If you think OMG this is ..... write it. If you thing wow ..... or if you think hm ..... write it.
I want to know your truth. As a reward I offer straight forward support on your project.
best regards Stefan

I stopped evaluating that link when I came across this, dunno if it is clogged with such nonsense…

In any event it looks more like a syllabus for a semesters long slog learning programming; I believe it would be appropriate for about 3 percent of the ppl who come here asking for help.

Even if it didn’t have this kinda stuff:

It is important to have the loop() function in the sketch, even if it is empty, because without it the microcontroller on the Arduino board will try to execute whatever it finds next in memory after the statements in the setup() function have been executed. The microcontroller will try to execute whatever it finds in memory as an instruction, but the loop() function prevents it from doing this by keeping program execution in the loop.

As always, YMMV. There are many different ways to learn, take a few minutes looking around for something better.

a7

1 Like

alto777 obviously does not like the way programming is explained in the tutorial I linked to in post #7.

alto777 you claim to know that 97% of all users looking for help in the arduino-forum prefer a different way of explaining programming.

So would you be kind enough to go beyond criticism and give a detailed description of this different way how programming can be explained better?

If your opinion is: there is no single way of doing this. Start listing up the different ways!
If you just write

If it takes only minutes for me - for you - it should take only seconds to post some links.
And I can assure you that I have searched not only minutes but hours for good explanations.
Maybe I'm just to dumb to search the right way so show some of your extraordinary expertness by posting some links.

You wrote me a private message with your opinion. I was asking back some quite specific questions like "do you think this part has too much text?"
You did not answer me.

So filling up a thread just with critic without writing a suggestion on how to make it better does not help.

So where is your contribution that will improving explanations or will help the TO?

If you keep on just writing generalised critics without doing real specific suggestions on how to do better my opinion about you will change to alto777 is just a troll.

You’re welcome.

a7

Hey thanks with the help of the loop() function i've moved this code into my loop() and it displays time and turns on led too. But the led is still sometimes unable to turn on ( tapped multiple times on my app before it lights up )

Data = Bluetooth.read(); 
      if(Data == '1')
      {
          digitalWrite(Bedroom_Light,HIGH);
          sendData("Bedroom Light On");
      }
      if(Data== '2')
      {
          digitalWrite(Bedroom_Light,LOW);
          sendData("Bedroom Light Off");
      }
      if(Data== '3')
      {
          digitalWrite(Kitchen_Light,HIGH);
          sendData("Kitchen Light On");
      } 
      if(Data == '4')
      {
          digitalWrite(Kitchen_Light,LOW);
          sendData("Kitchen Light Off");
      }    
      if(Data == '5')
      {
          digitalWrite(Living_Room_Light,HIGH);
          sendData("Living Room Light On");
      }     
      if(Data == '6')
      {
          digitalWrite(Living_Room_Light,LOW);
          sendData("Living Room Light Off");
      }
      if(Data == '7')
      {
          digitalWrite(Bedroom_Light,HIGH);
          digitalWrite(Kitchen_Light,HIGH);
          digitalWrite(Living_Room_Light,HIGH);
          sendData("All Lights On");
      }
      if(Data== '8')
      {
          digitalWrite(Bedroom_Light,LOW);
          digitalWrite(Kitchen_Light,LOW);
          digitalWrite(Living_Room_Light,LOW);
          sendData("All Lights Off");
      }