Using Bluetooth Data serial freezing after second and rework after second

Using Bluetooth (the problem)

When using Bluetooth, the data is turned off for a period of time, and the data is output randomly (note that the same code is used)

Using serial Port (it's work)

image

//Pin connected to ST_CP of 74HC595
int latchPin = 6;
//Pin connected to SH_CP of 74HC595
int clockPin = 13;
////Pin connected to DS of 74HC595
int dataPin = 11;
byte FirstByte;
byte SecondByte;
byte ThirdByte;
byte FourthByte;
byte FifthByte;
int val;
byte serialInArray[5]; // array for storing 3 bytes as they arrive from processing
int serialCount = 0; // for counting the number of bytes received


void setup() {
  //Start Serial for debuging purposes	
  Serial.begin(9600);
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);

}

void loop() {
  
   if (Serial.available() > 0){
       serialInArray[serialCount] = Serial.read(); // read a byte sent by processing
        serialCount++;  // increment number of bytes received

    if (serialCount > 4 ) {  // when 3 bytes received
	FirstByte = serialInArray[0]; // get value for Red LEDs
	SecondByte = serialInArray[1]; // get value for Green LEDs
        ThirdByte = serialInArray[2];
        FourthByte = serialInArray[3];
        FifthByte = serialInArray[4];
Serial.print(FirstByte);
Serial.print(SecondByte);
Serial.print(ThirdByte);
Serial.print(FourthByte);
Serial.print(FifthByte);
Serial.println("");
  //count up routine
     digitalWrite(latchPin, 0);
    //count up on GREEN LEDs
    
    //count down on RED LEDs
    shiftOut(dataPin, clockPin, FirstByte); 
             shiftOut(dataPin, clockPin, SecondByte);
                 shiftOut(dataPin, clockPin, ThirdByte);
                 shiftOut(dataPin, clockPin, FourthByte);  
       shiftOut(dataPin, clockPin, FifthByte);
     
   
    //return the latch pin high to signal chip that it 
    //no longer needs to listen for information
    digitalWrite(latchPin, 1);
 serialCount = 0;
 delay(20);
   }
}
}
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

//internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

 //clear everything out just in case to
 //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=7; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {	
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}

What's the source of those 5 sequential bytes, for each of the two sources? It seems to me it's a problem somehow related to either the source (not the same data) or the comm speed. Double check them.

EDIT: also compare the bytes you suppose it should receive with the values shown on Serial, and post them: it could give you/us some advise.

Just a few comments about that code. Not real problems, but "style suggestions".

If serialCount is equal to the number of bytes received, when it's greater than 4 means you received at least 5 bytes, not 3.
Said that, if you check serialCount for each signle byte received it won't ever be greater than 5, so the "if" basically is fired always when "serialCount == 5". If, for any reason, it'd become greater you'll be in trouble because the array has just 5 elements...
Third, I wonder why you need "FirstByte", ""SecondByte" and so on, if you already have them stored into the byte array.
Fourth, use for() loops to handle arrays.
Last but not least, please use a good indentation (the IDE can do it for you, press Ctrl-T).

This is a "cleaner" version of the loop():

...
void loop() {
   if (Serial.available() > 0){
     serialInArray[serialCount++] = Serial.read(); // read a byte sent by processing

     if (serialCount == 5 ) {  // when all the 5 bytes received
	   // Show data over Serial
	   for(int i=0; i<=4; ++i) {
	     Serial.print(serialInArray[i]);
		 Serial.print(" ");
	   }
       Serial.println();
	   
       //count up routine
       digitalWrite(latchPin, 0);

       //count up on GREEN LEDs
       // (todo?)
	   
       //count down on RED LEDs
	   for(int i=0; i<=4; ++i)
         shiftOut(dataPin, clockPin, serialInArray[i]); 
   
       //return the latch pin high to signal chip that it 
       //no longer needs to listen for information
       digitalWrite(latchPin, 1);
       serialCount = 0;
       delay(20);
     }
  }
}
...

That mean 40 bit (led) from first to fifth

Watch the video I use the same code he works well on the serial port but when you add Bluetooth the problem appears

Ok, but the terminal window you posted contains some non printable chars (some "binary" bytes): I don't know what they are and from where you got them (you haven't specified their source), and that's not the intended Arduino output.
Print the "packet" received bytes in hex and compare them with what the source, for Arduino you can use:

	   // Show data over Serial
      for(int i=0; i<=4; ++i) {
        if (serialInArray[i] < 16) Serial.print("0");
        Serial.print(serialInArray[i], HEX);
        Serial.print(" ");
      }
      Serial.println();

Do the same to the source.

But I must ask you again, what's the source of the data, and how did you switch from serial to BT? Are you sure the sent packets are received with the same speed and protocol (if any)?

You appear to be receiving serial data in order to light LEDs. I don't know anything about lighting LEDs but I doubt that Bluetooth is causing the problem. I assume you originally sent the data from the serial monitor and now want to do the same thing via Bluetooth, i.e. simply add the lower wiring diagramme to the upper one. That should be entirely OK, and no change to code is required, but note that you cannot use the serial monitor to send data with that arrangement, you can only monitor the traffic sent via Bluetooth.

If the monitor is actually out of the game when using Bluetooth, what is the power source then?

Its source from visual basic 6 I moved the code to basic4android, the data is processed correctly like vb6, but the problem when it reached the Bluetooth module, I used another code the LED on and off, the same problem

                         This is b4a data

There is an empty byte in both programs, ASCII of char is not recognized because the keyboard languge has a specific char... Is this true????

Capkkjjture

I know VB6 but I know nothing about B4A, anyway the picture you posted here seems to me a multiple base dump, 4 lines for the each sent byte. The first line is binary, the second hex, the third octal, the last is just the ASCII character.

What you need to look at are just the first two (so forget the third and fourth lines of each block): the binary, because represents the desired led statuses, and the hexadecimal, because this is what you need to check with the code I suggested you before.

In this case, for example, if on Arduino Serial monitor you see the sequence given by the second line of each row (I assume the first 5 only, as the block is made of 5 bytes) is "1C 38 70 E0 C0" the data from the B4A code is correctly received. If not, you need to check what's going on and we can't do it for you.

Said that, if you want/need to debug using the Serial (pins D0, D1) you first of all need to use SoftwareSerial to connect the BT module to another pins pair, and not the default, and change your code accordingly.
If you don't know SoftwareSerial have a look HERE. Depending on how your B4A does with the BT port, it could solve your issue by avoiding communication conflicts.

If it doesn't solve your issue, please do what I asked you before: let the B4A program send just a few packets and track both the hex values sent (if possible put just the hex values on the debug B4A window you have shown us) and received (using the code to print received packet hex values).

After removing (((second byte)) and above, from the code

Before I isolated the serial port from the BT port, giving it another pin, same problem

Ok, but it's not enough. Please tell us what is your BT module (with the link to the exact item you have), how exactly you connected it to Arduino (pins, cable types, and/or a picture of the whole), and the Arduino code you used to produce the serial monitor dump shown on the left.

BT moudule HC_05



#include <SoftwareSerial.h>

const byte rxPin =2;
const byte txPin =3;

SoftwareSerial mySerial(rxPin, txPin); 

//Pin connected to ST_CP of 74HC595
int latchPin = 6;
//Pin connected to SH_CP of 74HC595
int clockPin = 13;
////Pin connected to DS of 74HC595
int dataPin = 11;
byte FirstByte;

int val;
byte serialInArray[1]; // array for storing 3 bytes as they arrive from processing
int serialCount = 0; // for counting the number of bytes received


void setup() {
  //Start Serial for debuging purposes  
 
  Serial.begin(9600);
  mySerial.begin(9600);
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(rxPin,INPUT);
  pinMode(txPin, OUTPUT);

}

void loop() {
  
   if (mySerial.available() > 0){
       serialInArray[serialCount] = mySerial.read(); // read a byte sent by processing
        serialCount++;  // increment number of bytes received

    if (serialCount > 0 ) {  // when 3 bytes received
  FirstByte = serialInArray[0]; // get value for Red LEDs

Serial.print(FirstByte);

Serial.println("");
  //count up routine
     digitalWrite(latchPin, 0);
    //count up on GREEN LEDs
    
    //count down on RED LEDs
    shiftOut(dataPin, clockPin, FirstByte); 
          
     
   
    //return the latch pin high to signal chip that it 
    //no longer needs to listen for information
    digitalWrite(latchPin, 1);
 serialCount = 0;
 delay(20);
   }
}
}
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  // This shifts 8 bits out MSB first, 
  //on the rising edge of the clock,
  //clock idles low

//internal function setup
  int i=0;
  int pinState;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, OUTPUT);

 //clear everything out just in case to
 //prepare shift register for bit shifting
  digitalWrite(myDataPin, 0);
  digitalWrite(myClockPin, 0);

  //for each bit in the byte myDataOut�
  //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  //This means that %00000001 or "1" will go through such
  //that it will be pin Q0 that lights. 
  for (i=1; i>=0; i--)  {
    digitalWrite(myClockPin, 0);

    //if the value passed to myDataOut and a bitmask result 
    // true then... so if we are at i=6 and our value is
    // %11010100 it would the code compares it to %01000000 
    // and proceeds to set pinState to 1.
    if ( myDataOut & (1<<i) ) {
      pinState= 1;
    }
    else {  
      pinState= 0;
    }

    //Sets the pin to HIGH or LOW depending on pinState
    digitalWrite(myDataPin, pinState);
    //register shifts bits on upstroke of clock pin  
    digitalWrite(myClockPin, 1);
    //zero the data pin after shift to prevent bleed through
    digitalWrite(myDataPin, 0);
  }

  //stop shifting
  digitalWrite(myClockPin, 0);
}

I can't actually see anything wrong (I need to get back home to test your code with one of my BT modules...), but this is only one side of the communication: are you sure your BT sender is ok (e.g. no interferences or badly connected)?
Anyway, I already said I know nothing about B4A, except it's a framework to create Android apps, so I was wondering how you switched from serial (where everything seems to work) to BT (where it seems some bytes are corrupted or in excess).
Could you please describe me the B4A side (together with the B4A code section you use to send data via BT or serial, if possible) also?

Meanwhile, as I often say when something does not work get rid of the shifters and try this code (as you can see, I made some modifications to let you have parametric packets):

#include <SoftwareSerial.h>
// Configuration
const byte rxPin = 2;
const byte txPin = 3;
const byte PACKET_SIZE = 5;

SoftwareSerial mySerial(rxPin, txPin); 
byte serialInArray[PACKET_SIZE];
int serialCount = 0; // for counting the number of bytes received

void setup() {
  Serial.begin(9600);
  mySerial.begin(9600);
  Serial.println("STARTED");
  Serial.print("PACKET_SIZE="); Serial.println(PACKET_SIZE);
}

void loop() {
  if (mySerial.available() > 0){
    serialInArray[serialCount++] = mySerial.read();
    if (serialCount == PACKET_SIZE ) {  
	  // Dump packet values
      Serial.print("Packet: ");
	  for(int i=0; i<PACKET_SIZE; i++) {
	    Serial.print("0x");
        Serial.print(serialInArray[i], HEX);
		Serial.print(" ");
	  }
	  Serial.println();
	  // Reset packet byte counter
      serialCount = 0;
    }
  }
}

As done before, please make this test again, posting the corresponding serial monitor and B4A outputs (show the hex values only, we don't need the other formats), after sending at least 3 or 4 consecutive packets (15-20 bytes or more) and recording them into two files you can attach here or include as "code" and see what happens.

it was solved in b4a using

GetByte ("ISO-8859-1")
rather than
GetByte ("UTF8")

but noticed something going on, the data is piling up(freezing). And after a moment, it's back

It there a bug in bluetooth? Is there anything to bypass this? At Command settings. or anything

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