Bluetooth 2 Mega 2560 Cards / LDR and LED

Folks, I have a project I have been working on. I was able to attach a LED (pin13) and LDR (pinA) on the same board and make it work properly - when light is on LDR, LED is off and vice versa. Now I am trying to place the LED on one MEGA card and the LDR on a separate MEGA card and have them communicate through Bluetooth module that will be connected to the 5v, GND, RX and TX pins on both cards. The code for the Bluetooth modules will be completed by using AT commands in the COM1 window setting one to master and the other to slave. Below I have submitted the code I have been trying but am confused. I also submitted a photo of the schematic.

LED CARD

const int ledPin = 13;

void setup() {
   Serial.begin(38400); //My understanding sets the baud rate
   pinMode(ledPin, OUTPUT); //This should initialize LED as the OUTPUT
}

void loop() {
   if (ldrStatus <= 300) {
      digitalWrite(ledPin, HIGH); //Turns LED on
      Serial.println("LDR is DARK, LED is ON"); //Writes out this message in window
}
   esle {
      digitalWrite(ledPin, LOW); //Turns LED off
      Serial.println("OFF"); //Writes out this message in window
}

LDR CARD

const int ldrPin = A0;

void setup() {
   Serial.begin(38400); //My understanding sets the baud rate
   pinMode(ldrPin, INPUT); //This should initialize LDR as the INPUT
}

void loop() {
   int ldrStatus = analogRead(ldrPin); //Reads the status of the LDR value

I am confused as to where I need to go from here. I understand the Bluetooth modules are just connecting the two cards together. I am a week old when it comes to Arduino. Again, I can make it all work on a single card but not sure what I do when I use two. Any assistance is greatly appreciates.

Hi there happy new year to you.

See before going for the coding you have to first configure your HC05 in master and slave mode.

If you need any help feel free to contact me on

techtidda@gmail.com

I have the Bluetooth Modules linked together now and ready to move on. When I was able to make this work on a single board, this is the code I used:

//set pin numbers
//const won't change
const int ledPin = 13;   //the number of the LED pin
const int ldrPin = A0;  //the number of the LDR pin


void setup() {

  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as an output
  pinMode(ldrPin, INPUT);   //initialize the LDR pin as an input
}

void loop() {

  int ldrStatus = analogRead(ldrPin);   //read the status of the LDR value

  //check if the LDR status is <= 300
  //if it is, the LED is HIGH

   if (ldrStatus <=300) {

    digitalWrite(ledPin, HIGH);               //turn LED on
    Serial.println("LDR is DARK, LED is ON");
    
   }
  else {

    digitalWrite(ledPin, LOW);          //turn LED off
    Serial.println("---------------");
  }


}

Obviously my goal now is to accomplish the same thing but to have the LDR on the board with the Master Module, and the LED on the board with the Slave Module. The code I posted the other day were my thoughts on how to get there but very uncertain. Any assistance will be greatly appreciated. Thank you for your time.

Any assistance will be greatly appreciated.

What data do you want to send from the master to the slave? How are the bluetooth devices connected to the Arduinos? Have you written any code to verify that the two devices actually pair and can communicate?

Not knowing much about this, I know that my goal is to have the ldr receive light and stay off and once it goes dark, the led on the 2nd board should light up.

I am thinking on the LDR Board I need to identify the ldrPin, I need to under setup set pinMode as INPUT and Serial.begin with the baud rate of 38400. For the loop I believe I need something with the ldrStaus = analogRead (for the LDR) and the ldrStatus for what makes the light turn on and off (HIGH/LOW).

On the LED Board, I know that I have to identify the ledPin. Under the setup I have the pinMode as OUTPUT, the digitalWrite and Serial.begin for baud rate.

This is where I get lost, if I am even correct there and no, I haven't written any code to test the two Bluetooth modules.

Are you doing this as a learning project or trying to build something specific? I ask because I have a couple of microcontroller boards with LEDs and Bluetooth interfaces available. It's easy enough for me to add an LDR to one of them to transmit data to the other one if you need something off the shelf.

It’s really a little bit of both. I was playing around on youtube one day and thought it would be neat to learn how to create circuit boards ( I have been in IT for 20+ years and never really learned). Then I saw the bluetooth. I know exactly how to code the boards for a pushbutton, just not the LDR.

What I am thinking I need to do differently is take the 1 and 0 out (digital) and add a parameter 0 through 1023 (analog). Instead of pushing the information over with digitalWrite, I am going to try the analogWrite.

my new code

LED CODE

const int ledPin = 13;
int status = 301;
int state = 0;

void setup()  {
  Serial.begin(38400);        // Default communication rate for Bluetooth Module
  pinMode(ledPin, OUTPUT);    // Initializes the LED pin as an OUTPUT
}

void loop()  {
  if(Serial.available() > 0)  {   // Checks whether data is coming from serial port
    state = Serial.read();        // Reads data from the serial port
}
  if (status <=300)  {
  digitalWrite(ledPin, HIGH);  // Turns LED on
  state = 0;
  }
  else if (status >300)  {
  digitalWrite(ledPin, LOW);   // Turns LED off
  state = 0;
  }
}

LDR CODE

const int ldrPin = A0;
int state = 20;

void setup()  {
  Serial.begin(38400);       // Default communication rate for Bluetooth Module
  pinMode(ldrPin, INPUT);    // Initializes the LDR pin as an INPUT
}

void loop ()  {
  if(Serial.available() > 0)  {   // Checks whether data is coming from serial port
    state = Serial.read();        // Reads data from the serial port
}

  int ldrStatus = analogRead(ldrPin);   // Reads the status of the LDR value

  if (ldrStatus <=300)  {       // LDR parameters if yes, LED on -- if no, LED off
  analogWrite(ldrPin, HIGH);   // Turns LED on
  }
  else  {
  analogWrite(ldrPin, LOW);  // Turns LED off
  }
}

On the LED device, you read some data, if there is anything to read, and store it in state.

Then, you compare the value in status, hardcoded to 301, to 300, to decide what to do. That hardly seems like a useful thing to do.

On the LDR device, you read any serial data that the other device sends. But the other device doesn't send any. So, why are you reading?

The LDR device doesn't send any data, either.

Your code, on each device, seems completely useless.

You have the basic concept right:

  1. Reader board: Read analog input and print that value to serial port
  2. Writer board: Read serial port and if data is available and the data read is over the threshold, turn on LED, otherwise, turn it off.

Just do some code cleanup and it should start working.