Go Down

Topic: Arduino robot remote control buttons and voice (Read 1 time) previous topic - next topic

PaulS

Quote
I know the code is a mess
Good, because I was going to point that out.

You took code that properly received packets with end markers, and stopped sending end markers. That was the wrong approach.

You are now hoping that an entire packet, and NOTHING ELSE, will arrive between times that you read the bluetooth data. Can you see why that is a bad idea?

You incorporated a useless delay(), in the hopes that that would deal with the fact that serial communication is slow. That is NOT the proper way to do that. End of packet markers ARE.

You KNOW that you are not going to send novels, so using the String class is not necessary. You CAN use a char array of some fixed length.

I can't image why "start" means turn an LED on. I would think that "on" would mean that. YMMV.
The art of getting good answers lies in asking good questions.

Sirverian

So i need to use end of packet markers and instead sending strings i must send just chars? And how i use end of packet markers?

Code: [Select]
#include <SoftwareSerial.h>
#define led 13
SoftwareSerial BT(4, 2);//TX,RX



void setup() {
  BT.begin(9600);
  Serial.begin(9600);
  pinMode(13, OUTPUT);

}

void loop() {
  while (BT.available()) {
    char c = BT.read();
    if(char=='#') break;
  }
  if (data.length() > 0) {
    Serial.println(data);
    if (data =='O')
    {
      digitalWrite(13, HIGH);
    }

    else if (data =='S')
    {
      digitalWrite(led, LOW);
    }
   
  }
  data="";
 

}



Or i can use "<" for begining of package and ">" for end of package.


Code: [Select]


#define StartPackage='<'
#define StopPackage='>'
bool start;
bool stop;

void setup(){
    BT.begin(9600);
    Serial.begin(9600);
    pinMode(13, OUTPUT);
}

void loop()
{
  while (BT.available() > 0)
  {
    char c = BT.read();


    if (c == StartPackage)
    { 
      start = true;
      stop = false;
    }

    else if (c ==EndPackage)
    {
       Stop= true;
      break;
    }


Can you give me an exemple?

PaulS

The art of getting good answers lies in asking good questions.


Go Up