Bluetooth data trans problem

hi guys
i have connected 2 bluetooth devices hc-05 to transmit data, while only reading the pot value it works clean, at the moment i put the code to do something with the neopixel stick the data i get is all over the place the current code is this ones

#include <SoftwareSerial.h>

#define BT_SERIAL_TX 4

#define BT_SERIAL_RX 3

SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);

int potpin = A0;

int val;

void setup()

{

Serial.begin(9600);

BluetoothSerial.begin(9600);

}

void loop()

{

val = analogRead(potpin);

val = map(val, 0, 1023, 10, 179);
Serial.println(val);

BluetoothSerial.write(val);
//BluetoothSerial.write(val);

//delay(100);

}

and the receiver code

///NANO WITH THE MASTER BLUETOOTH, READS THE SIGNAL OF THE SALVE ON THE UNO( POT DATA)
//////NEOPIXEL SETUP
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel stick1 = Adafruit_NeoPixel(8, 10, NEO_GRB + NEO_KHZ800);  //12 neopixels on pin 10  (PIN , NUMBER OF LED, NEO_GRB + NEO_KHZ800)



#include <SoftwareSerial.h>

#define BT_SERIAL_TX 4

#define BT_SERIAL_RX 3

SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);


int val;
void setup()

{
  ///START THE PIXEL STICK
  stick1.begin();
  ///

Serial.begin(9600);

BluetoothSerial.begin(9600);



}

void loop()

{

if (BluetoothSerial.available()>0)

{
val = BluetoothSerial.read();
Serial.println(val);

if (val <=20)
val = BluetoothSerial.read(),
Serial.println(val),
   stick1.setPixelColor(0, stick1.Color(0,0,0)),
   stick1.setPixelColor(1, stick1.Color(0,0,0)),
   stick1.setPixelColor(2, stick1.Color(0,0,0)),
   stick1.setPixelColor(3, stick1.Color(0,0,0)),
   stick1.setPixelColor(4, stick1.Color(0,0,0)),
   stick1.setPixelColor(5, stick1.Color(0,0,0)),
   stick1.setPixelColor(6, stick1.Color(0,0,0)),
   stick1.setPixelColor(7, stick1.Color(0,0,0)),
   stick1.setPixelColor(8, stick1.Color(0,0,0)),
   stick1.setPixelColor(9, stick1.Color(0,0,0)),
   stick1.setPixelColor(10, stick1.Color(0,0,0)),
   stick1.setPixelColor(11, stick1.Color(0,0,0)),
   stick1.setPixelColor(12, stick1.Color(0,0,0)),
   stick1.setPixelColor(13, stick1.Color(0,0,0)),
   stick1.setPixelColor(14, stick1.Color(0,0,0)),
   stick1.setPixelColor(15, stick1.Color(0,0,0)),
   stick1.show();
   
   if (val>=80)// and mapped < 2)
   val = BluetoothSerial.read(),
Serial.println(val),
   stick1.setPixelColor(0, stick1.Color(0,0,255)), //blue
   stick1.setPixelColor(1, stick1.Color(0,0,0)),
   stick1.setPixelColor(2, stick1.Color(0,0,0)),
   stick1.setPixelColor(3, stick1.Color(0,0,0)),
   stick1.setPixelColor(4, stick1.Color(0,0,0)),
   stick1.setPixelColor(5, stick1.Color(0,0,0)),
   stick1.setPixelColor(6, stick1.Color(0,0,0)),
   stick1.setPixelColor(7, stick1.Color(0,0,0)),
      stick1.setPixelColor(8, stick1.Color(0,0,0)),
   stick1.setPixelColor(9, stick1.Color(0,0,0)),
   stick1.setPixelColor(10, stick1.Color(0,0,0)),
   stick1.setPixelColor(11, stick1.Color(0,0,0)),
   stick1.setPixelColor(12, stick1.Color(0,0,0)),
   stick1.setPixelColor(13, stick1.Color(0,0,0)),
   stick1.setPixelColor(14, stick1.Color(0,0,0)),
   stick1.setPixelColor(15, stick1.Color(0,0,255)),
   stick1.show();
   
}



}

without the neopixel the data is fine

once i put in the neopixel config in the loop the data i get is totally over the place

how can i fix this? thanks

Hello.
There are several questions.
How often do you send data from the transmitter?
(How much data per second is BluetoothSerial.write (val) sending?)

How often can you receive data at the receiver?
How long does one pass of the receiver loop() take?

The receiver has a code like this

if (BluetoothSerial.available()>0)
{
   val = BluetoothSerial.read();
   ...
   if (val <= 20)
   val = BluetoothSerial.read(),...
   ...
   if (val> = 80)
    val = BluetoothSerial.read(),...
}

Why do you check for a byte in the buffer once, but read it twice.
Is the second byte always ready? How do you check this?

i am parting from a example code as my knowledge in bt is very limited atm
i have tested now if the value passes x to turn on the builtin led and under to be off
it works good with no random all over the place values read from the pot to the bt device
as soon i run the neopixel its all over the place

How can I explain it in a nutshell?
The transmitter loop() code is very short, it runs fast and sends thousands (if not millions) of data per second. You removed the 100ms delay.
The receiver code is much larger in volume, it executes more slowly, you simply do not have time to read all the data. The Serial buffer is limited, as far as I remember, it is 64 bytes. This means that some of the data simply disappears.
You must synchronize the operation of the transmitter and receiver.
Read the basics of data transfer over Serial for a start

Because the timing requirements are so tight, the Neopixel library disables interrupts when executing the show function. Software serial depends on interrupts. See here.

Hi
Thanks for the info
I managed to get it working
I added 30 delay on the emitting signal and comes out clean now

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