0

I am using an Arduino Mega connected with a motion sensor and HC05 bluetooth chip to transmit distance data to a larger computer, process it, and send a data back. Currently, I am having trouble receiving/reading bluetooth output received from the program running on my PC which is successfully receiving data from the HC05 chip. I am using the Serial Software library to process serial input/output to the Arduino Mega. Having probed the output of the HC05 chip that serves as return stream of decision data to the Mega, I see that the magnitude of the waveform is around 3V. My guess is that for the Arduino and Software Serial library to process this input, it must be at 5 volts, just like the output, for which I had to build a voltage divider so that I could provided the specified 3.3 V to the HC05 input. Clearly if the output at TXD from the HC05 is 3.3V then the Mega will receive 3.3 volts at PIN4, the receiving pin.

Here is a basic diagram without the sensors or motors. Exact resistances are not provided but should not affect the issue. enter image description here

I have provided the code below for Arduino-side operation.

/*
    This module uses the L293D chip to modulate and power
    two DC motors controlling the wheels of the FryeBot
*/

#include <SoftwareSerial\SoftwareSerial.h>


SoftwareSerial mySerial(4, 2); // RX, TX

/*L293D INPUT 7*/
const int YELLOW_GEAR_MOTOR_BACK = 8;

/*L293D INPUT 2*/
const int YELLOW_GEAR_MOTOR_FORWARD = 9;

/*L293D INPUT 10*/
const int RED_GEAR_MOTOR_BACK = 10;

/*L293D INPUT 15*/
const int RED_GEAR_MOTOR_FORWARD = 11;

/*
    Establish ratio os the rate of the faster slower gear
    to the faster gear to scale the duty cycle of the
    faster gear
*/
const float RED_GEAR_SPEED_TO_YELLOW = 0.97;

/*Constant for full duty cycle for analogWrite*/
const int FULL_ANALOG_MOTOR_SPEED = 255;


const int pingPin = 7;

long duration, cm;


char buf[10];

char current;
String messageHolder;
String message;


void setup() {
    Serial.begin(9600);
    mySerial.begin(9600);

    /* add setup code here */
    pinMode(YELLOW_GEAR_MOTOR_FORWARD, OUTPUT);
    pinMode(YELLOW_GEAR_MOTOR_BACK, OUTPUT);
    pinMode(RED_GEAR_MOTOR_BACK, OUTPUT);
    pinMode(RED_GEAR_MOTOR_FORWARD, OUTPUT);

}

void loop() {

    pinMode(pingPin, OUTPUT);
    digitalWrite(pingPin, LOW);
    delayMicroseconds(2);
    digitalWrite(pingPin, HIGH);
    delayMicroseconds(5);
    digitalWrite(pingPin, LOW);

    pinMode(pingPin, INPUT);
    duration = pulseIn(pingPin, HIGH);

    cm = microsecondsToCentimeters(duration);

    Serial.print(cm);
    Serial.print("cm");
    Serial.println();

    /* Load the distance value into a buffer */
    String sendValue = String(cm);
    sendValue.toCharArray(buf, sizeof(buf));

    /* Write the buffer with a space after for parsing*/
    mySerial.write(buf);
    mySerial.write(' ');

    /* Stop if too close to an object */
    //Serial.print("current: ");
    //Serial.print((int)current);
    Serial.println();

    if (message != "STOP_MOTORS") {

        analogWrite(YELLOW_GEAR_MOTOR_FORWARD, (int)(RED_GEAR_SPEED_TO_YELLOW * (float)FULL_ANALOG_MOTOR_SPEED));
        digitalWrite(YELLOW_GEAR_MOTOR_BACK, LOW);

        digitalWrite(RED_GEAR_MOTOR_BACK, LOW);
        digitalWrite(RED_GEAR_MOTOR_FORWARD, HIGH);
    }
    else {
        digitalWrite(YELLOW_GEAR_MOTOR_FORWARD, 0);
        digitalWrite(YELLOW_GEAR_MOTOR_BACK, LOW);

        digitalWrite(RED_GEAR_MOTOR_BACK, LOW);
        digitalWrite(RED_GEAR_MOTOR_FORWARD, LOW);
    }

    current = mySerial.read();
    Serial.print((int)current);
    Serial.println();

    if (current == ' ') {
        message = messageHolder;
        messageHolder = "";
    }
    else {
        messageHolder += current;
    }

    delay(100);

}


long microsecondsToCentimeters(long microseconds) {
    // The speed of sound is 340 m/s or 29 microseconds per centimeter.
    // The ping travels out and back, so to find the distance of the
    // object we take half of the distance travelled.
    return microseconds / 29 / 2;
}
  • 4
    I am using the Serial Software library to process serial input/output to the Arduino Mega. ... Why on earth would you do that when you have three hardware UARTs...?!?!?!?! – Majenko Aug 19 '17 at 9:16
  • I mean is the hardware implementation significantly better than the software one – Jack Frye Aug 19 '17 at 14:59
  • 1
    Well.... yes? Massively better. You should only use SoftwareSerial when you absolutely must. – Majenko Aug 19 '17 at 15:40
  • Ill give the UART a try and get back to you with the results. I'm assuming I should keep the voltage divider leading to the RX of the HC05? – Jack Frye Aug 19 '17 at 16:22
  • 1
    Why would it? And what would it matter if it did? It would lag BOTH sides of the signal and the signal would be the same with a slight offset. Mind, what do you think it is that is used in high frequency oscillators and things? BJTs. They can go to many hundreds of MHz no problem. – Majenko Aug 19 '17 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.