Go Down

Topic: LDR Analog reading via Bluetooth (Read 2543 times) previous topic - next topic

vishruth_kumar

#15
Apr 03, 2017, 05:33 pm Last Edit: Apr 03, 2017, 06:02 pm by vishruth_kumar
Following the same connections with slave code as:
Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
#define ldrPin A0
int state = 20;
int ldrValue = 0;
void setup() {
  pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
  digitalWrite(9, HIGH);
  pinMode(ldrPin, INPUT);
  BTSerial.begin(9600);
  Serial.begin(9600);
  
}
void loop()
{
 ldrValue = analogRead(ldrPin);
 BTSerial.println(ldrValue);
 Serial.println(ldrValue);
 delay(10);
}


and master code as:
Code: [Select]


// Example 2 - Receive with an end-marker
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX
const byte numChars = 1024;
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup() {
    pinMode(9, OUTPUT);  // this pin will pull the HC-05 pin 34 (key pin) HIGH to switch module to AT mode
    digitalWrite(9, HIGH);
    BTSerial.begin(9600);
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvWithEndMarker();
    showNewData();
}

void recvWithEndMarker() {
    static byte ndx = 0;
    char endMarker = '\n';
    char rc;
   
    while (BTSerial.available() > 0 && newData == false) {
        rc = BTSerial.read();

        if (rc != endMarker) {
            receivedChars[ndx] = rc;
            ndx++;
            if (ndx >= numChars) {
                ndx = numChars - 1;
            }
        }
        else {
            receivedChars[ndx] = '\0'; // terminate the string
            ndx = 0;
            newData = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        delay(1000);
        newData = false;
    }
}


I am getting the readings in serial monitor of master as:
Code: [Select]
<Arduino is ready>
This just in ... 8
This just in ... 3
This just in ... 4
This just in ... 3
This just in ... 3
This just in ... 3
This just in ... 3
This just in ... 4
This just in ... 3
This just in ... 4
This just in ... 3
This just in ... 3

 
and so on. The problem might be with the code Can you please check?

Robin2

I don't have time to study all this now. I will bookmark it and try to look at it in the morning.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

vishruth_kumar

I tried searching in other forums and blogs, could not find the solution. Please take your time , and look into this.

Its really important for me to get this working.

vishruth_kumar

Oh come on dont leave me hanging!!
I followed every step. The serial monitor of master is showing the first digit , but it is correct. How can I receive the exact readings . There must be slight modifications in the functions in the Example 2 of your link.

I am a beginner in arduino , So I am not able to understand. Please help me!

Robin2

Sorry. I forgot about you - I was distracted by trying to build a model railway locomotive.

My guess is that your master program is actually showing what it receives. If it was not receiving anything it would not print anything.

Debugging should always focus on simplicity. Rather than sending the value from the LDR why not just send a fixed value - for example BTSerial.println("1234");

Also, you are sending values very frequently - 100 times per second. For testing slow that down to 2 per second.

In your slave program you are printing the value to the Serial monitor - is that showing the same thing as the master is receiving?

In the direction of simplicity it would also be a good idea to remove the Bluetooth devices and connect the two Arduinos by wires (and GND) using the same pins as are used by the Bluetooth devices.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up