Hello, first time posting so forgive me if there is any etiquette I am not following.
My goal is actively adjust an audio delay (EchoEffect) using the TFmini-s LIDAR Module from Benewake. The challenge is that this module uses TX & RX which I assume from the code is serial, but the audio module (Nootropic Audio Hacker) uses analogRead input signal from a 10K potentiometer.
NOTE: I have successfully implemented both the EchoEffect and TFmini-s LIDAR codes by themselves, now I need to combine the two.
Essentially, I am trying to match an audio echo effect with the Nootropics Audio Hacker module based on the distance measured by the TFmini-s LIDAR Module
From the research I've gathered, this DJ shield can be used to adjust the settings of various code provided by Nootropic Design
- DJ Shield - DJ Shield – Assembled
I believe this line allows a potentiometer to adjust the delay of the audio echo effect.
//*Analog read 0 = A0 and is connected to the (first from left) potentiometer dial using DJ Shield
' echoDelay = analogRead(0) * 30; '
TF-mini-s Lidar Module TFMini-S - Micro LiDAR Module - SEN-16977 - SparkFun Electronics
This communicates with the Arduino using serial from the TX & RX (pins 0, and 1) - see the code below.
/*
#include <SoftwareSerial.h>
#include "TFMini.h"
TFMini tfmini;
//EDIT: The only value that matters here is the first one, 0, Rx (Where (RX, TX) --> (WHITE, GREEN) )
' SoftwareSerial SerialTFMini(0, 1); '
*/
Would it be easier to find a DAC module to complete this conversion or should/can this be done using a software solution?
'''
//BEGIN: EchoEffect Program
#include <AudioHacker.h>
#define DEBUG
unsigned int playbackBuf;
unsigned int sampleRate;
unsigned int readBuf[2];
unsigned int writeBuf;
boolean evenCycle = true;
unsigned int timer1Start;
volatile unsigned int timer1EndEven;
volatile unsigned int timer1EndOdd;
unsigned long lastDebugPrint = 0;
volatile long address = 0;
unsigned int echoDelay;
boolean echoWrapped = false;
void setup() {
#ifdef DEBUG
Serial.begin(115200); // connect to the serial port
#endif
playbackBuf = 2048;
sampleRate = DEFAULT_RECORDING_SAMPLE_RATE;
timer1Start = UINT16_MAX - (F_CPU / sampleRate);
AudioHacker.begin();
#ifdef DEBUG
Serial.print("sample rate = ");
Serial.print(sampleRate);
Serial.print(" Hz");
Serial.println();
#endif
}
void loop() {
delay(300);
// echoDelay is number of memory slots back into the past to read for the echo.
// must be a factor of 3 because we pack 2 samples into each 3-byte sequence of SRAM.
//EDIT: Analog read 0 = A0 and is connected to the (first from left) pot dial using DJ Shield
echoDelay = analogRead(0) * 30;
#ifdef DEBUG
if ((millis() - lastDebugPrint) >= 1000) {
lastDebugPrint = millis();
//each 3 echoDelay2 is 2 samples.
unsigned int delayMillis = (float)(((float)((echoDelay * 2) / 3)) / (float)(sampleRate/1000.0));
Serial.print("echo delay = ");
Serial.print(delayMillis);
Serial.print(" ms even cycles remaining = ");
Serial.print(UINT16_MAX - timer1EndEven);
Serial.print(" odd cycles remaining = ");
Serial.print(UINT16_MAX - timer1EndOdd);
Serial.println();
}
}
#endif
ISR(TIMER1_OVF_vect) {
TCNT1 = timer1Start;
unsigned int signal;
unsigned int echo;
int mix;
AudioHacker.writeDAC(playbackBuf);
// Read ADC
signal = AudioHacker.readADC();
if (evenCycle) {
long echoAddress = address - echoDelay;
if (echoAddress < 0) {
echoAddress += MAX_ADDR;
}
AudioHacker.readSRAMPacked(0, echoAddress, readBuf);
if ((!echoWrapped) && (echoAddress > address)) {
// avoid reading from unwritten memory
echo = 2048;
readBuf[1] = 2048;
} else {
echo = readBuf[0];
}
} else {
echo = readBuf[1];
}
if (echoDelay == 0) {
echo = 2048;
}
if (evenCycle) {
writeBuf = signal;
} else {
AudioHacker.writeSRAMPacked(0, address, writeBuf, signal);
address += 3;
if (address > MAX_ADDR) {
address = 0;
echoWrapped = true;
}
}
mix = signal-2048;
echo = echo >> 1; // attenuate echo
mix += (echo - 1024); // since we are dividing echo by 2, decrement by 1024
if (mix < -2048) {
mix = -2048;
} else {
if (mix > 2047) {
mix = 2047;
}
}
playbackBuf = mix + 2048;
#ifdef DEBUG
if (evenCycle) {
timer1EndEven = TCNT1;
} else {
timer1EndOdd = TCNT1;
}
#endif
evenCycle = !evenCycle;
}
//END: EchoEffect Program
'''
//BEGIN: TF-mini-s LIDAR Module Program
'''
#include <SoftwareSerial.h>
#include "TFMini.h"
TFMini tfmini;
//EDIT: The only value that matters here is the first one, 0, Rx (Where (RX, TX) --> (WHITE, GREEN) )
SoftwareSerial SerialTFMini(0, 1);
void getTFminiData(int* distance, int* strength)
{
static char i = 0;
char j = 0;
int checksum = 0;
static int rx[9];
if (SerialTFMini.available())
{
rx[i] = SerialTFMini.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()
{
Serial.begin(115200); //Initialize hardware serial port (serial debug port)
while (!Serial); // wait for serial port to connect. Needed for native USB port only
tft.println ("Initializing...");
SerialTFMini.begin(TFMINI_BAUDRATE); //Initialize the data rate for the SoftwareSerial port
tfmini.begin(&SerialTFMini); //Initialize the TF Mini sensor
}
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);
}
//END: TF-mini-s LIDAR Module Program
'''