Hi all i'm building a radar system with a lidar and need to communicate distance and angle information via bluetooth. My code worked fine using the serial connection but having issues with the bluetooth.
As it stands i have the HC-05 module successfully sending data to the arduino ide and i can see this on the serial port /dev/cu.HC-05-DevB. However when i ask processing to read this port i get nothing.
Arduino Code
#include <AccelStepper.h>
#include <SoftwareSerial.h>
#include "TFMini.h"
#include <EEPROM.h>
#include <stdio.h>
// Setup Serial Port LIDAR
SoftwareSerial mySerial(12, 13);
TFMini tfmini; // Uno RX (TFMINI TX), Board TX (TFMINI RX)
//Setup Pins for Motor Control
#define dirPin 4
#define stepPin 5
#define motorInterfaceType 1
AccelStepper stepper = AccelStepper(motorInterfaceType, stepPin, dirPin);
int address = 0;
int degree = 0;
void setup() {
Serial.begin(9600);
// Wait for Serial Port to Connect
while (!Serial);
Serial.println ("Serial Port Found");
mySerial.begin(TFMINI_BAUDRATE);
tfmini.begin(&mySerial);
//degree = EEPROM.read(address);
}
void loop() {
// put your main code here, to run repeatedly:
motor ();
}
void Lidar()
{
uint16_t dist = tfmini.getDistance();
uint16_t strength = tfmini.getRecentSignalStrength();
degree = degree + (18 * 0.3428571429);
degree = round(degree);
Serial.print(degree);
Serial.print(",");
Serial.print(dist);
Serial.print(".");
Serial.print("\n");
if(degree == 360){
degree = 0;
}
//EEPROM.write(address,degree);
//Serial.println(strength);
}
void motor ()
{
// Set the speed in steps per second:
digitalWrite(dirPin,HIGH);
for(int x = 0; x < 10; x++) { //200 steps per revolution
digitalWrite(stepPin,HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin,LOW);
delayMicroseconds(1000);
}
Lidar();
}
Processing Code
import java.awt.event.KeyEvent;
import processing.serial.*;
import java.io.IOException;
Serial myPort;
PFont Font1;
int LineWidth = 9;
String noObject;
String val;
int Distance;
int Angle;
float minRange = 30; //in metres
int maxRange = 66000;
float pixsDistance;
String data = "";
String angle = "";
String distance = "";
int iAngle, iDistance;
int index1 = 0;
void setup()
{
size(1400,800);
smooth();
myPort = new Serial(this,"/dev/cu.HC-05-DevB", 9600);
myPort.bufferUntil('.');
Font1 = loadFont("Arial-Black-30.vlw");
}
void draw()
{
fill(98, 245, 31);
textFont(Font1);
noStroke();
fill(0,10);
rect(0, 0, width, height);
fill(30, 250, 60); //Green Colour
drawRadar();
drawLine();
drawObject();
drawText();
}
void serialEvent(Serial myPort)
{
data = myPort.readStringUntil('.');
data = data.substring(0, data.length() - 1);
index1 = data.indexOf(","); // find the character ',' and puts it into the variable "index1"
angle = data.substring(0, index1); // read the Angle value from position "0" to position of the variable index1
distance = data.substring(index1 + 1, data.length()); // read the Distance value from position "index1" to the end of the data
// converts the String variables into Integer
iAngle = int(trim(angle));
iDistance = int(distance);
}
void drawRadar()
{
pushMatrix();
translate(width/2, height);
noFill();
strokeWeight(1);
stroke(0,120,0);
circle(0, -400, 150); //1m
circle(0, -400, 300); //2m
circle(0, -400, 450); //3m
circle(0, -400, 600); //4m
circle(0, -400, 750); //5m
line(0, -400 + 375 + 20, 0, -775 - 20);
line(-375 - 20, -400, 375 + 20, -400);
line(0, -400, 346.4101615, -400 - 200);
line(0, -400, 200, -400 - 346.4101615);
line(0, -400, 346.4101615, -400 + 200);
line(0, -400, 200, -400 + 346.4101615);
line(0, -400, -346.4101615, - 400 - 200);
line(0, -400, -200, -400 - 346.4101615);
line(0, -400, -346.4101615, - 400 + 200);
line(0, -400, -200, -400 + 346.4101615);
popMatrix();
}
void drawLine()
{
pushMatrix();
strokeWeight(LineWidth);
stroke(15, 125, 30);
translate(width/2, height/2);
line(0, 0, -(height) * sin(radians(iAngle +180)), (height)*cos(radians(iAngle+180)));
popMatrix();
}
void drawObject()
{
pushMatrix();
translate(width/2,height/2);
strokeWeight(LineWidth);
stroke(255, 10, 10); // Red Colour
pixsDistance = ((215+(iDistance-25)*(450/75.5))/10)+10;
if (iDistance > minRange && iDistance < maxRange) {
line(-pixsDistance * sin(radians(iAngle+180)), pixsDistance * cos(radians(iAngle+180)), -(width) * sin(radians(iAngle+180)), (width) * cos(radians(iAngle+180)));
}
popMatrix();
}
void drawText()
{
pushMatrix();
textSize(30);
text("Lidar_Security_System", 30, 50);
fill(0,0,0);
noStroke();
fill(100, 100, 100);
textSize(30);
text("Angle:" + iAngle, 1050, 50); //Include Angle Information
text("Distance:" + iDistance, 1050, 100); //Include Distance Information
text(" cm", 1250, 100);
noStroke();
fill(98, 150, 31);
textSize(20);
text("1m", 700 + 80 - 40, 400);
text("2m", 700 + 155 -40, 400);
text("3m", 700 + 230 -40, 400);
text("4m", 700 + 305 -40, 400);
text("5m", 700 + 380 -40, 400);
noStroke();
fill(98, 150, 31);
textSize(20);
text("270°", 700 + 400, 408);
text("90°", 700 - 400 - 40, 408);
text("0°", 700 - 12.5, 20);
text("180°", 700 - 19, 800 - 4);
text("300°", 700 + 346.4101615 + 5, 400 - 200);
text("330°", 700 + 200, 400 - 346.4101615 - 5);
text("30°", 700 - 200 - 30, 400 - 346.4101615 - 5);
text("60°", 700 - 346.4101615 - 35, 400 - 200 - 5);
text("120°", 700 - 346.4101615 - 35, 400 + 200 + 20);
text("150°", 700 - 200 - 35, 400 + 346.4101615 + 20);
text("210°", 700 + 200, 400 + 346.4101615 + 20);
text("240°", 700 + 346.4101615, 400 + 200 + 20);
noStroke();
popMatrix();
}
Any help gratefully appreciated. Thanks