Code to have two arduinos talk to each other over bluetooth

Hello,

So far I have been able to get things to work in terms of communication between two arduinos but it has been using delays to give the slave device time to execute a request. Now I would like to use a query/response type of system where the master sends a 1 and waits to receive a 1 before sending its first request. Then it waits to receive a 1 from the slave before sending the next request. Right now it is just sending numbers that the slave will use to blink the pin13 led. When I run it the LED pin just blinks continuously rather than once, twice, three times, and so on. Hopefully someone can help me spot my problem in my logic. The Master code is:

void setup() {                
  // initialize the digital pin as an output.
  
  Serial.begin(38400);  
 
}


void loop() {
  
  //Query device and wait for a response
  while(!(Serial.available()))
  {
    delay(20);
    Serial.write(1);
  }
  //Once we get a response begin sending data
  if(Serial.read() == 1){
    while(1)
    {
  
    for(int i = 1; i<11; i++)
      {
        Serial.write(i);
        
        //wait to get a response back from the slave saying
        //that the command was executed
        while(!(Serial.available())){}
        if(Serial.read() == 1)
        {
          delay(500);
        }
        
        
      }
    }
  }
}

and the Slave code is:

#define led 13 


void setup() {
  // initialize serial:
  Serial.begin(38400);
  pinMode(led, OUTPUT);
  
}

void loop() {
  // wait for a query from the master
  while(!(Serial.available())){}
  
  //If you receive a 1 respond with a 1
    if (Serial.read()==1){
      Serial.write(1);
    }
    while(1){
    //wait for the data to come in
    while(!(Serial.available())){}
      int i = Serial.read();
    for(int j = 0; j < i; j++)
    {
      digitalWrite(led,HIGH);
      delay(200);
      digitalWrite(led, LOW);
      delay(200);
      
    }
    //send a 1 to notify the master that the command has been executed
    Serial.write(1);
    }
  }

Does anyone see any thing? I can explain what is supposed to happen more clearly.

  1. Slave waits for the master to contact
  2. Master sends a '1' integer (not ascii) and waits for a response
  3. Slave receives the '1' and responds with a '1' to let the master know that it is there
  4. Master sends an integer (again not in ascii) and waits for the slave to sent a '1' to let the master know that the command has been executed
  5. Slave receives the integer and blinks its LED whatever number of times the master requested. Then sends a '1' to notify the master that the command has been executed.
  6. Master receives '1' from the slave and sends the next integer and again wait for a '1' to come back
  7. Slave receives the integer from the master and blinks the led.....etc.

So the communication would go something like this:

Master Sends '1' Slave Sends '1' Master Sends '5' Slave blinks the led 5 times and then send '1' Master Sends '6' Slave blinks the led 6 times and then send '1' Master Sends '7' Slave blinks the led 7 times and then send '1' and so on

In the master:

void loop() {
  
  //Query device and wait for a response
  while(!(Serial.available()))
  {
    delay(20);
    Serial.write(1);
  }

It's sending "1"s every 20ms. If it manages to send more than one before the slave can respond then the slave will store the extra in its buffer. Your slave will never purge its buffer but instead, in the main loop, keep asking for another "1" after each is processed.

Chagrin: In the master:

void loop() {
  
  //Query device and wait for a response
  while(!(Serial.available()))
  {
    delay(20);
    Serial.write(1);
  }

It's sending "1"s every 20ms. If it manages to send more than one before the slave can respond then the slave will store the extra in its buffer. Your slave will never purge its buffer but instead, in the main loop, keep asking for another "1" after each is processed.

Thanks! I will increase the delay and see what happens. I think what you are saying is that the slaves Serial Buffer is filling up with a bunch of 1's and it is trying to work through all of those. As soon as it sends a "1" back though it should break the Master out of that loop and in to the master's "while(1)" loop shouldn't it?

jerseyguy1996:
As soon as it sends a “1” back though it should break the Master out of that loop and in to the master’s “while(1)” loop shouldn’t it?

Yes but the damage has already been done. If the slave ever has more than one byte in its buffer it will never clear out those bytes faster than the master sends more.

The fix might be as simple as:

 //If you receive a 1 respond with a 1
    if (Serial.read()==1){
      Serial.write(1);
      while (Serial.read()) {} // <---------------------------
    }

Chagrin:

jerseyguy1996:
As soon as it sends a “1” back though it should break the Master out of that loop and in to the master’s “while(1)” loop shouldn’t it?

Yes but the damage has already been done. If the slave ever has more than one byte in its buffer it will never clear out those bytes faster than the master sends more.

The fix might be as simple as:

 //If you receive a 1 respond with a 1

if (Serial.read()==1){
     Serial.write(1);
      while (Serial.read()) {} // <---------------------------
   }

Nice! I will try that. I’m surprised that in the Serial library there isn’t a Serial.purge or something to empty the buffer :slight_smile:

I'm surprised that in the Serial library there isn't a Serial.purge or something to empty the buffer :-)

You are surprised that there is not a way to throw away random amounts of unread data? Why? What possible use could there be for a function like that?

If you REALLY need one, write one. Don't bother posting the code, though.