Bluetooth Modul Empfangsprobleme

Hallo

Ich habe einen Arduino Mega für das Empfangen von seriellen Daten an Pin 0 und 1 sowie ein Bluetooth Modul, welches an Pin 11 empfangen soll und diese Daten dann an Pin 28, resp. 30 per SoftwareSerial weitergibt.
Nun wenn ich den folgenden Testsketch für das Empfangen von Bluetoothdaten auf einen UNO lade, dann funktioniert es ohne Probleme:

#include <SoftwareSerial.h>   //adds library for software serial (Bluetooth module)

const unsigned long Baudrate = 9600;   //starts serial ports with a baudrate by 9600 b/s
const unsigned long BaudrateBT = 19200; //starts serial port for the Bluetooth module with a baudrate by 19200 b/s

const byte Bluetooth = 11;         //Bluetooth Module connected to pin D11

String BT_RX;       //reads the letter from Bluetooth serial

//create software serial ports
SoftwareSerial BT(Bluetooth, 20);  //RX, TX pin from Bluetooth module

void setup() {

  BT.begin(BaudrateBT);     //starts serial port with a baudrate by "BaudrateBT" (b/s)
  BT.setTimeout(3);         //sets the maximum milliseconds to wait for serial data
  Serial.begin(Baudrate);     //starts serial port with a baudrate by "BaudrateMP" (b/s)

}

void loop() {

//read software serial and send it via software serial xx to the device
  while (BT.available() > 0) {              //while serial is available, make...
    BT_RX = BT.readString();               //store received string from serial to BT_RX
    Serial.print(BT_RX);                        //send string from BT_RX to the serial pin
    Serial.print('\r');                         //send carriage return to the serial pin
  }

}

Wenn ich nun aber auf meinem Mega in den seriellen Empfangssketch diesen Teil einfüge, dann empfängt er keine Daten mehr per Bluetooth. Oder es macht den Anschein, weil der serielle Monitor nichts mehr anzeigt. Ist hier etwas zu beachten, was beim UNO anders ist? Oder was könnte hier das Problem sein?

 /**SETTINGS**/
const unsigned long Baudrate = 38400;   //starts serial ports with a baudrate by 38400 b/s
const unsigned long BaudrateBT = 19200; //starts serial port for the Bluetooth module with a baudrate by 19200 b/s
const unsigned long BaudrateMP = 9600;  //starts serial port wit ha baudrate by 9600 b/s
const unsigned long BaudrateTC = 2400;  //starts serial port wit ha baudrate by 2400 b/s

const byte RSSIsensitivity = 40;        //received signal strength from Xbee controls the range until signal is lost and the fail safe will stop the motors,
                                        //the Xbee's standard edition will give a value between 0 and 92 and the pro version a value between 0 and 100



const byte DDstop = 90;         //Dome drive, basic position joystick (stop position)
const byte FDLRstop = 90;       //Foot drive left/right, basic position joystick (stop position)
const byte FDUDstop = 90;       //Foot drive forward/backward, basic position joystick (stop position)

const byte MPanel = 28;      //MPanel connected to pin D28
const byte TClight = 30;     //TClight connected to pin D30

const byte XbeeRSSI = 10;    //Xbee RSSI pin connected to pin D10
const byte Bluetooth = 11;   //Bluetooth Module connected to pin D11

int DP1 = 0;    
int DP2 = 0;    
int DP3 = 0;    
int DP4 = 0;    
int DP5 = 0;    
int DP6 = 0;    
int HTLR = 0;   
int HTUD = 0;   
int HF = 0;     
int HT = 0;     
int HR = 0;     
int BUT16 = 0;  
int BUT19 = 0;  
int BUT20 = 0;  
int BUT21 = 0;  
int BUT22 = 0;  
int BUT23 = 0;  

char var = 0;       //reads the letter from serial

int rssi = 0;       //variable for RSSI value from Xbee, the standard edition will give a value between 0 and 92 and the pro version a value between 0 and 100

String BT_RX;       //reads the letter from Bluetooth serial

#include <SoftwareSerial.h>   //adds library for software serial (Bluetooth module)

//create software serial ports
SoftwareSerial BT(Bluetooth, 20);  //RX, TX pin
SoftwareSerial MP(12, MPanel);     //RX, TX pin
SoftwareSerial TC(13, TClight);    //RX, TX pin

void setup() {

  Serial.begin(Baudrate);   //starts serial port with a baudrate by "Baudrate" (b/s)
  Serial1.begin(Baudrate);  //starts serial port with a baudrate by "Baudrate" (b/s)
  Serial2.begin(Baudrate);  //starts serial port with a baudrate by "Baudrate" (b/s)
  Serial3.begin(Baudrate);  //starts serial port with a baudrate by "Baudrate" (b/s)

  BT.begin(BaudrateBT);     //starts serial port with a baudrate by "BaudrateBT" (b/s)
  BT.setTimeout(3);         //sets the maximum milliseconds to wait for serial data
  MP.begin(BaudrateMP);     //starts serial port with a baudrate by "BaudrateMP" (b/s)
  TC.begin(BaudrateTC);     //starts serial port with a baudrate by "BaudrateTC" (b/s)

  //definition of input/output pins
  pinMode(XbeeRSSI, INPUT_PULLUP);   //pin XbeeRSSI is an input

}

void loop() {

  unsigned long currentMillis = millis();               //count milliseconds

//RSSI value from the Xbee to detect when the signal is missing
  rssi = pulseIn(XbeeRSSI, LOW, 200);                   //read the RSSI value from Xbee (the XBee’s RSSI PWM period is 200µs), the standard edition will give a value between 0 and 92 and the pro version a value between 0 and 100

  if(rssi <= RSSIsensitivity){                          //if RSSI value is less or equal to RSSIsensitivity (inside controllable range), then make...

//read serial and store it to designation or send it via serial xx to the next Arduino
  while(Serial.available() > 0){                        //while serial is available, make...
    var = Serial.read();                                //store first character from serial to var
    switch(var){                                        
      case 'A' : DP1 = Serial.parseInt();               //if var is equal to A, then store second character from serial to DP1
                 break;                                 //finish the command and return
      case 'B' : DP2 = Serial.parseInt();
                 break;
      case 'C' : DP3 = Serial.parseInt();
                 break;
      case 'D' : DP4 = Serial.parseInt();
                 break;
      case 'E' : DP5 = Serial.parseInt();
                 break;
      case 'F' : DP6 = Serial.parseInt();
                 break;
      case 'G' : Serial2.print('G');                    //if var is equal to G, then send the character G to serial2
                 Serial2.println(Serial.parseInt());    //send second character from serial to serial2
                 Serial2.flush();                       //waits for the transmission of outgoing serial2 data to complete
                 break;
      case 'H' : Serial2.print('H');
                 Serial2.println(Serial.parseInt());
                 Serial2.flush();
                 break;
      case 'I' : Serial2.print('I');
                 Serial2.println(Serial.parseInt());
                 Serial2.flush();
                 break;
      case 'J' : Serial2.print('J');
                 Serial2.println(Serial.parseInt());
                 Serial2.flush();
                 break;
      case 'K' : Serial2.print('K');
                 Serial2.println(Serial.parseInt());
                 Serial2.flush();
                 break;
      case 'L' : Serial2.print('L');
                 Serial2.println(Serial.parseInt());
                 Serial2.flush();
                 break;
      case 'M' : HTLR = Serial.parseInt();
                 break;
      case 'N' : HTUD = Serial.parseInt();
                 break;
      case 'O' : Serial2.print('O');
                 Serial2.println(Serial.parseInt());
                 Serial2.flush();
                 break;
      case 'P' : Serial2.print('P');
                 Serial2.println(Serial.parseInt());
                 Serial2.flush();
                 break;
      case 'Q' : Serial3.print('Q');
                 Serial3.println(Serial.parseInt());
                 Serial3.flush();
                 break;
      case 'R' : Serial3.print('R');
                 Serial3.println(Serial.parseInt());
                 Serial3.flush();
                 break;
      case 'S' : Serial3.print('S');
                 Serial3.println(Serial.parseInt());
                 Serial3.flush();
                 break;
      case 'T' : Serial3.print('T');
                 Serial3.println(Serial.parseInt());
                 Serial3.flush();
                 break;
      case 'U' : Serial3.print('U');
                 Serial3.println(Serial.parseInt());
                 Serial3.flush();
                 break;
      case 'V' : Serial3.print('V');
                 Serial3.println(Serial.parseInt());
                 Serial3.flush();
                 break;
      case 'W' : Serial3.print('W');
                 Serial3.println(Serial.parseInt());
                 Serial3.flush();
                 break;
      case 'X' : Serial1.print('X');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'Y' : Serial1.print('Y');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'Z' : Serial1.print('Z');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'a' : HF = Serial.parseInt();
                 break;
      case 'b' : HT = Serial.parseInt();
                 break;
      case 'c' : HR = Serial.parseInt();
                 break;
      case 'd' : Serial1.print('d');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'e' : Serial1.print('e');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'f' : Serial1.print('f');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'g' : Serial1.print('g');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'h' : Serial3.print('h');
                 Serial3.println(Serial.parseInt());
                 Serial3.flush();
                 break;
      case 'i' : Serial1.print('i');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'j' : Serial1.print('j');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'k' : Serial1.print('k');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'l' : Serial3.print('l');
                 Serial3.println(Serial.parseInt());
                 Serial3.flush();
                 break;
      case 'm' : Serial1.print('m');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'n' : Serial1.print('n');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'o' : Serial1.print('o');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'p' : Serial3.print('p');
                 Serial3.println(Serial.parseInt());
                 Serial3.flush();
                 break;
      case 'q' : Serial1.print('q');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'r' : Serial1.print('r');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 's' : Serial1.print('s');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 't' : BUT16 = Serial.parseInt();
                 break;
      case 'u' : Serial1.print('u');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'v' : Serial1.print('v');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case 'w' : BUT19 = Serial.parseInt();
                 break;
      case 'x' : Serial3.print('x');
                 Serial3.println(Serial.parseInt());
                 Serial3.flush();
                 break;
      case 'y' : BUT21 = Serial.parseInt();
                 break;
      case 'z' : Serial1.print('z');
                 Serial1.println(Serial.parseInt());
                 Serial1.flush();
                 break;
      case '@' : BUT22 = Serial.parseInt();
                 break;
      case '$' : BUT23 = Serial.parseInt();
                 break;
      
    }
  }

  }
  else if(rssi > RSSIsensitivity){          //if RSSI value is greater than RSSIsensitivity (out of controllable range), then make...
    Serial1.print('X');                     //send the letter X (for dome drive) by serial1
    Serial1.println(DDstop);                //dome movement stopped (left < 90 < right, 80 slow left, 0 full throttle resp. 100 slow right, 180 full throttle)
    Serial1.print('Y');                     //send the letter Y (for foot drive left/right) by serial1
    Serial1.println(FDLRstop);              //travel stopped (right < 90 < left, 80 slow right, 0 full throttle resp. 100 slow left, 180 full throttle)
    Serial1.print('Z');                     //send the letter Z (for foot drive forward/backward) by serial1
    Serial1.println(FDUDstop);              //travel stopped (backwards < 90 < forwards, 80 slow backward, 0 full throttle resp. 100 slow forward, 180 full throttle)
  }

//read software serial and send it via software serial xx to the device
  while (BT.available() > 0) {              //while serial is available, make...
    BT_RX = BT.readString();                //store received string from serial to BT_RX
    MP.print(BT_RX);                        
    MP.print('\r');                         
    TC.print(BT_RX);                        
    TC.print('\r');                         
  }
  
}

Grüsse

Stef

Setze Deinen Code bitte in Codetags [code] davor und [/code] dahinter oder gehe in der IDE auf Bearbeiten - Für Forum kopieren und füge es hier ein.
Das kannst Du auch noch nachträglich ändern.
Außerdem formatiere bitte den Code ordentlich (Strg+T in der IDE hilft Dir dabei).

Gruß Tommy

Habe ich gemacht, wusste aber nicht, dass das auf eine neue Zeile muss! Danke :slight_smile:

Hm. Falsche Frage.
Du baust zwei vollkommen unterschiedliche Sketche.

Nimm den Sketch vom Uno, zieh den auf den Mega und schau was passiert.

Was glaubst Du denn, wer sich die fast 300 Zeilen durchastest, blos um rauszufinden, das die keinerlei Information zum Thema hergeben?

Naja, komplett verschieden sind sie nicht. Ich hatte auf dem Mega den seriellen Empfangssketch und wollte diesen mit Bluetooth erweitern. Dann habe ich nur den funktionierenden Bluetooth Sketch vom Uno in den seriellen Empfangssketch des Megas kopiert.

Ich habe den ganzen Sketch gepostet um nicht wieder darauf hingewiesen zu werden, dass ich nicht alles angebe. Ich kann den seriellen Teil nach dem Empfangen auch rausnehmen, dann entfallen sehr viele Zeilen.

Die Idee mit dem reinen Bluetooth Teil welcher auf dem Uno funktioniert hat, auf dem Mega zu testen ist aber eine gute Idee. Das werde ich machen um das mal auszuschliessen. Werde dann wieder berichten. Wenns da auch funktioniert, werde ich vom obenstehenden Sketch mal das "unnötige" rausnehmen und nur die Hauptfunktionen des Empfangens drin lassen. Vielleicht sieht dann jemand eine mögliche Fehlerquelle.

Danke bis dahin und Gruss

Stef

Kein Wunder, wenn der serielle Monitor nichts mehr anzeigt. Du blockierst den ja auch mit den seriellen Daten aud D0 und D1. Warum verwendest du am Mega nicht eine der anderen seriellen Schnittstellen ?
D0 und D1 sollten frei bleiben, da hier der Serial-USB-Adapter dran hängt.

Hallo zusammen

Ich habe heute etwas rumgespielt und verschiedenes versucht. Unter anderem andere Pins zu definieren, mal RX > TX, mal RX < TX. Auch die Analogen Pins als digitale definiert. Hat aber alles nichts gebracht.
Die Lösung zum Problem war, wenn ich den RX pin nicht brauche (der zuerst definiert werden muss), ein minus vor die Pin-Nummer zu setzen, dann funktioniert es! Nun die Frage, warum ist das so? Muss ich am RX Pin immer etwas angeschlossen haben wenn ich diesen definiere? Darf dieser nicht unbesetzt bleiben?
Und was hat es mit dem negativ definierten Pin auf sich? Was ist hier anders oder was passiert hierbei? Es gibt ja keine negativen Pin-Nummern.

Hier noch der Sketch gekürzt aufs Minimum welcher funktioniert hat:

 /**SETTINGS**/
const unsigned long Baudrate = 38400;   //starts serial ports with a baudrate by 38400 b/s
const unsigned long BaudrateBT = 19200; //starts serial port for the Bluetooth module with a baudrate by 19200 b/s
const unsigned long BaudrateMP = 9600;  //starts serial port wit ha baudrate by 9600 b/s
const unsigned long BaudrateTC = 2400;  //starts serial port wit ha baudrate by 2400 b/s

const byte MPanel = 28;      //MPanel connected to pin D28
const byte TClight = 30;     //TClight connected to pin D30

const byte Bluetooth = 11;   //Bluetooth Module connected to pin D11

char var = 0;       //reads the letter from serial

String BT_RX;       //reads the letter from Bluetooth serial

#include <SoftwareSerial.h>   //adds library for software serial (Bluetooth module)

//create software serial ports
SoftwareSerial BT(Bluetooth, 20);  //RX, TX pin
SoftwareSerial MP(-12, MPanel);     //RX, TX pin => If RX is not used, it must have a minus in front of the pin number!
SoftwareSerial TC(-13, TClight);    //RX, TX pin => If RX is not used, it must have a minus in front of the pin number!

void setup() {

  Serial.begin(Baudrate);   //starts serial port with a baudrate by "Baudrate" (b/s)

  BT.begin(BaudrateBT);     //starts serial port with a baudrate by "BaudrateBT" (b/s)
  BT.setTimeout(3);         //sets the maximum milliseconds to wait for serial data
  MP.begin(BaudrateMP);     //starts serial port with a baudrate by "BaudrateMP" (b/s)
  TC.begin(BaudrateTC);     //starts serial port with a baudrate by "BaudrateTC" (b/s)

}

void loop() {

//read serial and store it to designation or send it via serial xx to the next Arduino
  while(Serial.available() > 0){                        //while serial is available, make...
    var = Serial.read();                                //store first character from serial to var
    /*.....mach was mit dem Empfangenen....*/
  }
  
//read software serial and send it via software serial xx to the device
  while (BT.available() > 0) {              //while serial is available, make...
    BT_RX = BT.readString();                //store received string from serial to BT_RX
    MP.print(BT_RX);                        
    MP.print('\r');                         
    TC.print(BT_RX);                        
    TC.print('\r');                         
  }
  
}

Grüsse

Stef

Ist doch prima, wenn es funktioniert, auch wenn ich dein geschreibsel nicht verstehe.
Und danke, dass du die Fragen nicht beantwortest und auf unsere Anmerkungen nicht eingehst.
So macht helfen Spass.

in der SoftSerial.h steht das

SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);

mit -1 hast du den rxPIN somit auf 255 festgelegt.

warum verwendest du nicht nicht die verfügbaren HardwareSerial 1, 2, oder 3 am Mega?

Zur Frage bezüglich warum nicht eine andere serielle Schnittstelle verwendet wird => Ich habe diese schon alle in Gebrauch! Dass D0/D1 mit dem PC kommuniziert und nicht anders verwendet werden sollte ist grundsätzlich klar, aber auch diese ist (wenn nicht am PC angeschlossen) schon vergeben und in Gebrauch...
Im ersten Sketch könnte man dies sehen am Anfang bei den Serial.begin, dass da alle angeschlossen sind.

Ach so, danke. Also wäre das theoretisch kein Problem und kann so verwendet werden.
Was kommte denn bei raus für -12, resp. -13 wie ich es definiert habe? Kann man das umrechnen?

Du machst da einfach einen overflow auf eine vorzeichenlose 1 byte Variable. Also ist 0 - 1 quasi 255.

Imho kannst da gleich überall 255 hinschreiben.

Das würde bedeuten, ich kann einfach einen Pin definieren, den es nicht gibt auf dem Mega? Also alles grösser/gleich 70? Hat dies einen Einfluss auf irgendwas/unerwartete Reaktionen?

Das kommt darauf an, was Du erwartest. Wenn Du erwartest, dass da etwas Sinnvolles passiert, wird die Reaktion unerwartet sein :wink:

Gruß Tommy

Ich meine, wenn ich den RX Pin nicht brauche und ihn mit >=70 definiere, was auf dem Mega ja nicht mehr vorkommt, dann passiert einfach nichts? Oder hat dies dann unerwartete Reaktionen mit der Lib?

Hmm... das verwirrt :slight_smile: Mit anderen Worten: Wenn ich möchte, dass Daten empfangen werden, dann erhalte ich keine. Wenn ich aber keine empfangen möchte, dann bin ich so auf dem richtigen Weg?

müsstest in der lib nachsehen, aber VERMUTLICH so lange du kein available oder read machst, KÖNNTE es egal sein.

edit:
zum Teil sind Absicherungen gegen schmarren drinnen:

 // Only setup rx when we have a valid PCINT for this pin
  if (digitalPinToPCICR((int8_t)_receivePin)) {

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