Go Down

Topic: Two Arduinos talking via Bluetooth (Read 849 times) previous topic - next topic

Nick_Pyner

The bad news is that there is a kind of time lag which is growing in the course of time. So when I power them up (and they got connected via bt) the time lag is like 1 second. After some minutes the time lag is about 3 seconds. After some more minutes the time lag is like 5 seconds and so on until nothing happens any more.
Besides that I spotted/picked out (I don't know what's the right word in this case) that the synchronous flashing/blinking of the bluetooth modules slowly turns into an asynchronus flashing/blinking. So there's a kind of time lag, too.

OK, so we conclude that the only real problem was Bluetooth/Arduino wiring.

I don't think there is ever any synchronous blinking, even if it looks like there is. There may be blinking 
triggered by communication action, but I don't know about that.

I can only suggest you go back to wire connection between Arduinos and run the code for a similar period of time, something you may not have actually done before. If the time lag problem is still there, then clearly there is a problem in the code after all - and probably a minor one.

If not, then the bluetooth config is suss, but I really don't know how that can happen.


marCenzeichen

Ok I went back to wire connection and everything worked and still works great...

When I press the button the LED is shining immediately even after a similar period of time. Besides that the BT modules are just powered up (without TX RX connection) and they're blinking synchronously without losing it. So no time lag at all.

My hypothesis is that the BT modules got a problem with the permanent sending of data. So when I do not press the button the master sends thousands of '0's although one '0' would be enough. When I press the button it sends as many '1's as long as I do press the button (although one '1' would be enough too). Maybe the buffer is some kind of overloaded?

The question is: is there a possibility to program the master arduino in a way that it just sends one '1' when I press the button and sends one '0' when I don't press it?

Another idea is to change the baud rate to 115200 but the question is: what would be the effect? Does it cause even more '0's and '1's?

Regards

Marc

Nick_Pyner

I don't know much about this stuff, as I'm really only interested in sending data, and I'm afraid I certainly don't understand why what you have works with wires and gives grief with bluetooth, but clearly this
Code: [Select]
  if (buttonState == HIGH) {
    Serial.write('1');
    } else {
    Serial.write('0');
    }

means there will be Os for ever, or so long as you don't send a 1, hence lots of Os at breathtaking speed.

I suggest you put up a flag following the 

    Serial.write('1');

and make it being up a condition for
 
     Serial.write('0');

and take it down after you do write an 0. There may be more elegant ways of doing it in C, but I recall it worked in 1966.

The button press may be flagged similarly, but there is no real need for it.

The wiring around the switch in your Fritz may be kosher, but it makes no sense to me, and I think you should check it.

Another idea is to change the baud rate to 115200 but the question is: what would be the effect? Does it cause even more '0's and '1's?
Very likely.

marCenzeichen

Ok I fixed the time lag problem and I think that may be interesting to anyone.

Quote
My hypothesis is that the BT modules got a problem with the permanent sending of data. So when I do not press the button the master sends thousands of '0's although one '0' would be enough. When I press the button it sends as many '1's as long as I do press the button (although one '1' would be enough too). Maybe the buffer is some kind of overloaded?
My hypothesis was right! I tried to use some kind of flag to make the Arduino sends just one '0' when I don't press the button and sends one '1' when I press it.

So I changed the code of the master as follows:

Code: [Select]

const int pushButton = 3;     //constant number of the pushButton pin
int buttonState = 0;          //variable for reading the pushbutton status
int Flag = 0;                 //variable for the flag

void setup() {
  Serial.begin(9600);          //opens serial with 9600 baud/bites per second
  pinMode(pushButton, INPUT);  // initialize the pushButton pin as input

}

void loop() {

  buttonState = digitalRead(pushButton);  // read the state of the pushButton value

  if (Flag == 0) {               //if the flag is '0'
    if (buttonState == HIGH) {   // plus if the buttonState is 'high'
      Serial.write('1');         // then write '1' to the serial
      Flag = 1;                  // plus set flag to '1'
    } else {                     // if the flag is '0' plus buttonState is 'low' do nothing
      // empty   
    }
  }
  if (Flag == 1) {               //if the flag is '1'
    if (buttonState == LOW) {   // plus if the buttonState is 'low'
      Serial.write('0');         // then write '0' to the serial
      Flag = 0;                  // plus set flag to '0'
    } else {                     // if the flag is '1' plus buttonState is 'high' do nothing
      // empty
    }     
  }
}


It may be not the best/ most elegant way, but it works!

No time lags in case of transmitting even after hours - but there are time lags in case of synchronous blinking (master bt and slave bt) which doesn't matter and doesn't have any effects.

Regards

Marc

Go Up