Need some help with coding!

This is a school project that I need help with.

I am trying to build a "Smart Home Hub" that displays time using RTC DS1307 and turn on LED using 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".

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


In what way doesn't it work now ?

What's going on here?

LEDs are buggy ... when i try to turn them on via a bluetooth connection thru an app, it sometimes doesnt light up and take multiple taps for it to on/off.

I have no clue on how to display the scrolling status of the LEDs onto the RGB matrix board. (E.g Bedroom light On/Off)

Its to use the Pin 5,6 & 7 as outputs to turn on my LED lights

The precise quoted code is relevant to the question. The name of your variable is not. Nor is what is connected to your board.

Hi sorry, im not really good as this kinda stuff do you mind elaborating a little?

Pins 5, 6 & 7 aren't used in your code. Kitchen, bedroom and living room leds all share pin 0 according to your sketch.
Float doesn't make sense for pin numbers.

That's not correct

I've assigned Pin 5,6 & 7 as outputs

 Bedroom_Light = 5;
  Kitchen_Light = 6;
  Living_Room_Light = 7;
  pinMode(Bedroom_Light,OUTPUT);
  pinMode(Kitchen_Light,OUTPUT);
  pinMode(Living_Room_Light,OUTPUT);

Or you could have been sensible

const byte Living_Room_Light = 7;
const byte  Kitchen_Light = 6;
const byte Bedroom_Light = 5;

As hinted, it makes NO sense using float to describe a pin number

1 Like

So, why does it have all that matrix stuff in it?

I'm also using a matrix board to display time using RTC DS1307 & LED status (Bedroom Light On/Off).

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