Hello All,
I have used some basic scanning code from the internet and modified it significantly to work with the TFMini, a ‘LIDAR’ distance device (really IR). Many programs/websites out there use the same basic scanning program for the ultrasound distance device. My IR range is 14m but useful to about 10m (up to 100 cycles/ sec). I have modified the display to correct for the extra distance. I have added sounds and color changes to the program to enhance the fun of the device when detecting different range objects. Also plan to add a ‘distance ZOOM’ option.
But, again, the issue is similar to my sbus PWM problem. I have tried many variations but I do not know how to time a pinout HIGH for 10ms without using millis() (and not using a delay() ). In the loop, I have 4 millis() timers going , each for a short time. This works fine (but not optimal). What is my next step to improve this?
The purpose of the 10ms pinout HIGH is to trigger a sound function from a sound board:
- Only needs to be 10ms
- has to be momentary and then turn pinout LOW for remainder of L or R scan cycle
- due to loop construction, the ‘left’ and ‘right’ scans each initiate the pinout HIGH command when they begin(if criteria met) and the pinout HIGH also occurs when distance changes (even in the middle of a sweep).
Although it works, my code looks messy . What is the better way (array? I still have not figured this out)
Thanks for any input!
PS( I had to omit the RIGHT sweep code block due to exceeding max character length)
//Need these libraries
#include <TFT.h>
#include <SPI.h>
#include <TFMini.h>
#include <Servo.h>
//Teensy 3.2 pin definitions:
#define SERVO_PIN 6 // servo control signal
#define RST 8
#define DC 9
#define CS 10
#define MOSI 11 // defined in lib too, needs to be wired up
// can be commented out
//#define MISO 12 // not necessary to be defined or wired up
// for this program
#define SCK 13 // defined in lib too, needs to be wired up
// can be commented out
#define PINAUDIO1 16 // scanner start sound
#define PINAUDIO2 17 // scanner cycle
#define PINAUDIO3 18 // scanner object <6m
#define PINAUDIO4 19 // scanner object <3m
#define PINAUDIO5 20 // scanner object >6m
#define BLACK 0x0000 // 16 bit color choices
#define RED 0x001F
#define CYAN 0x07FF
#define GREEN 0x0400
#define MAGENTA 0xF81F
#define BLUE 0xF800
#define WHITE 0xFFFF
#define YELLOW 0xFFE0
TFT tft = TFT( CS, DC, RST );
Servo servo;
TFMini tfmini;
// distance char array to print to the screen
char rc_Printout[ 4 ];
int interval = 0;
double distance = 0;
int buttonState = 0;// for future button
unsigned long scanTime1 = 20;
unsigned long startMillis1;
unsigned long startMillis2;
unsigned long startMillis3;
unsigned long startMillis4;
unsigned long currentMillis1;
unsigned long currentMillis2;
unsigned long currentMillis3;
unsigned long currentMillis4;
void setup( )
{
servo.attach( SERVO_PIN );
Serial.begin(115200);
// setup sudio
pinMode(PINAUDIO1, OUTPUT);
pinMode(PINAUDIO2, OUTPUT);
pinMode(PINAUDIO3, OUTPUT);
pinMode(PINAUDIO4, OUTPUT);
pinMode(PINAUDIO5, OUTPUT);
digitalWrite(PINAUDIO1, HIGH);
delay(10);
digitalWrite(PINAUDIO1, LOW);
//start the TFMini
Serial1.begin(TFMINI_BAUDRATE);
tfmini.begin(&Serial1);
delay(100);
tfmini.setSingleScanMode();
startMillis1 = millis();
startMillis2 = millis();
startMillis3 = millis();
startMillis4 = millis();
//clear the TFT
tft.begin( );
tft.background( BLACK );
delay(200);
//cool start up
for ( int i = 0; i < 5; i ++ )
{
tft.setTextSize( 2 );
tft.stroke( WHITE );
tft.text( "WARNING", 40, 30 );
tft.text( "INITIALIZING", 10, 50 );
tft.text( "LIDAR", 50, 70 );
delay(500);
tft.background( BLACK );
}
digitalWrite(PINAUDIO2, HIGH);
delay(10);
digitalWrite(PINAUDIO2, LOW);
}
void loop( )
{
currentMillis1 = millis();
currentMillis2 = millis();
currentMillis3 = millis();
currentMillis4 = millis();
int r_beam = 100; // pixel length of green scan radius
int r2 = r_beam * .33;
int r3 = r_beam * .66;
int r4 = r_beam ;// used for distance sound (maybe)
tft.stroke( BLUE );
tft.circle( 80, 128, r_beam );
tft.circle ( 80, 128, r_beam - r2 );
tft.circle ( 80, 128, r_beam - r3);
tft.setTextSize( 2 );
tft.stroke( RED );
tft.text( "RANGE(m)", 10, 1 );
tft.setTextSize( 1 );
tft.stroke( RED );
tft.text( "10m", 3, 38 );
tft.setTextSize( 1 );
tft.text( "6.5m", 15, 78 );
tft.setTextSize( 1 );
tft.text( "3m", 30, 120 );
//Left display/servo rotation
for ( int i = 60; i < 120; i = i + 1 )
{
tfmini.externalTrigger();
uint16_t distance = tfmini.getDistance();
Serial.println(distance);
servo.write( i );
delay( 30 );
//Set the distance for LIDAR scan display and adjust to
//TFT meter ranges (green semi-circles). Currently active display
//range is the longest setting , 10m.
//TFMini not active below 30cm, max distance 14m
//This is for 50cm display range:
//int r = distance * 2;
//String r_Printout = String( r +2 );
//This is for 10m display range
int r = distance * .1;
String r_Printout = String( r * 0.1 );
if ( r < r2 ) {
if (currentMillis1 - startMillis1 < scanTime1) {
digitalWrite(PINAUDIO4, LOW);
delay(10);
}
else if ( r < r2 ) {
startMillis1 = currentMillis1;
digitalWrite(PINAUDIO4, HIGH);
delay(10);
}
}
if ( r < r3 && r > r2) {
if (currentMillis2 - startMillis2 < scanTime1) {
digitalWrite(PINAUDIO3, LOW);
delay(10);
}
else if ( r < r3 && r > r2) {
startMillis2 = currentMillis2;
digitalWrite(PINAUDIO3, HIGH);
delay(10);
}
}
tft.stroke( 0, 0, 0 );
tft.setTextSize( 2 );
tft.text( rc_Printout, 115, 0 );
r_Printout.toCharArray( rc_Printout, 4 );
tft.stroke( WHITE );
tft.setTextSize( 2 );
tft.text( rc_Printout, 115, 0 );
tft.stroke( GREEN );
tft.line( 80, 128, 80 + r_beam * cos( ( 360 - i ) * 3.14 / 180 ), 128 + r_beam * sin( ( 360 - i ) * 3.14 / 180 ) );
if ( r > 0 && r <= r_beam + 2 )
{
tft.circle( 80 + r * cos( ( 360 - i ) * 3.14 / 180 ), 128 + r * sin( ( 360 - i ) * 3.14 / 180 ), 2 );
tft.fillCircle( 80 + r * cos( ( 360 - i ) * 3.14 / 180 ), 128 + r * sin( ( 360 - i ) * 3.14 / 180 ), 2 , 0x001F );
if (r < r2) {
tft.fillCircle( 80 + r * cos( ( 360 - i ) * 3.14 / 180 ), 128 + r * sin( ( 360 - i ) * 3.14 / 180 ), 2 , 0xFFFF );
}
}
}
tft.background( BLACK );
tft.stroke( BLUE );
tft.circle( 80, 128, r_beam );
tft.circle ( 80, 128, r_beam - r2 );
tft.circle ( 80, 128, r_beam - r3);
tft.setTextSize( 2 );
tft.stroke( RED );
tft.text( "RANGE(m)", 10, 1 );
tft.setTextSize( 1 );
tft.stroke( RED );
tft.text( "10m", 3, 38 );
tft.setTextSize( 1 );
tft.text( "6.5m", 15, 78 );
tft.setTextSize( 1 );
tft.text( "3m", 30, 120 );
tft.background( BLACK );
}
uint16_t color565( uint8_t r, uint8_t g, uint8_t b )
{
return ( ( r & 0xF8 ) << 8 ) | ( ( g & 0xFC ) << 3 ) | ( b >> 3 );
}