I would like to sweep a Lidar TF Mini for an alarm system

Hi there everyone.

Thank you for taking the time to click on my post. I am new to programming and I am a bit struggling with some things I am trying to make my Arduino do. I have both a Uno R3 and Mega 2560 R3.

So I am working on this Metal Sculpture. It would be my sixth creation and I would like to make it tech infused... So it's basically a metal bust that would detect upcoming people. I want it to open it's eyes mechanically and electronically as a person approach and closes as they leave... not on a timer, but by it's distance... i also want an alarm to sound 1m away from it... I want it's eyes to be dim up green as it opens and dims down as it moves away and eyes shuts. I also want red for when it's within 1.5m.

So right now I have figured out the tf-mini and lights per distance. not the dimming part though. I have added a sweep code but now they seem to run at the same time and won't detect disance but servo is random and lights are random.

I will add my code and yeah... it's a mess.

I am willing to take any advice.

/*
Example code for Benewake TFMini time-of-flight distance sensor. 
by Peter Jansen (December 11/2017)
This example code is in the public domain.

This example communicates to the TFMini using a SoftwareSerial port at 115200, 
while communicating the distance results through the default Arduino hardware
Serial debug port. 

SoftwareSerial for some boards can be unreliable at high speeds (such as 115200). 
The driver includes some limited error detection and automatic retries, that
means it can generally work with SoftwareSerial on (for example) an UNO without
the end-user noticing many communications glitches, as long as a constant refresh
rate is not required. 

The (UNO) circuit:
 * Uno RX is digital pin 10 (connect to TX of TF Mini)
 * Uno TX is digital pin 11 (connect to RX of TF Mini)

THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/

#include <SoftwareSerial.h>
#include "TFMini.h"
#include <Servo.h>

// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

// Setup software serial port 
SoftwareSerial mySerial(10, 11);      // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini;
int ledR = 13;
int ledY = 12;
int ledG = 8;
Servo myservo;  // create servo object to control a servo
void setup() {
  
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(ledR, OUTPUT);
  pinMode(ledY, OUTPUT);
  pinMode(ledG, OUTPUT);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  // Step 1: Initialize hardware serial port (serial debug port)
  Serial.begin(115200);
  // wait for serial port to connect. Needed for native USB port only
  while (!Serial);
     
  Serial.println ("Initializing...");

  // Step 2: Initialize the data rate for the SoftwareSerial port
  mySerial.begin(TFMINI_BAUDRATE);

  // Step 3: Initialize the TF Mini sensor
  tfmini.begin(&mySerial);    

}


void loop() {
 
  
  // Take one TF Mini distance measurement
  uint16_t dist = tfmini.getDistance();
  uint16_t strength = tfmini.getRecentSignalStrength();

  // Display the measurement
  Serial.print(dist);
  Serial.print(" cm      sigstr: ");
  Serial.println(strength);

  // Wait some short time before taking the next measurement
  delay(25);  

  if (dist < 450 ){
  digitalWrite(ledR, HIGH);   // turn the LED on (HIGH is the voltage level)

  }
      else{
      digitalWrite(ledR, LOW);    // turn the LED off by making the voltage LOW
      }
      
  if (dist < 400 ){
  digitalWrite(ledR, HIGH);   // turn the LED on (HIGH is the voltage level)

  }
      else{
      digitalWrite(ledR, LOW);    // turn the LED off by making the voltage LOW    
      }
      
  if (dist < 350 ){
  digitalWrite(ledR, HIGH);   // turn the LED on (HIGH is the voltage level)

  }
      else{
      digitalWrite(ledR, LOW);    // turn the LED off by making the voltage LOW
      }
      
  if (dist < 300 ){
  digitalWrite(ledR, HIGH);   // turn the LED on (HIGH is the voltage level)

  }
      else{
      digitalWrite(ledR, LOW);    // turn the LED off by making the voltage LOW
      }
      
  if (dist < 250 ){
  digitalWrite(ledR, HIGH);   // turn the LED on (HIGH is the voltage level)

  }
      else{
      digitalWrite(ledR, LOW);    // turn the LED off by making the voltage LOW
      }
      
  if (dist < 200 ){
  digitalWrite(ledR, HIGH);   // turn the LED on (HIGH is the voltage level)

  }
      else{
      digitalWrite(ledR, LOW);    // turn the LED off by making the voltage LOW   
      }
  
  if (dist < 150 ){
  digitalWrite(ledR, HIGH);   // turn the LED on (HIGH is the voltage level)

  }
      else{
      digitalWrite(ledR, LOW);    // turn the LED off by making the voltage LOW
      }
  if (dist < 100 ){
  digitalWrite(ledR, HIGH);   // turn the LED on (HIGH is the voltage level)
  tone(7, 1140, 200);
  }
  if (dist > 450){
    digitalWrite(ledG, HIGH);
  }
  if (dist > 100) {
    for (pos = 0; pos <= 115; pos += 1) { // goes from 0 degrees to 180 degrees
  }
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  for (pos = 115; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15 ms for the servo to reach the position
  }
  else
    
  }

your code should reflect this. Probably a state machine architecture would be suited

sounds complicated. I will go read more about it.

it's not that complicated - basically you describe the various states your system can be in (idle, acquisition, eye animation, end of animation, ...) and describe what are the events which will make you transition from one state to the other and what actions need to be taken during the transition and during that state

Ok, thanks for the info. more tempting to go read now :smile:

Do you have this mechanical part working by itself? Until you do there is no sense continuing. You will need it for testing all the subsequent processes.

I'm not quite there with the sculpture yet. I haven't worked on the mechanism, I do some welding everyday and some work on the code as well. I don't see the mechanism being too complicate... compares to scissors and will be using a 13kg servo (Power HD, LF-13MG).

Test as you develop.

Then I shall work on the mechanism today. the eye opening is there. I have to make eyelids on pivots with a rod attached to the servo.

I will probably build mechanism and make a wooden rack to mount it on to test as the bust I am working on is quite large (16 inches high at the time).

1 Like

Do you mean they aren't doing what the code says, or that you haven't expressed correctly yet the thing you want them to do?

If they aren't following the code, you may have a power supply issue which woukd make us wonder about how you are ensuring adequate power delivery to the variuous parts of your device.

a7

So today I built my eyelid mechanism... I need couple M3 bolts. will need to go Ottawa tomorrow to grab some. Once I get the screws some nuts and washers, I will be able to put them in place and find the travel distance i need on the servo.

May I suggest shoulder bolts. you do not want to use threads as bearings.

It needs to be M3 so I could only find screws online at fastenal with double nuts and double washers for under 5$.

There is a local fastenal where I am going tomorrow and can easily pick them up.

Ask to look at their shoulder bolt selection. You seem to be building stuff like an old farmer would build.

I do things with what I have in hands yeah... Here is my 5th artpiece sold for 300$ and was built with scrap metal. Using an old set of Propane / Oxygene cutting torch and an arc welder. lots of fun :slight_smile:

I wouldn't want to come across this guy at night in a dark alley, glowing eyes or not :skull_and_crossbones:

Your new project sounds really cool. Will it be kept indoors? If so, have you considered this for the eyes (not for Uno or Mega):

Nice use of scrap. You are much more of an artist that I am. I just made tool type stuff. But sold all my scrap to the local recycler a couple of years ago. I think the grandkids will get the welding stuff.

These eyes are pretty cool. I do have a raspberry pi also. But for this one I think it will go outside so I will just go for the original idea. both my Servo and Lidar TF-Mini are good up to -20 celsius.

I still have to make a weather proof box for all my electronics to be safe outdoor.

I googled a bit this morning and found some M3 4mm shoulder bolts but price is out of my budget for now. If the normal M3 bolts causes issues because of thread then I will upgrade at a later date.

1 Like

Looking forward to seeing some pics of the completed project.

Hi,

I have completed the mechanism and servo is attached to it with sweep code and it works like a charm. the eyes simultaneously shut and open.


I am now trying to figure out the code for eyes to open when a certain distance is reached and I cannot succeed. I can have the servo sweep on its own and laser and lights on its own but can't seem to combine them... Maybe a power supply issue?

#include <SoftwareSerial.h>
#include "TFMini.h"
#include <Servo.h>

// twelve servo objects can be created on most boards



// Setup software serial port 
SoftwareSerial

mySerial(10, 11);      // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini;

int ledR = 13;
int ledG = 8;
int pos1 = 0;
int pos2 = 30;
Servo myservo;  // create servo object to control a servo


void setup() {
  
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(ledR, OUTPUT);
  pinMode(ledG, OUTPUT);
   myservo.attach(9);
  // Step 1: Initialize hardware serial port (serial debug port)
  Serial.begin(115200);
  // wait for serial port to connect. Needed for native USB port only
  while (!Serial);
     
  Serial.println ("Initializing...");

  // Step 2: Initialize the data rate for the SoftwareSerial port
  mySerial.begin(TFMINI_BAUDRATE);

  // Step 3: Initialize the TF Mini sensor
  tfmini.begin(&mySerial);    


 
}

void loop() {
 
  
  // Take one TF Mini distance measurement
  uint16_t dist = tfmini.getDistance();
  uint16_t strength = tfmini.getRecentSignalStrength();

  // Display the measurement
  Serial.print(dist);
  Serial.print(" cm      sigstr: ");
  Serial.println(strength);

  // Wait some short time before taking the next measurement
  delay(25);
  
{
    if (dist > 400 ){
    for(pos1 = 0; pos2 <= 30; pos2 += 1);
    digitalWrite(ledG, HIGH);   // turn the LED on (HIGH is the voltage level)
    tone(12, 66, 200);
    myservo.write(pos2);
    delay(15);
    }
    if (dist < 150){
    for(pos2 = 30; pos2 >= 0; pos2 -= 1);
    digitalWrite(ledR, HIGH);
    tone(12.1066,200);
    myservo.write(pos2);
    delay(15);
  }}}
  
2 Likes