Hi
I am trying to make a 3D room scanner as a project with a recent purchase of my TFmini UART edition and have kinda hit a brick wall with it. I am using processing to plot the 3D coordinates that I have created by converting for spherical to cartesian on the Arduino then sending this over using a serial connection to processing to visualize this point cloud. The main problem is that I can't get a stable enough connection with the TFmini to scan only a few scans before reading a constant reading of either -3 or 65335 which I have seen as an error online but now solution to yet.
I plan to use a ESP32 later on but I thought it may be easier to do it on the mega as a first prototype.
Im currently using:
TFmini
Arduino mega 2560
2 x SG 90 Servo motor
3D printed tilt scan (of Thingiverse)
Logic level converter
My questions
[1] Will this idea work with the existing setup or should I start a new?
[2] Why does the code consistently show -3 which only resets after rebooting the Arduino mega.
My code:
#include <Servo.h> // Library used for servo motors with predefined functions and methods
#include <SoftwareSerial.h>
#include "TFMini.h"
Servo tiltServo; // Connceting Servos to libary
Servo panServo;
//Variables initialised here so that they can be used later on.
int tiltPos = 0;
int panPos = 0;
int distance;
long duration;
int x,y,z;
float deg2rad = 3.14159265 / 180.0;
SoftwareSerial mySerial(10,11);
TFMini tfmini;
void setup() {
//Serial Connection started for communcation with processing
Serial.begin(115200);
mySerial.begin(TFMINI_BAUDRATE);
tfmini.begin(&mySerial);
//Defining the pins of the two servos in use
tiltServo.attach(9);
panServo.attach(6);
/* For loops used to tilt from 90(down) degrees to 0(up).
* For each tilt degree the other servo pans from 0 to 180 degrees getting a distance value for each degree.
* Every value is turned into Cartesian values from spherical.
* Then transmitted through the serial USB connection then into processing.
*/
for(tiltPos = 45; tiltPos>=0; tiltPos--){
for(panPos = 0; panPos <=180; panPos++){
panServo.write(panPos);
float dist = get_DISTANCE();
float theta = deg2rad * (panPos);
float phi = deg2rad * (tiltPos) ;
double x = dist*sin(phi)*cos(theta);
double y = dist*sin(phi)*sin(theta);
double z = dist*cos(phi);
Serial.println(String(x, 5) + " " + String(y, 5) + " " + String(-z, 5));
Serial.println(dist);
delay(25);
}
tiltServo.write(tiltPos);
delay(25);
}
}
/*
* Function has a intger ouput that will go into the dist variable above.
* Sends out ultrasonic waves (so we can’t hear them) and then times how long it takes to read it again.
* Then using distance = speed * time then divding by 2 as it has travelled from the sensor and back again a reading is observed.
*/
int get_DISTANCE(){
uint16_t distance = int(tfmini.getDistance());
//Serial.println(distance);
return distance;
}
void loop() {
// put your main code here, to run repeatedly:
}
Picture