Go Down

Topic: Arduino Mega Bluetooth connection to Arduino Uno (Read 307 times) previous topic - next topic

justus100

Jan 01, 2016, 05:54 pm Last Edit: Jan 01, 2016, 09:59 pm by justus100
Dear Community,
Im trying to build a self driving robot car with an arduino uno and an arduino mega.
Both Arduinos have to send and to receive Information and Values from the other.
Im using Software Serial Library for the connection. In the Program, i will attach on this topic, is a example code I tried to run to send any data. Motor_x is just an poti. The Programm is almost copied from another forum and adjusted for my robot. Im using a HC-05 and a HC-06. The Bluetooth adapters connect with each other and send the data when i change the poti.
The Problem is, that the receiving Arduino doesn't get the right values. The sending arduino sends the right values as I can see at the serial Monitor. I don't know why the values have been changed, the UART rate is 9600 on both modules.
I hope anybody here knows the problem and can help me.
If this is the wrong topic for my problem please tell me so I can change it.
Thanks for help.
Justus

Sent:

Code: [Select]
#include <SoftwareSerial.h>
#define HC_05_TXD 2
#define HC_05_RXD 3
#define START_CMD_CHAR '*'
#define END_CMD_CHAR '#'

SoftwareSerial BTSerial(HC_05_TXD, HC_05_RXD); // RX | TX

String outText;
int Motor_x = A0;
int LedPin = 52;
int motorspeed, old_motorspeed;
int reference;
int ledstat, old_ledstat;
int zustand;

void setup() 
{
  pinMode(Motor_x, INPUT);
  pinMode(LedPin, OUTPUT);
  digitalWrite(LedPin, HIGH);
  reference = analogRead(Motor_x);
  Serial.begin(38400);   // For the Arduino IDE Serial Monitor
  BTSerial.begin(9600);  // HC-05 default speed
  }


void loop() 
{

  motorspeed = analogRead(Motor_x);
  motorspeed = map(motorspeed, reference,0,0,255);
  motorspeed = constrain(motorspeed, 0, 255);

  if(digitalRead(LedPin)==LOW){
    ledstat = HIGH;
  }

  else{
    ledstat = LOW;
  }

  if (motorspeed!=old_motorspeed || ledstat!=old_ledstat){
    outText = " ";
    outText += START_CMD_CHAR;
    outText += motorspeed;
    outText += ",";
    outText += ledstat;
    outText += END_CMD_CHAR;
    //Serial.println("Datenstrint: ");
    Serial.println(outText);
    BTSerial.println(outText);
    //Serial.println("gesendet!");

     old_motorspeed = motorspeed;
     old_ledstat = ledstat;
  }
}



Receive

Code: [Select]
#include <SoftwareSerial.h>
#define HC_06_TXD 10
#define HC_06_RXD 11
#define START_CMD_CHAR '*'
#define END_CMD_CHAR '#'

SoftwareSerial BTSerial(HC_06_TXD, HC_06_RXD);

int motorspeed;
int ledstat;

int Motor_x = 9;
int LedPin = 8;

void setup() {
  BTSerial.begin(9600);
  Serial.begin(9600);
  pinMode(Motor_x, OUTPUT);
  pinMode(LedPin, OUTPUT);
}

void loop() {
  while(BTSerial.available()){
    delay(1);
    Serial.println(BTSerial.read());
  }
  if (BTSerial.available()){
    char get_char = ' ';
    get_char = BTSerial.read();
    if(get_char != START_CMD_CHAR) return;
    delay(2);
    motorspeed = BTSerial.parseInt();
    Serial.println(motorspeed);
    delay(2);
    ledstat = BTSerial.parseInt();
    Serial.println(ledstat);
    if(ledstat == 1){
      digitalWrite(LedPin, HIGH);
    }
    else{
      digitalWrite(LedPin, LOW);
    }
    if(motorspeed >=0 && motorspeed <=255){
      analogWrite(Motor_x,motorspeed);
    }
  }
}

zoomkat

Please read #7 below about posting code so people can see it. Do you have examples of what is sent and what is received?

http://forum.arduino.cc/index.php/topic,148850.0.html
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

justus100

Thank you for the quick answer, I've changed my Post and I hope its okay now.
I will post the example tomorrow.

PaulS

Code: [Select]
  while(BTSerial.available()){
    delay(1);
    Serial.println(BTSerial.read());
  }

Read, and show, any data that came from the sender, via the bluetooth device.

Code: [Select]
  if (BTSerial.available()){
    char get_char = ' ';
    get_char = BTSerial.read();
    if(get_char != START_CMD_CHAR) return;
    delay(2);

Then, expect it to still be available to be read again. That is most certainly not a valid expectation.
The art of getting good answers lies in asking good questions.

Go Up