I have this code for the TFMini Lidar sensors and Speaker. I Need help figuring why I'm using this calculation in my code and why if I removed it the lidar will not give me any measurement? (see below plz).
And my code now it's only one sensor is working pins (4,5) I don't know why the three of them don't work together. Thanks in advance.
I took this code from the internet My coding experience is basic .
rx[i] = TFMiniOne.read();
if (rx[0] != 0x59) {
i = 0;
} else if (i == 1 && rx[1] != 0x59) {
i = 0;
} else if (i == 8) {
for (j = 0; j < 8; j++) {
checksum += rx[j];
}
if (rx[8] == (checksum % 256)) {
*distance = rx[2] + rx[3] * 256;
*strength = rx[4] + rx[5] * 256;
}
i = 0;
} else {
i++;
}
Here is the complete code:
#include "Talkie.h"
#include "Vocab_US_Large.h"
#include <SoftwareSerial.h>
#include "TFMini.h"
TFMini tfminiOne;
TFMini tfminiTwo;
TFMini tfminiThree;
Talkie voice;
const int Speaker = 3;
//Declaring variables and constants
SoftwareSerial TFMiniOne (4,5); //(RX, TX)
SoftwareSerial TFMiniTwo (6,7);
SoftwareSerial TFMiniThree (8,9);
void getTFminiData(int* distance, int* strength) {
static char i = 0;
char j = 0;
int checksum = 0;
static int rx[9];
if (TFMiniOne.available()) {
// Serial.println( "tfmini serial available" );
rx[i] = TFMiniOne.read();
if (rx[0] != 0x59) {
i = 0;
} else if (i == 1 && rx[1] != 0x59) {
i = 0;
} else if (i == 8) {
for (j = 0; j < 8; j++) {
checksum += rx[j];
}
if (rx[8] == (checksum % 256)) {
*distance = rx[2] + rx[3] * 256;
*strength = rx[4] + rx[5] * 256;
}
i = 0;
} else {
i++;
}
}
}
void setup() {
pinMode(Speaker, OUTPUT);
//Initializing
Serial.begin (115200);
while(!Serial); //Wait for USB Serial Port to connect
Serial.println("Initializing...");
TFMiniOne.begin (TFMINI_BAUDRATE);
//Initializing TFMini Sensors
tfminiOne.begin(&TFMiniOne);
tfminiTwo.begin(&TFMiniTwo);
}
void loop() {
int distance = 0;
int strength = 0;
getTFminiData(&distance, &strength);
while (!distance) {
getTFminiData(&distance, &strength);
if (distance) {
Serial.print(distance);
Serial.print("cm\t");
Serial.print("strength: ");
Serial.println(strength);
}
}
delay(100);
if (distance > 30) {
digitalWrite(Speaker,HIGH);
voice.say(sp2_ALERT);
voice.say(sp2_F);
} else {
digitalWrite(Speaker,LOW);
}
if (distance > 20) {
digitalWrite(Speaker,HIGH);
voice.say(sp2_ALERT);
voice.say(sp2_L);
} else {
digitalWrite(Speaker,LOW);
}
if (distance < 15) {
digitalWrite(Speaker,HIGH);
voice.say(sp2_ALERT);
voice.say(sp2_R);
} else {
digitalWrite(Speaker,LOW);
}
delay(300);