I have created a board (Mega 2560) that uses a light dependent resistor (LDR) as the input (A0) and an LED as the output (D13). I have been able to code this and make it work. Now, I am trying to use two cards utilizing bluetooth modules. I have the bluetooth working between the two but unsure of how to split the code between the two Mega cards. My code is below.
//set pin numbers
//const will never 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); //reads the status of the LDR value
//check if 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("-----------");
}
}
How can anyone "split the code between the two Mega cards" without knowing what you are trying to accomplish?
I am going to guess that you want one Mega to read the LDR and the other Mega to drive its LED. I am going to guess that you want the first Mega to send the state of the LDR (or the intended state of the LED) to the second Mega via Bluetooth.
Did I guess right?
Both Megas will need a setup() function. In one, you will have to set up to read the LDR pin. In the other, you will have to set up to drive the LED.
Both Megas will need a loop() function. If I guessed right, in one, you will read the LDR and send state information to the other Mega. In the other, you will check if you have received a Bluetooth message and, if so, will turn the LED on or off as appropriate.
Both Megas should have Serial.println() calls so that you know what is happening and can debug.
There is no easy place to split the code. You have some good pieces but a piece goes here, a piece goes there, etc.
Yes, I want the first card (the master) to have the LDR attached and the 2nd card to light up the LED. On the master card, I will have the LDR plugged into pin A0 and on the 2nd card I will have the LED plugged into pin D13.
On the master card I believe I have to identify the ledPin and under setup function, need to have it as an output.
On the 2nd card (slave) I have to identify the ldrPin and under setup function as input. However, that is about all I can figure out as I am completely new to all of this.
The LDR card should not drive the LED pin. Instead, it should send a Bluetooth message to turn the LED on or off.
The LED card needs to look for a Bluetooth message. If there is one about the LED, it should do what the message tells it to do - turn the LED on or off.
I have yet to see any Bluetooth messages sent or received.
So, after doing some research, I believe I have a little better understanding as to why you were saying I was backward. I also understand some of the code a little better. However, I am still a little confused and need some guidance. Let me know what you think please.
LDR CARD
const int ledPin = 13;
int state = 0;
void setup() {
pinMode(ledPin, OUTPUT);
digital.Write(ledPin, LOW);
Serial.begin(38400);
}
void loop() {
if(serial.available() > 0) {
state = Serial.read();
}
if(ldrStatus <=300) {
digital.Write(ledPin, HIGH);
Serial.println("LDR is DARK, LED is ON");
}
else {
digitalWrite(ledPin, LOW);
Serial.println("--------")
}
Here is my thoughts on the LED Card though I don't know all of it, just like above
const int ldrPin = A0;
int state = 0;
int ldrState = 0;
void setup() {
pinMode(ldrPin, INPUT);
Serial.begin(38400);
}
// I need something here like an analogRead I believe but lost beyond all beief
Again, I am new and no, I have not not been ignoring you but am a little confused as I have only been playing with this stuff for a week.
As for the Bluetooth code...there is none. All the Bluetooth code is done through the COM window using AT commands. I haven't found even one example where the Bluetooth is in with the Arduino code.
As for the schematic, I attached a picture to my last post earlier today showing the schematic.
Here is what I have come up with now after reading all the posts.
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 sorry that I previously missed the schematic.
However, I do not understand the point of connecting A0 to ground. The analog read of A0 will always return something close to 0. What did you do before?
Also, I do not understand the point of connecting the LDR between 5 volts and ground. What did you do before?
Connecting the Bluetooth modules (I guess that is what those are) to pins 0 and 1 is handy because it avoids needing to use Software Serial. Unfortunately, it will also make any debugging very difficult. You will have to remember that Serial really means Bluetooth. Serial will not write out a message in a window.
Your code is getting closer but will not compile. For example, "esle" is not part of C/C++ syntax.
I do not see any Bluetooth AT commands.
lpetrowicz wrote (in part, in the first message):
I have the bluetooth working between the two
But lpetrowicz also wrote (in part, in the most recent message):
As for the Bluetooth code...there is none.
You need to take this in easy (small) pieces.
Get the LDR working. Print out messages (in a window) that provide the value read from the LDR and whether the value is above or below the threshold.
Get the LED working. Make sure that you can turn it on and off.
Get the Bluetooth working so that you can communicate between the cards.
At this point, I would like to suggest combining 1 and 3 but you cannot because you have lost the ability to debug. You will be forced to combine 1, 2, and 3. Sigh.
By the way, having a single threshold for the LDR is generally a bad idea. However, it is not so bad if all you are doing is driving an LED. It would be better to have two thresholds: a slightly larger one when the values are increasing and a slightly smaller one when the values are decreasing. This is called hysteresis. It avoids the problem that will occur when the LDR is operated near the threshold value.
The code I have originally used for one card is the first code I pasted to this forum. The way you see the schematic is similar to the single card and it works, even with the LDR attached to the ground, 5v and A0. I used the code from a youtube post.
The youtube video that I watched that showed the schematic with just one card is as follows:
This video shows the LED in pin13 and the LDR in pinA0 while being attached to the ground and 5v as well. I hooked this setup up to one of my Arduino MEGA 2560 cards and it worked as advertised. This is what taught me about the analog coding.
Then I went and found the youtube video for using two cards and the Bluetooth modules as well. This video is as follows:
In this video, I learned how to configure the Bluetooth modules. However, in this example, the LED is turned on by a pushbutton which I know is digital. I now know that the LDR has to be analog.
Where I am lost is how to construct the analog read instead of the digital I believe. Again, I stress that I am new and trying to learn as I am going along.
There was an analog read in the code in the first post. This will do the job except that grounding A0 will cause a problem.
From the other posts that I have seen, the challenging part is going to be handling the received status message on the LED Mega.
Messages are easiest if they have a preamble (say, the < character), the content (say, a 0 or a 1), and a postamble (say, the > character). This system would then send one of two messages, either <0> or <1> . This will then be handled to turn the LED off or on.
The Mega also has Serial1, Serial2, and Serial3 so you do not have to give up debugging to use Bluetooth modules and you do not need to use Software Serial.
On the other hand…
Serial (pins 0 and 1) should not be used for Bluetooth because of the complications in downloading code.
The Mega already has an LED on pin 13 so an external LED should be unnecessary.
My suggestion: connect and use Serial1 for Bluetooth and reserve Serial for downloading and for debug prints.
Also, eliminate the external LED.
This video shows the LED in pin13 and the LDR in pinA0 while being attached to the ground and 5v as well. I hooked this setup up to one of my Arduino MEGA 2560 cards and it worked as advertised.
The video I saw (I only watched a part) used a 10K resistor in conjunction with the LDR, starting at about 1 minute into the video. One could eliminate this resistor by using the resistor in an Arduino input, but the code and the schematic do not reflect this usage.
The video connects one leg of the LDR to +5 volts. One leg of the 10K resistor is connected to ground. The other leg of the LDR is connected to the other leg of the 10K resistor and also connected to the A0 input. The A0 input is never connected directly to ground.
The posted schematic did not have the 10K resistor, could not work, and could possibly damage the LDR.
I actually spent the day looking at some code and I believe I have it. I am going to give it a shot this evening at some point. I ran it through the Arduino Editor and I fixed any errors. I know this does not mean it will function properly, just that I do have all syntax properly written.
If you would like to take a look at it to offer some assistance, I would greatly appreciate it.