AnalogRead via Bluetooth

Good morning and thank you in advance,

Summary:
Requiring assistance with translating two independent analog potentiometer signals from one arduino wireless via bluetooth to a second arduino and output the values on two independent led bar graphs.

When one potentiometer is adjusted from zero, the corresponding led bar graph will illuminate an increasing number of leds over the value of the potentiometer.

The inputs potentiometers and output led bar graphs are located on two separate boards and only will communicate via bluetooth.

I have successfully created the project with using a single arduino. The input consists of two independent 10k ohm potentiometers delivering analogRead signals to a MEGA 2560 and the resulting value is displayed on output pins to 20 leds, 10 per bar.

I need assistance to better understand the code for how to write the state of the potentiometer serial.write() of the master bt and use the state "".write() on the slave led board.

Hardware:
Board 1:
1x Mega 2560
2x 10k potentiometer
1x HC-06 bluetooth module
1x I2C Logic Level Converter

Board 2:
1x Mega 2560
1x HC-06 bluetooth module
1x I2C Logic Level Converter
20x led
20x 220 resistor

I appreciate you taking the time to assist and enlighten a new arduino member

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//                                                                              LIBRARY
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#define analogPinBRAKE A1                                                                                                                         // brake RED potentiometer
#define analogPinTHROTTLE A2                                                                                                                 // throttle GREEN potentiometer
#define ledBarCountRED 10                                                                                                                     // number of LEDs in RED array
#define ledBarCountGREEN 10                                                                                                                 // number of LEDs in GREEN array

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//                                                                             VARIABLES
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int ledBarPinsRED[] = {31, 30, 29, 28, 27, 26, 25, 24, 23, 22};                                             // an array of pin numbers to which RED LEDs are attached, 22-31
int ledBarPinsGREEN[] = {41, 40, 39, 38, 37, 36, 35, 34, 33, 32};                                         // an array of pin numbers to which GREEN LEDs are attached, 32-41

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//                                                                              SETUP
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void setup(){
  for (int thisBarLedRED = 0; thisBarLedRED < ledBarCountRED; thisBarLedRED++) {pinMode(ledBarPinsRED[thisBarLedRED], OUTPUT);}  // an array of pin array and set them all to output
  for (int thisBarLedGREEN = 0; thisBarLedGREEN < ledBarCountGREEN; thisBarLedGREEN++) {pinMode(ledBarPinsGREEN[thisBarLedGREEN], OUTPUT);}  // an array of pin array and set them all to output
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//                                                                            MAIN LOOP
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void loop(){
  int sensorReadingBRAKE = analogRead(analogPinBRAKE);                                                                                       // read the BRAKE potentiometer
  int ledSensorBRAKE = map(sensorReadingBRAKE, 0 , 1023, 0, ledBarCountRED);                         // map the result to a range from 0 to the number of LEDs for BRAKE bar
 
  int sensorReadingTHROTTLE = analogRead(analogPinTHROTTLE);                                                                                // read the TROTTLE potentiometer
  int ledSensorTHROTTLE = map(sensorReadingTHROTTLE, 0 , 1023, 0, ledBarCountGREEN);               // map the result to a range from 0 to the number of LEDs for TROTTLE bar

  for (int thisBarLedRED = 0; thisBarLedRED < ledBarCountRED; thisBarLedRED++) {                                                        // loop over the LED array for RED
     if (thisBarLedRED < ledSensorBRAKE) {digitalWrite(ledBarPinsRED[thisBarLedRED], HIGH);}  // if the array element's index is less than ledSensorBRAKE turn the pin for this element on
     else {digitalWrite(ledBarPinsRED[thisBarLedRED], LOW);}}                                                            // turn off all pins higher than the ledSensorBRAKE

  for (int thisBarLedGREEN = 0; thisBarLedGREEN < ledBarCountGREEN; thisBarLedGREEN++) {                                                // loop over the LED array for GREEN
     if (thisBarLedGREEN < ledSensorTHROTTLE) {digitalWrite(ledBarPinsGREEN[thisBarLedGREEN], HIGH);}  // if the array element's index is less than ledSensorTHROTTLE turn the pin for this element on
     else {digitalWrite(ledBarPinsGREEN[thisBarLedGREEN], LOW);}}                                                     // turn off all pins higher than the ledSensorTHROTTLE
}

Well I did googling
https://www.google.de/search?as_q=arduino+HC-06+bluetooth+send+receive+demo+code

and found this tutorial about HC-06
in this hit

they write
N.B.: Since the module HC-06 is a slave module it cannot connect to other device on its own. To do so you need a master module such as the Bluetooth module HC-05.

So I did a new googling
https://www.google.de/search?as_q=arduino+pairing+two+HC-06+modules
and found this

You might have the experience that googling for biology a special kind of product does not work good. In programming google is always worth a five-minute search.

best regards Stefan

@patchedhaven
BT is an UART device; so, connet it with UART1 (or UART2 or UART3) Port of MEGA and then eanble it using the following code in the setup() function.

Serial1.begin(9600);

1 Like

Thank you Stefan.
To clarify my question. Pairing is not the issue.

I am looking for information about what syntax ??.write() I should be using in place of analogRead on the slave board.

Most googling I see is an output to servos or a button click to one led high.
ex. servo.write(angle).

This is my basic bluetooth code of the master. I need to know what syntax I need on the slave to use on a string of leds output.

int stateBRAKE = 0; int sensorReadingBRAKE = 0; void setup() { Serial.begin(38400); // Default communication rate of the Bluetooth module } void loop() { if (Serial.available() > 0) { // Checks whether data is comming from the serial port stateBRAKE = Serial.read(); // Reads the data from the serial port } // MASTER Reading the potentiometer sensorReadingBRAKE = analogRead(A1); int sensorValueMappedBRAKE = map(sensorReadingBRAKE, 0, 1023, 0, 255); Serial.write(sensorValueMappedBRAKE); // Sends sensorReadingBRAKE to ledBarPinsRED delay(10); }

int stateBRAKE = 0;
int sensorReadingBRAKE = 0;
void setup() {
  Serial.begin(38400); // Default communication rate of the Bluetooth module
}
void loop() {
  if (Serial.available() > 0) { // Checks whether data is comming from the serial port
    stateBRAKE = Serial.read(); // Reads the data from the serial port
  }
  // MASTER Reading the potentiometer
  sensorReadingBRAKE = analogRead(A1);
  int sensorValueMappedBRAKE = map(sensorReadingBRAKE, 0, 1023, 0, 255);
  Serial.write(sensorValueMappedBRAKE); // Sends sensorReadingBRAKE to ledBarPinsRED
  delay(10);
}

Please explain how you get the two HC06's to connect to each other if they are slave only.

I need to know what syntax I need on the slave

You can think of classic Bluetooth as simply Serial without wires. How would you read an analog reading value which you typed into the Serial monitor and sent to your Arduino?

Any of the bluetooth-examples that transmits data over bluetooth use the same principle
they use a second serial connection.

There are two main tasks:

  1. receiving the data
  2. do something with the received data

the first task is always the same receive data. completely regardless of what the data is used for.

You are sending a single byte
So the slave does receive a single byte. That's all.

I want to insist on the HC06-slave-problem:

This will work:
Potentiometer-Arduino----software-serial----HC06-----> wireless_data ----->------Smartphone / PC

Smartphone / PC------>----wireless_data----->------------>HC-06--software-serial---- LED_Arduino

This will

NOT WORK with two HC-06-modules

Potentiometer-Arduino----software-serial----HC06----> wireless_data --->--->HC-06--software-serial---- LED_Arduino

This requires at least one HC-05-bluetooth-modul to get the pairing done

You can simulate the wireless data-transmission through direct linking two Arduinos by wires
Arduino 1 Tx------------Rx-of-Arduino2
Arduino 1 Rx------------Tx-of-Arduino2

As you are using two Arduino Mega 2560. Arduino Mega 2560 have three hardware-serial interfaces build in.

So simply use Serial2 or Serial3 for the bluetooth-modules.

As a first step

best regards Stefan

1 Like