Hey guys,
Meaps here. I just wanted to ask a question about a problem I simply cannot resolve. Skip the next paragraph for the important info. Sorry for the wall of text. Just wanted to be as thorough as possible.
I just started programming with arduinos during the past 3-4 months. Been doing good on my own for a while, but I hit a wall, and a pretty frustrating one. Usually I can work around the problems by searching google, but this time I couldn't find much. Yes, I know, there are a lot of threads about connecting two HC-05 modules together for a bluetooth connection, and believe me, I've searched for over a week trying to find help. There isn't a post or an archive that I haven't read on the subject. On that note, let's begin:
My goal is to send a voltage read from my arduino nano (voltage of a lipo battery pack) through the HC-05 modules to a teensy 3.2 and display an approximate voltage through an RGB anode led. My project's goal is to create a custom remote for my diy electric longboard. That is why I have other parts of my code sending angle values for the motor speed controllers.
My issue is that no matter what I try, I seem to get inconsistent values sent or received through my HC-05. I'll send an int (annalogValueMapped in the longboard code) and receive random values like a stream of eights and some high value like 144 every now and then on my remote code (batteryBt) when printing to the serial monitor. Please note that the teensy uses two separate serial leads for USB and tx0 and rx0 unlike the nano. As a side note, I am 100% certain that the values are reading correctly on the nano (my voltage should read 3.7volts, which the nano appropriately prints on the serial monitor while debugging without the HC-05 factored in). This was all triple checked after countless hours of testing.
Here is what I expect to happen, my nano sends out a value between 0 - 255 through bluetooth with the analogValueMapped integer. Then, my teesny should receive that value, store it in batteryBt, display it on my serial monitor and then calculate and average of all the last 25 received values by converting the 0-255 value to the reference voltage which is about 4.85V.
Here is what is even more interesting, notice my remote code also reads an analog value from a potentiometer, then remaps it to a 0-179 value, and sends it to the lonboard (nano). This is because I want to control the speed of the motors with the angle of the pot. I hooked up the longboard to the nano, and reset the max/neutral/min positions accordingly.When testing, the motors worked perfectly with the angle of the potentiometer I was inputting... Which means it was sending the appropriate values between 0 and 179 to the speed controllers without a problem through the HC-05 modules...
More info:
My remote is set as the master (teensy) and the longbaord is set as the slave (nano). I doubt this has any effect but if it helps any of you now you know ![]()
Here is the code on my longboard (nano, I realise it's sloppy, sorry for that):
#include <Servo.h>
Servo esc;
int incThrottle;
int ledPin = 13;
int analogValue = 0;
void setup() {
analogReference(EXTERNAL);
analogRead(A0);
pinMode(ledPin, OUTPUT);
esc.attach(9);
Serial.begin(38400); //Initialize serial bluetooth connection
}
void loop() {
analogValue = (analogRead(A0));
int analogValueMapped = map(analogValue, 0, 1023, 0, 255);
Serial.write(analogValueMapped);
if(Serial.available()>0){
incThrottle = Serial.read(); //Updates incThrottle when receiving Serial
}
if(analogValue > 600){
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
esc.write(incThrottle); //Sends angle to esc
delay(1);
}
and here is the code in the remote (teensy, idem):
int outThrottle;
int outThrottlePin = 14;
int ledPin = 13;
int redPin = 15;
int greenPin = 16;
int bluePin = 17;
double batteryVolt;
int batteryBt;
float avgBatt;
const int numReadings = 25;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
double total = 0; // the running total
double average = 0; // the average
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(outThrottlePin, INPUT);
delay(1000);
Serial1.begin(38400);
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}
void loop() {
outThrottle = analogRead(outThrottlePin);
outThrottle = map(outThrottle, 0, 1023, 0, 179);
Serial1.write(outThrottle);
if(Serial1.available()>0){
batteryBt = Serial1.read(); //Gets battery voltage
avgBatt = (((batteryBt*4.85)/256L));
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = avgBatt;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = (total) / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
}
if (average > 4.1) {
setColor(255, 255, 255); // white
}
else{
if (average > 3.9 && average <= 4.1){
setColor(0, 255, 0); // green
}else{
if (average > 3.7 && average <= 3.9){
setColor(255, 255, 0); // yellow
}else {
if (average > 3.5 && average <= 3.7){
setColor(255, 0, 0); // red
}else{
if (average > 3.4 && average <= 3.5) {
blinkRed();
}
}
}
}
}
delay(1);
if(outThrottle > 90){
digitalWrite(ledPin,HIGH);
}
else{
digitalWrite(ledPin,LOW);
}
}
void setColor(int red, int green, int blue)
{
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
void blinkRed(){
setColor(255, 0, 255);
}
Any help would be greatly appreciated. As for the actual circuit, I am 99% sure everything is fine. It's pretty simple after all. One last note, I double checked everything except the baud rate set in for the HC-05 modules, although I am once again pretty certain of having set both to 38 400. Will be making sure tomorrow, I don't have time anymore for tonight ![]()
Will be making sure to post my full step by step guide to the remote/receiver process when I'm done, hopefully will be able to help some others out there.
Just incase, here is the specific wiring for both modules:
RX -> TX
TX -> RX
Same for both.